Class: Zoop::ZoopObject
Direct Known Subclasses
Model
Constant Summary
collapse
- RESOURCES =
Dir[File.expand_path('../resources/*.rb', __FILE__)].map do |path|
File.basename(path, '.rb').to_sym
end
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(response = {}) ⇒ ZoopObject
Returns a new instance of ZoopObject.
9
10
11
12
13
|
# File 'lib/zoop/object.rb', line 9
def initialize(response = {})
@attributes = Hash.new
@unsaved_attributes = Set.new
update_model response
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/zoop/object.rb', line 85
def method_missing(name, *args, &block)
name = name.to_s
unless block_given?
if name.end_with?('=') && args.size == 1
attribute_name = name[0...-1]
return self[attribute_name] = args[0]
end
if args.size == 0
return self[name] || self[name.to_sym]
end
end
if attributes.respond_to? name
return attributes.public_send name, *args, &block
end
super name, *args, &block
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/zoop/object.rb', line 3
def attributes
@attributes
end
|
Class Method Details
.convert(response) ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/zoop/object.rb', line 108
def convert(response)
case response
when Array
response.map{ |i| convert i }
when Hash
resource_class_for(response['resource']).new(response)
else
response
end
end
|
Instance Method Details
#==(other) ⇒ Object
24
25
26
|
# File 'lib/zoop/object.rb', line 24
def ==(other)
self.class == other.class && id == other.id
end
|
#[]=(key, value) ⇒ Object
15
16
17
18
|
# File 'lib/zoop/object.rb', line 15
def []=(key,value)
@attributes[key] = value
@unsaved_attributes.add key
end
|
#empty? ⇒ Boolean
20
21
22
|
# File 'lib/zoop/object.rb', line 20
def empty?
@attributes.empty?
end
|
#respond_to?(name, include_all = false) ⇒ Boolean
40
41
42
43
44
|
# File 'lib/zoop/object.rb', line 40
def respond_to?(name, include_all = false)
return true if name.to_s.end_with? '='
@attributes.has_key?(name.to_s) || super
end
|
34
35
36
37
38
|
# File 'lib/zoop/object.rb', line 34
def to_hash
Hash[@attributes.map do |key, value|
[ key, to_hash_value(value, :to_hash) ]
end]
end
|
#to_s ⇒ Object
Also known as:
inspect
46
47
48
49
50
51
52
|
# File 'lib/zoop/object.rb', line 46
def to_s
attributes_str = ''
(attributes.keys - ['id', 'object']).sort.each do |key|
attributes_str += " \033[1;33m#{key}:\033[0m#{self[key].inspect}" unless self[key].nil?
end
"\033[1;31m#<#{self.class.name}:\033[0;32m#{id}#{attributes_str}\033[0m\033[0m\033[1;31m>\033[0m"
end
|
#unsaved_attributes ⇒ Object
28
29
30
31
32
|
# File 'lib/zoop/object.rb', line 28
def unsaved_attributes
Hash[@unsaved_attributes.map do |key|
[ key, to_hash_value(self[key], :unsaved_attributes) ]
end]
end
|