Class: ObjectMomma::Builder
- Inherits:
-
Object
- Object
- ObjectMomma::Builder
show all
- Extended by:
- ClassAttributes
- Defined in:
- lib/object_momma/builder.rb
Class Method Summary
collapse
Instance Method Summary
collapse
class_attribute, inherited
Constructor Details
#initialize(child) ⇒ Builder
Returns a new instance of Builder.
48
49
50
|
# File 'lib/object_momma/builder.rb', line 48
def initialize(child)
@child = child
end
|
Class Method Details
.builder_for(object_type) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/object_momma/builder.rb', line 61
def self.builder_for(object_type)
if ObjectMomma.builder_path
builder_file = File.join(ObjectMomma.builder_path, "#{object_type}_builder.rb")
require builder_file if File.size?(builder_file)
end
classified_name = "_#{object_type}Builder".gsub(/_\w/) do |underscored|
underscored[1].upcase
end
ObjectMomma.const_get(classified_name)
end
|
.child_id(&block) ⇒ Object
132
133
134
|
# File 'lib/object_momma/builder.rb', line 132
def child_id(&block)
self.child_id_serializer = block
end
|
.has_child_id_serializer? ⇒ Boolean
74
75
76
|
# File 'lib/object_momma/builder.rb', line 74
def self.has_child_id_serializer?
self.child_id_serializer.respond_to?(:to_proc)
end
|
.has_siblings(*args) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/object_momma/builder.rb', line 136
def has_siblings(*args)
if args.last.is_a?(Hash)
hash = args.pop.each_with_object({}) do |(sibling_name, object_type), hash|
hash[sibling_name] = object_type
end
else
hash = {}
end
args.each_with_object(hash) do |sibling_name|
object_type = sibling_name
hash[sibling_name] = object_type
end
self.siblings = hash
end
|
.has_siblings? ⇒ Boolean
78
79
80
|
# File 'lib/object_momma/builder.rb', line 78
def self.has_siblings?
self.siblings.is_a?(Hash)
end
|
.run_with_binding(props = {}, &block) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/object_momma/builder.rb', line 82
def self.run_with_binding(props = {}, &block)
Object.new.tap do |o|
props.each do |name, value|
ivar_name = "@#{name}".to_sym
o.singleton_class.class_eval do
define_method(name) { instance_variable_get(ivar_name) }
end
o.instance_variable_set(ivar_name, value)
end
o.singleton_class.class_eval(&block) if block_given?
end.instance_exec(&child_id_serializer)
end
|
.string_to_hash(object_type, string) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/object_momma/builder.rb', line 96
def self.string_to_hash(object_type, string)
builder = builder_for(object_type)
if builder.has_child_id_serializer?
vars = []
builder.run_with_binding(vars: vars) do
def method_missing(sym, *args, &block)
return super unless args.empty? && !block_given?
vars << sym unless vars.include?(sym)
end
end
string_matcher = "([#{VALID_IDENTIFIER_CHARS}]+)"
regex_string = builder.run_with_binding(string_matcher: string_matcher) do
def method_missing(sym, *args, &block)
return super unless args.empty? && !block_given?
string_matcher
end
end
regex = Regexp.new("^#{regex_string}$")
matches = string.match(regex).to_a[1..-1]
if matches.nil?
raise BadChildIdentifier, "Bad child_id `#{string}' for builder "\
"`#{name}'"
end
Hash[vars.zip(matches)]
else
{child_id: string}
end
end
|
Instance Method Details
#build!(*args) ⇒ Object
40
41
42
|
# File 'lib/object_momma/builder.rb', line 40
def build!(*args)
raise Objectmomma::SubclassNotImplemented
end
|
#build_child_from_hash(hash, &block) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/object_momma/builder.rb', line 12
def build_child_from_hash(hash, &block)
if has_child_id_serializer?
child.child_id = self.class.run_with_binding(hash)
hash.each do |name, value|
if has_siblings?
sibling_object_type = self.class.siblings[name]
if sibling_object_type
if value.is_a?(ObjectMomma::Child)
child = value
else
child = yield(sibling_object_type, value)
end
value = child.child_object
end
end
ivar_name = "@#{name}"
instance_variable_set(ivar_name, value)
self.singleton_class.instance_exec(name, ivar_name) do |name, ivar_name|
define_method(name) { instance_variable_get(ivar_name) }
end
end
else
child.child_id = hash.delete(:child_id)
end
end
|
#child_id ⇒ Object
44
45
46
|
# File 'lib/object_momma/builder.rb', line 44
def child_id
child.child_id
end
|
#is_persisted?(object) ⇒ Boolean
52
53
54
55
56
57
58
59
|
# File 'lib/object_momma/builder.rb', line 52
def is_persisted?(object)
if object.respond_to?(:persisted?)
object.persisted?
else
raise ObjectMomma::SubclassNotImplemented, "Override #is_persisted? "\
"to support objects that do not respond to #persisted?"
end
end
|