Module: Gorillib::Builder
Defined Under Namespace
Modules: ClassMethods
Classes: GetsetCollectionField, GetsetField, MemberField
Constant Summary
collapse
- CollectionField =
GetsetCollectionField
Instance Method Summary
collapse
-
#collection_of(plural_name) ⇒ Object
-
#get_collection_item(plural_name, item_key) ⇒ Object
-
#getset(field, *args, &block) ⇒ Object
-
#getset_collection_item(field, item_key, attrs = {}, &block) ⇒ Object
-
#getset_member(field, *args, &block) ⇒ Object
-
#has_collection_item?(plural_name, item_key) ⇒ Boolean
-
#key_method ⇒ Object
-
#receive!(*args, &block) ⇒ Object?
The return value of the block, or nil if no block given.
-
#set_collection_item(plural_name, item_key, item) ⇒ Object
-
#to_inspectable ⇒ Object
-
#to_key ⇒ Object
Methods included from Concern
append_features, extended, included
Methods included from Model
#==, #as_json, #attribute_set?, #attribute_values, #attributes, #compact_attributes, #handle_extra_attributes, #initialize, #inspect, #inspect_compact, #read_attribute, #read_unset_attribute, #to_json, #to_s, #to_tsv, #to_wire, #unset_attribute, #update_attributes, #write_attribute
Instance Method Details
#collection_of(plural_name) ⇒ Object
96
97
98
|
# File 'lib/gorillib/builder.rb', line 96
def collection_of(plural_name)
self.read_attribute(plural_name)
end
|
#get_collection_item(plural_name, item_key) ⇒ Object
70
71
72
|
# File 'lib/gorillib/builder.rb', line 70
def get_collection_item(plural_name, item_key)
collection_of(plural_name)[item_key]
end
|
#getset(field, *args, &block) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/gorillib/builder.rb', line 18
def getset(field, *args, &block)
ArgumentError.check_arity!(args, 0..1)
if args.empty?
read_attribute(field.name)
else
write_attribute(field.name, args.first)
end
end
|
#getset_collection_item(field, item_key, attrs = {}, &block) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/gorillib/builder.rb', line 52
def getset_collection_item(field, item_key, attrs={}, &block)
plural_name = field.plural_name
if attrs.is_a?(field.item_type)
val = attrs
set_collection_item(plural_name, item_key, val)
elsif has_collection_item?(plural_name, item_key)
val = get_collection_item(plural_name, item_key)
val.receive!(attrs, &block)
else
val = field.item_type.receive({ key_method => item_key, :owner => self }.merge(attrs), &block)
set_collection_item(plural_name, item_key, val)
end
val
end
|
#getset_member(field, *args, &block) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/gorillib/builder.rb', line 27
def getset_member(field, *args, &block)
ArgumentError.check_arity!(args, 0..1)
attrs = args.first
if attrs.is_a?(field.type)
val = attrs
write_attribute(field.name, val)
else
val = read_attribute(field.name)
if val.present?
val.receive!(*args, &block) if args.present? or block_given?
elsif attrs.blank? and not block_given?
return nil
else
options = args..merge(:owner => self)
val = field.type.receive(*args, options, &block)
write_attribute(field.name, val)
end
end
val
end
|
#has_collection_item?(plural_name, item_key) ⇒ Boolean
80
81
82
|
# File 'lib/gorillib/builder.rb', line 80
def has_collection_item?(plural_name, item_key)
collection_of(plural_name).include?(item_key)
end
|
#key_method ⇒ Object
84
85
86
|
# File 'lib/gorillib/builder.rb', line 84
def key_method
:name
end
|
#receive!(*args, &block) ⇒ Object?
Returns the return value of the block, or nil if no block given.
11
12
13
14
15
16
|
# File 'lib/gorillib/builder.rb', line 11
def receive!(*args, &block)
super(*args)
if block_given?
(block.arity == 1) ? block.call(self) : self.instance_eval(&block)
end
end
|
#set_collection_item(plural_name, item_key, item) ⇒ Object
74
75
76
77
78
|
# File 'lib/gorillib/builder.rb', line 74
def set_collection_item(plural_name, item_key, item)
collection = collection_of(plural_name)
collection[item_key] = item
collection[item_key]
end
|
#to_inspectable ⇒ Object
92
93
94
|
# File 'lib/gorillib/builder.rb', line 92
def to_inspectable
super.tap{|attrs| attrs.delete(:owner) }
end
|
88
89
90
|
# File 'lib/gorillib/builder.rb', line 88
def to_key
self.send(key_method)
end
|