Module: ObjectMomma::ModuleMethods
- Included in:
- ObjectMomma
- Defined in:
- lib/object_momma/module_methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/object_momma/module_methods.rb', line 7
def method_missing(method_name, *args, &block)
return super unless respond_to?(method_name)
return super if block_given?
object_type = object_type_from_attributes_getter(method_name)
if object_type
args.push(:find_or_create)
child = ObjectMomma::Child.new(object_type, *args)
return child.attributes_for_child
end
object_type, actualize_strategy = object_type_and_actualize_strategy_from_method_name(method_name)
args.push(actualize_strategy)
child = ObjectMomma::Child.new(object_type, *args)
child.child_object
end
|
Instance Method Details
#builder_for(object_type) ⇒ Object
#mullet! ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/object_momma/module_methods.rb', line 25
def mullet!
return false if Object.const_defined?(:ObjectMother)
object_mother = Class.new(BasicObject) do
def self.method_missing(*args)
ObjectMomma.send(*args)
end
end
Object.const_set(:ObjectMother, object_mother)
true
end
|
#object_type_and_actualize_strategy_from_method_name(method_name) ⇒ Object
Also known as:
parse_method_name
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/object_momma/module_methods.rb', line 53
def object_type_and_actualize_strategy_from_method_name(method_name)
begin
builder_for(method_name)
object_type = method_name.to_sym
return [object_type, :find_or_create]
rescue NameError
end
public_method_name, object_type = [*method_name.to_s.match(/^(create|find|spawn)_(\w+)$/).to_a[1..-1]].compact.map(&:to_sym)
return nil if object_type.nil?
begin
builder_for(object_type)
if public_method_name == :spawn
actualize_strategy = :find_or_create
else
actualize_strategy = public_method_name
end
[object_type, actualize_strategy]
rescue NameError
nil
end
end
|
#object_type_from_attributes_getter(method_name) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/object_momma/module_methods.rb', line 38
def object_type_from_attributes_getter(method_name)
return nil unless ObjectMomma.use_serialized_attributes
match = method_name.to_s.match(%r{^(\w+)_attributes$}).to_a[1..-1]
return nil unless match
object_type = match[0]
begin
builder_for(object_type)
object_type
rescue NameError
nil
end
end
|
#respond_to?(method_name, *args) ⇒ Boolean
80
81
82
83
84
|
# File 'lib/object_momma/module_methods.rb', line 80
def respond_to?(method_name, *args)
return true if super
return true if object_type_from_attributes_getter(method_name)
parse_method_name(method_name).nil? ? false : true
end
|
#spawn(hash = {}) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/object_momma/module_methods.rb', line 86
def spawn(hash = {})
hash.each do |object_type, child_id_or_ids|
begin
builder_for(object_type)
rescue NameError => ne
singularized = object_type.to_s.chomp('s').to_sym
raise ne if singularized == object_type
builder_for(singularized)
object_type = singularized
end
child_ids = [*child_id_or_ids]
child_ids.each { |child_id| send("spawn_#{object_type}", child_id) }
end
end
|