Class: Machinery::Object
- Inherits:
-
Object
show all
- Defined in:
- lib/object.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Object
Returns a new instance of Object.
59
60
61
|
# File 'lib/object.rb', line 59
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/object.rb', line 98
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)
else
if @attributes.has_key?(name.to_s)
if !args.empty?
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
end
@attributes[name.to_s]
else
nil
end
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
57
58
59
|
# File 'lib/object.rb', line 57
def attributes
@attributes
end
|
Class Method Details
.convert_element(key, value) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# 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 ::Array
Machinery::Array.from_json(value)
when Hash
Machinery::Object.from_json(value)
else
value
end
end
end
|
.convert_raw_hash(hash) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/object.rb', line 42
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
52
53
54
|
# File 'lib/object.rb', line 52
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?
73
74
75
|
# File 'lib/object.rb', line 73
def ==(other)
self.class == other.class && @attributes == other.attributes
end
|
86
87
88
|
# File 'lib/object.rb', line 86
def [](key)
@attributes[key.to_s]
end
|
#[]=(key, value) ⇒ Object
90
91
92
|
# File 'lib/object.rb', line 90
def []=(key, value)
@attributes[key.to_s] = self.class.convert_element(key, value)
end
|
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/object.rb', line 131
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
146
147
148
|
# File 'lib/object.rb', line 146
def compare_with(other)
self == other ? [nil, nil, self] : [self, other, nil]
end
|
#empty? ⇒ Boolean
94
95
96
|
# File 'lib/object.rb', line 94
def empty?
@attributes.keys.empty?
end
|
82
83
84
|
# File 'lib/object.rb', line 82
def hash
@attributes.hash
end
|
#initialize_copy(orig) ⇒ Object
126
127
128
129
|
# File 'lib/object.rb', line 126
def initialize_copy(orig)
super
@attributes = @attributes.dup
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
118
119
120
121
122
123
124
|
# File 'lib/object.rb', line 118
def respond_to?(name, include_all = false)
if name.to_s.end_with?("=")
true
else
@attributes.has_key?(name) || super(name, include_all)
end
end
|
#set_attributes(attrs) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/object.rb', line 63
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
|