arusarka-dynamic-active-resource 0.0.2 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/README +115 -4
- metadata +5 -4
data/History.txt
CHANGED
data/README
CHANGED
@@ -89,11 +89,122 @@ class Person < DynamicActiveResource::Base
|
|
89
89
|
end
|
90
90
|
|
91
91
|
The instance methods will be available as instance methods in the objects created, class methods
|
92
|
-
will be available as class methods in the class of the object.
|
92
|
+
will be available as class methods in the class of the object. Also active resource associations
|
93
|
+
are sometimes paginated. So even if you use find(:all), you get only the first few results. To
|
94
|
+
overcome this, include in your ClassMethods module a method called find_without_pagination. In that
|
95
|
+
method route it to acitve resource find with proper parameters. An example of the method implementation
|
96
|
+
is
|
97
|
+
|
98
|
+
def find_without_pagination(*args)
|
99
|
+
scope = args.slice!(0)
|
100
|
+
options = args.slice!(0) || {}
|
101
|
+
options[:params] ||= {}
|
102
|
+
options[:params].merge!({:page => 'all'})
|
103
|
+
# call ActiveResource::Base::find with proper options
|
104
|
+
find(scope, options)
|
105
|
+
end
|
106
|
+
|
107
|
+
The class level find method will automatically pick up this method if defined as a ClassMethod method
|
108
|
+
else it will pass it to find.
|
109
|
+
|
110
|
+
ASSOCIATIONS
|
111
|
+
------------
|
112
|
+
|
113
|
+
This gem also provides active record like associations(highly experimental). Right now it provides only two
|
114
|
+
associations - 1) has_many, 2) belongs_to.
|
115
|
+
|
116
|
+
1) has_many
|
117
|
+
-----------
|
118
|
+
|
119
|
+
Taking the example from above the way it should be done
|
120
|
+
|
121
|
+
class Person < DynamicActiveResource::Base
|
122
|
+
has_many :cars
|
123
|
+
|
124
|
+
module ClassMethods
|
125
|
+
def count
|
126
|
+
find(:all).size
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
module InstanceMethods
|
131
|
+
def occupation
|
132
|
+
return job if job
|
133
|
+
'Unemployed'
|
134
|
+
end
|
135
|
+
|
136
|
+
def resource_identifier
|
137
|
+
name()
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
Next a car class has to be initialized
|
143
|
+
|
144
|
+
class Car < DynARBase (alias for DynamicActiveResource::Base)
|
145
|
+
end
|
146
|
+
|
147
|
+
set resource options only at the top level( Person in this case)
|
148
|
+
|
149
|
+
Person.site = 'http://localhost:8080/'
|
150
|
+
Person.user = 'test'
|
151
|
+
Person.password = 'secret'
|
152
|
+
|
153
|
+
associations take care of setting the site in the children classes automatically
|
154
|
+
you will notice that an additional method resource_identifier() has been defined
|
155
|
+
in the parent class. It would be discussed shortly.
|
156
|
+
|
157
|
+
The way associations work is if you do something like
|
158
|
+
|
159
|
+
person = Person.find('asur').cars it will hit the url http://localhost:8080/people/asur/cars.xml
|
160
|
+
|
161
|
+
You would notice that its getting the cars for the person with name 'asur'. It does so because
|
162
|
+
in the Person class a method called resource_identifier has been defined which says that the id
|
163
|
+
is actually name instead of the database id. If the id attribute in the xml is set appropriately
|
164
|
+
(to name in this case) then you do not need to define the method.
|
165
|
+
|
166
|
+
2) belongs_to
|
167
|
+
-------------
|
168
|
+
|
169
|
+
Again, referring to the example above
|
170
|
+
|
171
|
+
class Person < DynamicActiveResource::Base
|
172
|
+
has_many :cars
|
173
|
+
|
174
|
+
module ClassMethods
|
175
|
+
def count
|
176
|
+
find(:all).size
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
module InstanceMethods
|
181
|
+
def occupation
|
182
|
+
return job if job
|
183
|
+
'Unemployed'
|
184
|
+
end
|
185
|
+
|
186
|
+
def resource_identifier
|
187
|
+
name()
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
Next a car class has to be initialized
|
193
|
+
|
194
|
+
class Car < DynARBase (alias for DynamicActiveResource::Base)
|
195
|
+
belongs_to :person
|
196
|
+
end
|
197
|
+
|
198
|
+
Right now belongs_to supports only assocation with a single class. After defining this you
|
199
|
+
automatically have a method 'person' available.
|
200
|
+
|
201
|
+
car = Car.find('WB1234I')
|
202
|
+
owner = car.person
|
93
203
|
|
94
204
|
== FEATURES/PROBLEMS:
|
95
205
|
|
96
|
-
|
206
|
+
Only site, element_name, collection_name, user, password attributes of ActiveResource::Base is managed
|
207
|
+
by the wrapper class.
|
97
208
|
|
98
209
|
== SYNOPSIS:
|
99
210
|
|
@@ -101,11 +212,11 @@ will be available as class methods in the class of the object.
|
|
101
212
|
|
102
213
|
== REQUIREMENTS:
|
103
214
|
|
104
|
-
|
215
|
+
requires active-resource
|
105
216
|
|
106
217
|
== INSTALL:
|
107
218
|
|
108
|
-
|
219
|
+
sudo gem install arusarka-dynamic-active-resource
|
109
220
|
|
110
221
|
== LICENSE:
|
111
222
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arusarka-dynamic-active-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- asur
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
-
description: A wrapper around active resource. Normally changing active resource attributes doesn't get reflected. So this gem allows to dynamically create active resource classes
|
25
|
+
description: A wrapper around active resource. Normally changing active resource attributes doesn't get reflected. So this gem allows to dynamically create active resource classes. This gem also provides very lightweight activerecord like associations.
|
26
26
|
email: arusarka@gmail.com
|
27
27
|
executables: []
|
28
28
|
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- History.txt
|
36
36
|
has_rdoc: false
|
37
37
|
homepage: http://github.com/arusarka/dynamic-active-resource/
|
38
|
+
licenses:
|
38
39
|
post_install_message:
|
39
40
|
rdoc_options: []
|
40
41
|
|
@@ -55,9 +56,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
requirements: []
|
56
57
|
|
57
58
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.
|
59
|
+
rubygems_version: 1.3.5
|
59
60
|
signing_key:
|
60
61
|
specification_version: 2
|
61
|
-
summary: Wrapper around active resource so that classes can be easily reused
|
62
|
+
summary: Wrapper around active resource so that classes can be easily reused.
|
62
63
|
test_files: []
|
63
64
|
|