Skip to content

Commit 17c2421

Browse files
committed
Create Executor make it testable
1 parent e70495b commit 17c2421

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/active_method.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
require_relative "active_method/version"
44

55
module ActiveMethod
6-
autoload :Base, "active_method/base"
76
autoload :Util, "active_method/util"
7+
autoload :Executor, "active_method/executor"
8+
autoload :Base, "active_method/base"
89

910
def self.included(base)
1011
base.extend ClassMethods
@@ -15,9 +16,7 @@ def active_method(name, method_class = nil)
1516
method_class ||= Util.constantize self, Util.camel_case(name)
1617

1718
define_method name do |*args|
18-
method = method_class.new(*args)
19-
method.__set_owner(self)
20-
method.call
19+
method_class[self].call(*args)
2120
end
2221
end
2322
end

lib/active_method/base.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def initialize(name, default: nil)
1212

1313
class << self
1414

15+
def [](owner)
16+
Executor.new(self, owner)
17+
end
18+
alias_method :on, :[]
19+
1520
def call(*args)
1621
new(*args).call
1722
end
@@ -96,9 +101,6 @@ def initialize(*args)
96101
end
97102
end
98103

99-
def call
100-
end
101-
102104
def __set_owner(owner)
103105
@__method_owner = owner
104106

@@ -108,6 +110,9 @@ def __set_owner(owner)
108110
end
109111
end
110112

113+
def call
114+
end
115+
111116
private
112117

113118
def arguments

lib/active_method/executor.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module ActiveMethod
2+
class Executor
3+
4+
attr_reader :method_class, :owner
5+
6+
def initialize(klass, owner)
7+
@method_class = klass
8+
@owner = owner
9+
end
10+
11+
def call(*args)
12+
method = method_class.new(*args)
13+
method.__set_owner(owner)
14+
method.call
15+
end
16+
17+
end
18+
end

0 commit comments

Comments
 (0)