Class: Makers::Proxy
Instance Method Summary
collapse
Methods included from Callbacks
#callbacks
Constructor Details
#initialize(maker, &block) ⇒ Proxy
Returns a new instance of Proxy.
5
6
7
8
9
10
|
# File 'lib/makers/proxy.rb', line 5
def initialize(maker, &block)
@maker = maker
@attributes = []
@sequences = {}
instance_eval &block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/makers/proxy.rb', line 31
def method_missing(name, *args, &block)
unless name == :maker
options = args.
strategy = options.delete(:strategy) || :build
if block_given?
logic = block
elsif maker = Makers.definitions.find(name) rescue nil
logic = -> { maker.send(strategy, options) }
elsif maker = Makers.definitions.find(name.to_s.singularize.to_sym) rescue nil
logic = -> { maker.send(strategy, (args.first || 1), options) }
elsif args.any?
logic = -> { args.first }
end
if defined? logic
@attributes.send (block_given? ? :push : :unshift), name
class_eval { define_method(name, logic) }
end
end
end
|
Instance Method Details
#attributes ⇒ Object
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/makers/proxy.rb', line 12
def attributes
{}.tap do |hash|
if @maker.parent
hash.merge! @maker.parent.proxy.attributes
end
@attributes.each do |name|
hash[name] = send(name)
end
end
end
|
#sequence(name, &block) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/makers/proxy.rb', line 23
def sequence(name, &block)
@attributes << name
@sequences[name] = Sequence.new(&block)
class_eval do
define_method(name) { @sequences[name] }
end
end
|