Class: Socialcastr::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/socialcastr/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(arguments = {}, prefix_options = {}) ⇒ Base
Returns a new instance of Base.
6
7
8
9
10
11
12
|
# File 'lib/socialcastr/base.rb', line 6
def initialize(arguments={}, prefix_options={})
@data = {}
arguments.each_pair do |k,v|
@data[k.to_s] = v
end
@prefix_options = prefix_options
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/socialcastr/base.rb', line 87
def method_missing(method, *args, &block)
if method.to_s =~ /=$/
@data[method_name(method.to_s.sub(/=$/,''))] = args.first
else
obj = @data[method_name(method)]
return nil unless obj
unless new?
self.class.set_prefix_options(obj, to_prefix_options)
end
return obj
end
end
|
Instance Attribute Details
#prefix_options ⇒ Object
Returns the value of attribute prefix_options.
5
6
7
|
# File 'lib/socialcastr/base.rb', line 5
def prefix_options
@prefix_options
end
|
Class Method Details
.all(*arguments) ⇒ Object
151
152
153
|
# File 'lib/socialcastr/base.rb', line 151
def all(*arguments)
find(:all, *arguments)
end
|
.api ⇒ Object
123
124
125
|
# File 'lib/socialcastr/base.rb', line 123
def api
Socialcastr.api
end
|
.collection_name ⇒ Object
190
191
192
|
# File 'lib/socialcastr/base.rb', line 190
def collection_name
model_name.downcase + "s"
end
|
.collection_path(options = {}) ⇒ Object
167
168
169
|
# File 'lib/socialcastr/base.rb', line 167
def collection_path(options = {})
"#{prefix(options)}#{collection_name}"
end
|
.element_path(id, prefix_options = {}) ⇒ Object
163
164
165
|
# File 'lib/socialcastr/base.rb', line 163
def element_path(id, prefix_options = {})
"#{collection_path(prefix_options)}/#{URI.escape id.to_s}"
end
|
.find(*arguments) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/socialcastr/base.rb', line 127
def find(*arguments)
args = arguments.dup
scope = args.slice!(0)
options = args.slice!(0) || {}
case scope
when :all then find_every(options)
when :first then find_every(options).first
when :last then find_every(options).to_a.last
else find_single(scope, options)
end
end
|
.find_every(options) ⇒ Object
145
146
147
148
149
|
# File 'lib/socialcastr/base.rb', line 145
def find_every(options)
(prefix_options, query_options) = parse_options(options)
path = collection_path(prefix_options)
parse(api.get(path, query_options), prefix_options)
end
|
.find_single(id, options) ⇒ Object
139
140
141
142
143
|
# File 'lib/socialcastr/base.rb', line 139
def find_single(id, options)
(prefix_options, query_options) = parse_options(options)
path = element_path(id, prefix_options)
parse(api.get(path, query_options), prefix_options)
end
|
.first(*arguments) ⇒ Object
155
156
157
|
# File 'lib/socialcastr/base.rb', line 155
def first(*arguments)
find(:first, *arguments)
end
|
.from_hash(h) ⇒ Object
119
120
121
|
# File 'lib/socialcastr/base.rb', line 119
def from_hash(h)
new.tap { |object| object.instance_variable_set("@data", h) }
end
|
.last(*arguments) ⇒ Object
159
160
161
|
# File 'lib/socialcastr/base.rb', line 159
def last(*arguments)
find(:last, *arguments)
end
|
.model_name ⇒ Object
186
187
188
|
# File 'lib/socialcastr/base.rb', line 186
def model_name
self.to_s.gsub(/^.*::/, '')
end
|
.parse(xml = "", prefix_options = {}) ⇒ Object
101
102
103
104
105
|
# File 'lib/socialcastr/base.rb', line 101
def parse(xml="", prefix_options={})
resource = SAX::ActiveResource.new
Nokogiri::XML::SAX::Parser.new(resource).parse(xml)
return set_prefix_options(resource.data, prefix_options)
end
|
.parse_options(options) ⇒ Object
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/socialcastr/base.rb', line 175
def parse_options(options)
prefix_options = {}
options.each_pair do |k,v|
if k.to_s.match(/_id$/)
prefix_options[k] = v
options.delete(k)
end
end
return [prefix_options, options]
end
|
.prefix(options) ⇒ Object
171
172
173
|
# File 'lib/socialcastr/base.rb', line 171
def prefix(options)
options.map { |k,v| k.to_s.gsub("_id", 's') + "/" + v.to_s }.join("/") + "/"
end
|
.set_prefix_options(obj, prefix_options = {}) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/socialcastr/base.rb', line 107
def set_prefix_options(obj, prefix_options={})
if obj.class.ancestors.include? Socialcastr::Base
obj.prefix_options = prefix_options
return obj
end
if obj.class == Array
obj.each do |o|
set_prefix_options(o, prefix_options)
end
end
end
|
Instance Method Details
#api ⇒ Object
51
52
53
|
# File 'lib/socialcastr/base.rb', line 51
def api
@api ||= self.class.api
end
|
#collection_path ⇒ Object
59
60
61
|
# File 'lib/socialcastr/base.rb', line 59
def collection_path
self.class.collection_path(@prefix_options)
end
|
#copy_attributes_from_object(object = nil) ⇒ Object
42
43
44
45
|
# File 'lib/socialcastr/base.rb', line 42
def copy_attributes_from_object(object=nil)
@data = object.instance_variable_get("@data")
return self
end
|
#create ⇒ Object
18
19
20
21
22
|
# File 'lib/socialcastr/base.rb', line 18
def create
api.post(collection_path, to_params).tap do |xml|
copy_attributes_from_object(self.class.parse(xml))
end
end
|
#destroy ⇒ Object
37
38
39
40
|
# File 'lib/socialcastr/base.rb', line 37
def destroy
api.delete(element_path)
return self
end
|
#element_path ⇒ Object
55
56
57
|
# File 'lib/socialcastr/base.rb', line 55
def element_path
self.class.element_path(self.id, @prefix_options)
end
|
#id ⇒ Object
83
84
85
|
# File 'lib/socialcastr/base.rb', line 83
def id
return @data["id"]
end
|
#method_name(s) ⇒ Object
79
80
81
|
# File 'lib/socialcastr/base.rb', line 79
def method_name(s)
s.to_s.gsub("_","-")
end
|
#new? ⇒ Boolean
47
48
49
|
# File 'lib/socialcastr/base.rb', line 47
def new?
id.nil?
end
|
#param_name(variable_name) ⇒ Object
75
76
77
|
# File 'lib/socialcastr/base.rb', line 75
def param_name(variable_name)
"#{self.class.model_name.downcase}[#{variable_name}]"
end
|
#refresh ⇒ Object
30
31
32
33
34
35
|
# File 'lib/socialcastr/base.rb', line 30
def refresh
api.get(element_path).tap do |xml|
copy_attributes_from_object(self.class.parse(xml))
end
return self
end
|
#save ⇒ Object
14
15
16
|
# File 'lib/socialcastr/base.rb', line 14
def save
new? ? create : update
end
|
#to_params ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/socialcastr/base.rb', line 63
def to_params
params = {}
@data.each_pair do |k,v|
params[param_name(k)] = v
end
params
end
|
#to_prefix_options ⇒ Object
71
72
73
|
# File 'lib/socialcastr/base.rb', line 71
def to_prefix_options
@prefix_options.merge "#{self.class.model_name.downcase}_id" => id
end
|
#update ⇒ Object
24
25
26
27
28
|
# File 'lib/socialcastr/base.rb', line 24
def update
api.put(element_path, to_params).tap do |xml|
copy_attributes_from_object(self.class.parse(xml))
end
end
|