Class: Machinery::Object
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Object
Returns a new instance of Object.
62
63
64
|
# File 'lib/object.rb', line 62
def initialize(attrs = {})
set_attributes(attrs)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/object.rb', line 108
def method_missing(name, *args, &block)
if name.to_s.end_with?("=")
if args.size != 1
raise ArgumentError, "wrong number of arguments (#{args.size} for 1)"
end
key = name.to_s[0..-2]
@attributes[key] = self.class.convert_element(key, args.first)
elsif @attributes.key?(name.to_s)
unless args.empty?
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
end
@attributes[name.to_s]
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
59
60
61
|
# File 'lib/object.rb', line 59
def attributes
@attributes
end
|
Returns the value of attribute scope.
60
61
62
|
# File 'lib/object.rb', line 60
def scope
@scope
end
|
Class Method Details
.convert_element(key, value) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/object.rb', line 26
def convert_element(key, value)
property_class = @property_classes[key.to_s] if @property_classes
if property_class
value.is_a?(property_class) ? value : property_class.from_json(value)
else
case value
when Hash
if value.keys.include?("_elements")
Machinery::Array.from_json(value)
else
Machinery::Object.from_json(value)
end
else
value
end
end
end
|
.convert_raw_hash(hash) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/object.rb', line 44
def convert_raw_hash(hash)
return nil unless hash
entries = hash.map do |key, value|
[key, convert_element(key, value)]
end
Hash[entries]
end
|
.from_json(json_object) ⇒ Object
54
55
56
|
# File 'lib/object.rb', line 54
def from_json(json_object)
new(json_object)
end
|
.has_property(name, options) ⇒ Object
21
22
23
24
|
# File 'lib/object.rb', line 21
def has_property(name, options)
@property_classes ||= {}
@property_classes[name.to_s] = options[:class]
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
83
84
85
|
# File 'lib/object.rb', line 83
def ==(other)
self.class == other.class && @attributes == other.attributes
end
|
96
97
98
|
# File 'lib/object.rb', line 96
def [](key)
@attributes[key.to_s]
end
|
#[]=(key, value) ⇒ Object
100
101
102
|
# File 'lib/object.rb', line 100
def []=(key, value)
@attributes[key.to_s] = self.class.convert_element(key, value)
end
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/object.rb', line 137
def as_json
entries = @attributes.map do |key, value|
case value
when Machinery::Array, Machinery::Object
value_json = value.as_json
else
value_json = value
end
[key, value_json]
end
Hash[entries]
end
|
#compare_with(other) ⇒ Object
152
153
154
|
# File 'lib/object.rb', line 152
def compare_with(other)
self == other ? [nil, nil, nil, self] : [self, other, nil, nil]
end
|
#empty? ⇒ Boolean
104
105
106
|
# File 'lib/object.rb', line 104
def empty?
@attributes.keys.empty?
end
|
92
93
94
|
# File 'lib/object.rb', line 92
def hash
@attributes.hash
end
|
#initialize_copy(orig) ⇒ Object
132
133
134
135
|
# File 'lib/object.rb', line 132
def initialize_copy(orig)
super
@attributes = @attributes.dup
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
124
125
126
127
128
129
130
|
# File 'lib/object.rb', line 124
def respond_to?(name, include_all = false)
if name.to_s.end_with?("=")
true
else
@attributes.key?(name) || super(name, include_all)
end
end
|
#set_attributes(attrs) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/object.rb', line 73
def set_attributes(attrs)
attrs = self.class.convert_raw_hash(attrs) if attrs.is_a?(Hash)
@attributes = attrs.inject({}) do |attributes, (key, value)|
key = key.to_s if key.is_a?(Symbol)
attributes[key] = value
attributes
end
end
|