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.
52
53
54
55
56
57
58
59
|
# File 'lib/object.rb', line 52
def initialize(attrs = {})
@attributes = attrs.inject({}) do |attributes, (key, value)|
key = key.to_sym if key.respond_to?(:to_sym)
attributes[key] = value
attributes
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/object.rb', line 86
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
@attributes[name.to_s[0..-2].to_sym] = args.first
else
if @attributes.has_key?(name)
if !args.empty?
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
end
@attributes[name]
else
nil
end
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
50
51
52
|
# File 'lib/object.rb', line 50
def attributes
@attributes
end
|
Class Method Details
.from_json(json) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/object.rb', line 26
def from_json(json)
return nil unless json
entries = json.map do |key, value|
value_converted = if @property_classes && @property_classes[key.to_sym]
@property_classes[key.to_sym].from_json(value)
else
case value
when ::Array
Machinery::Array.from_json(value)
when Hash
Machinery::Object.from_json(value)
else
value
end
end
[key, value_converted]
end
new(Hash[entries])
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_sym] = options[:class]
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
61
62
63
|
# File 'lib/object.rb', line 61
def ==(other)
self.class == other.class && @attributes == other.attributes
end
|
74
75
76
|
# File 'lib/object.rb', line 74
def [](key)
@attributes[key.to_sym]
end
|
#[]=(key, value) ⇒ Object
78
79
80
|
# File 'lib/object.rb', line 78
def []=(key, value)
@attributes[key.to_sym] = value
end
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/object.rb', line 119
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
134
135
136
|
# File 'lib/object.rb', line 134
def compare_with(other)
self == other ? [nil, nil, self] : [self, other, nil]
end
|
#empty? ⇒ Boolean
82
83
84
|
# File 'lib/object.rb', line 82
def empty?
@attributes.keys.empty?
end
|
70
71
72
|
# File 'lib/object.rb', line 70
def hash
@attributes.hash
end
|
#initialize_copy(orig) ⇒ Object
114
115
116
117
|
# File 'lib/object.rb', line 114
def initialize_copy(orig)
super
@attributes = @attributes.dup
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
106
107
108
109
110
111
112
|
# File 'lib/object.rb', line 106
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
|