Class: Socialcastr::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/socialcastr/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(arguments = {}) ⇒ Base
Returns a new instance of Base.
5
6
7
8
9
10
|
# File 'lib/socialcastr/base.rb', line 5
def initialize(arguments={})
@data = {}
arguments.map do |k,v|
@data[k.to_s] = v
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/socialcastr/base.rb', line 68
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)]
unless new?
if obj.class.ancestors.include? Socialcastr::Base
obj.add_container_id(self)
end
if obj.class == Array
obj.each do |o|
o.add_container_id(self)
end
end
end
return obj
end
end
|
Class Method Details
.all(*arguments) ⇒ Object
144
145
146
|
# File 'lib/socialcastr/base.rb', line 144
def all(*arguments)
find(:all, *arguments)
end
|
.api ⇒ Object
115
116
117
|
# File 'lib/socialcastr/base.rb', line 115
def api
@api ||= Socialcastr.api
end
|
.collection_name ⇒ Object
183
184
185
|
# File 'lib/socialcastr/base.rb', line 183
def collection_name
model_name.downcase + "s"
end
|
.collection_path(options = {}) ⇒ Object
160
161
162
|
# File 'lib/socialcastr/base.rb', line 160
def collection_path(options = {})
"#{prefix(options)}#{collection_name}"
end
|
.element_path(id, prefix_options = {}) ⇒ Object
156
157
158
|
# File 'lib/socialcastr/base.rb', line 156
def element_path(id, prefix_options = {})
"#{collection_path(prefix_options)}/#{URI.escape id.to_s}"
end
|
.find(*arguments) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/socialcastr/base.rb', line 119
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
138
139
140
141
142
|
# File 'lib/socialcastr/base.rb', line 138
def find_every(options)
(prefix_options, query_options) = parse_options(options)
path = collection_path(prefix_options)
parse_and_prefix(api.get(path, query_options), prefix_options)
end
|
.find_single(id, options) ⇒ Object
132
133
134
135
136
|
# File 'lib/socialcastr/base.rb', line 132
def find_single(id, options)
(prefix_options, query_options) = parse_options(options)
path = element_path(id, prefix_options)
parse_and_prefix(api.get(path, query_options), prefix_options)
end
|
.first(*arguments) ⇒ Object
148
149
150
|
# File 'lib/socialcastr/base.rb', line 148
def first(*arguments)
find(:first, *arguments)
end
|
.from_hash(h) ⇒ Object
111
112
113
|
# File 'lib/socialcastr/base.rb', line 111
def from_hash(h)
new.tap { |object| object.instance_variable_set("@data", h) }
end
|
.last(*arguments) ⇒ Object
152
153
154
|
# File 'lib/socialcastr/base.rb', line 152
def last(*arguments)
find(:last, *arguments)
end
|
.model_name ⇒ Object
179
180
181
|
# File 'lib/socialcastr/base.rb', line 179
def model_name
self.to_s.gsub(/^.*::/, '')
end
|
.parse(xml = "") ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/socialcastr/base.rb', line 92
def parse(xml="")
source = SAX::ActiveResource.new
Nokogiri::XML::SAX::Parser.new(source).parse(xml)
case source.data
when Hash
return from_hash(source.data)
else
return source.data
end
end
|
.parse_and_prefix(xml, prefix_options) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/socialcastr/base.rb', line 103
def parse_and_prefix(xml, prefix_options)
obj = parse(xml)
prefix_options.each_pair do |k,v|
obj.send("#{k}=", v)
end
return obj
end
|
.parse_options(options) ⇒ Object
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/socialcastr/base.rb', line 168
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
164
165
166
|
# File 'lib/socialcastr/base.rb', line 164
def prefix(options)
options.map { |k,v| k.to_s.gsub("_id", 's') + "/" + v.to_s }.join("/") + "/"
end
|
Instance Method Details
#add_container_id(container) ⇒ Object
87
88
89
|
# File 'lib/socialcastr/base.rb', line 87
def add_container_id(container)
self.send("#{container.class.model_name.downcase}_id=", container.id)
end
|
#api ⇒ Object
36
37
38
|
# File 'lib/socialcastr/base.rb', line 36
def api
self.class.api
end
|
#collection_path(options = {}) ⇒ Object
44
45
46
|
# File 'lib/socialcastr/base.rb', line 44
def collection_path(options={})
self.class.collection_path(options)
end
|
#copy_attributes_from_object(object = nil) ⇒ Object
28
29
30
|
# File 'lib/socialcastr/base.rb', line 28
def copy_attributes_from_object(object=nil)
@data = object.instance_variable_get("@data")
end
|
#create ⇒ Object
16
17
18
19
20
|
# File 'lib/socialcastr/base.rb', line 16
def create
api.post(collection_path, to_params).tap do |xml|
copy_attributes_from_object(self.class.parse(xml))
end
end
|
#element_path(prefix_options = {}) ⇒ Object
40
41
42
|
# File 'lib/socialcastr/base.rb', line 40
def element_path(prefix_options={})
self.class.element_path(self.id, prefix_options)
end
|
#id ⇒ Object
64
65
66
|
# File 'lib/socialcastr/base.rb', line 64
def id
return @data["id"]
end
|
#method_name(s) ⇒ Object
60
61
62
|
# File 'lib/socialcastr/base.rb', line 60
def method_name(s)
s.to_s.gsub("_","-")
end
|
#new? ⇒ Boolean
32
33
34
|
# File 'lib/socialcastr/base.rb', line 32
def new?
id.nil?
end
|
#param_name(variable_name) ⇒ Object
56
57
58
|
# File 'lib/socialcastr/base.rb', line 56
def param_name(variable_name)
"#{self.class.model_name.downcase}[#{variable_name}]"
end
|
#save ⇒ Object
12
13
14
|
# File 'lib/socialcastr/base.rb', line 12
def save
new? ? create : update
end
|
#to_params ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/socialcastr/base.rb', line 48
def to_params
params = {}
@data.each_pair do |k,v|
params[param_name(k)] = v
end
params
end
|
#update ⇒ Object
22
23
24
25
26
|
# File 'lib/socialcastr/base.rb', line 22
def update
api.put(element_path, to_params).tap do |xml|
copy_attributes_from_object(self.class.parse(xml))
end
end
|