Class: WeThePeople::Resource
- Inherits:
-
Object
- Object
- WeThePeople::Resource
show all
- Defined in:
- lib/we_the_people/resource.rb
Constant Summary
collapse
- COERCERS =
{
Integer => lambda {|v| Integer(v) },
Time => lambda {|v| Time.at(v) }
}
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs, parent = nil) ⇒ Resource
Returns a new instance of Resource.
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/we_the_people/resource.rb', line 134
def initialize(attrs, parent = nil)
@parent = parent
result = {}
attrs.each_pair do |key, value|
result[key.to_s.gsub(/\s/, '_')] = value
end
attrs = result.select {|a| self.class.attributes.include?(a)}
self.class.embedded_attributes.each do |embedded_key|
attrs[embedded_key] = "WeThePeople::Resources::#{embedded_key.classify}".constantize.new(attrs[embedded_key]) if !attrs[embedded_key].nil? && !attrs[embedded_key].empty?
end if self.class.embedded_attributes
self.class.embedded_array_attributes.each do |embedded_array_key|
if attrs[embedded_array_key].is_a?(Array)
attrs[embedded_array_key] = attrs[embedded_array_key].dup
attrs[embedded_array_key].map! do |embedded_array_element|
"WeThePeople::Resources::#{embedded_array_key.classify}".constantize.new(embedded_array_element)
end
end
end if self.class.embedded_array_attributes
@attributes = attrs
end
|
Class Attribute Details
.attributes ⇒ Object
Returns the value of attribute attributes.
4
5
6
|
# File 'lib/we_the_people/resource.rb', line 4
def attributes
@attributes
end
|
.embedded_array_attributes ⇒ Object
Returns the value of attribute embedded_array_attributes.
4
5
6
|
# File 'lib/we_the_people/resource.rb', line 4
def embedded_array_attributes
@embedded_array_attributes
end
|
.embedded_attributes ⇒ Object
Returns the value of attribute embedded_attributes.
4
5
6
|
# File 'lib/we_the_people/resource.rb', line 4
def embedded_attributes
@embedded_attributes
end
|
Class Method Details
.add_attribute_key(key) ⇒ Object
58
59
60
61
|
# File 'lib/we_the_people/resource.rb', line 58
def add_attribute_key(key)
@attributes ||= []
@attributes << key.to_s
end
|
.all(parent = nil, criteria = {}) ⇒ Object
42
43
44
|
# File 'lib/we_the_people/resource.rb', line 42
def all(parent = nil, criteria = {})
cursor(parent, criteria).all
end
|
.attribute(name, coerce_to = nil, key_name = nil) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/we_the_people/resource.rb', line 72
def attribute(name, coerce_to = nil, key_name = nil)
name = name.to_s
key_name ||= name
add_attribute_key(key_name)
define_method "#{name}=" do |val|
val = self.class.coerce_value(val, coerce_to) if coerce_to
@attributes[key_name] = val
end
define_method name do
coerce_to ? self.class.coerce_value(@attributes[key_name], coerce_to) : @attributes[key_name]
end
end
|
.bare_name ⇒ Object
6
7
8
|
# File 'lib/we_the_people/resource.rb', line 6
def bare_name
name.split("::").last
end
|
.belongs_to(klass_name) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/we_the_people/resource.rb', line 50
def belongs_to(klass_name)
@belongs_to = klass_name
define_method klass_name.to_s.downcase do
@parent
end
end
|
.build_index_url(parent = nil, criteria = {}) ⇒ Object
46
47
48
|
# File 'lib/we_the_people/resource.rb', line 46
def build_index_url(parent = nil, criteria = {})
"#{WeThePeople::Config.host}/#{path(parent)}.json"
end
|
.build_resource_url(id, parent = nil) ⇒ Object
27
28
29
|
# File 'lib/we_the_people/resource.rb', line 27
def build_resource_url(id, parent = nil)
"#{WeThePeople::Config.host}/#{path(parent)}/#{id}.json"
end
|
.coerce_value(val, klass) ⇒ Object
68
69
70
|
# File 'lib/we_the_people/resource.rb', line 68
def coerce_value(val, klass)
COERCERS[klass].call(val)
end
|
.cursor(parent = nil, criteria = {}) ⇒ Object
38
39
40
|
# File 'lib/we_the_people/resource.rb', line 38
def cursor(parent = nil, criteria = {})
Collection.new(self, criteria, fetch(parent, criteria), parent)
end
|
.fetch(parent = nil, criteria = {}) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/we_the_people/resource.rb', line 31
def fetch(parent = nil, criteria = {})
raise "Must be called by parent." if @belongs_to && parent.nil?
body = WeThePeople::Config.client.get(build_index_url(parent, criteria), :params => criteria.merge(WeThePeople::Config.default_params)).to_s
WeThePeople::Config.json.parse(body)
end
|
.find(id, parent = nil) ⇒ Object
10
11
12
13
14
15
|
# File 'lib/we_the_people/resource.rb', line 10
def find(id, parent = nil)
raise "Must be called by parent." if @belongs_to && parent.nil?
json = WeThePeople::Config.client.get(build_resource_url(id, parent), :params => WeThePeople::Config.default_params).to_s
new(WeThePeople::Config.json.parse(json)['results'].first)
end
|
.has_embedded(klass_name) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/we_the_people/resource.rb', line 87
def has_embedded(klass_name)
name = klass_name.to_s.singularize
add_attribute_key(name)
@embedded_attributes ||= []
@embedded_attributes << name
define_method "#{name}=" do |val|
val = klass.new(val)
@attributes[name] = val
end
define_method name do
@attributes[name]
end
end
|
.has_many(klass_name) ⇒ Object
123
124
125
126
127
128
129
130
131
|
# File 'lib/we_the_people/resource.rb', line 123
def has_many(klass_name)
name = klass_name.to_s.pluralize
add_attribute_key(name)
define_method name do
AssociationProxy.new(self, "WeThePeople::Resources::#{name.classify}".constantize)
end
end
|
.has_many_embedded(klass_name) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/we_the_people/resource.rb', line 104
def has_many_embedded(klass_name)
name = klass_name.to_s.pluralize
add_attribute_key(name)
@embedded_array_attributes ||= []
@embedded_array_attributes << name
define_method "#{name}=" do |val|
raise TypeError unless val.is_a?(Array)
val = val.map {|o| klass.new(o)}
@attributes[name] = val
end
define_method name do
@attributes[name]
end
end
|
.path(parent = nil) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/we_the_people/resource.rb', line 17
def path(parent = nil)
raise "Must be called by parent." if @belongs_to && parent.nil?
if parent
"#{parent.path}/#{bare_name.underscore.pluralize}"
else
"#{bare_name.underscore.pluralize}"
end
end
|
Instance Method Details
#path ⇒ Object
161
162
163
|
# File 'lib/we_the_people/resource.rb', line 161
def path
"#{self.class.path}/#{id}"
end
|
#to_json ⇒ Object
165
166
167
|
# File 'lib/we_the_people/resource.rb', line 165
def to_json
@attributes.to_json
end
|