Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 859 lines (769 sloc) 22.278 kb
e8b4f3b @sam Importing from old repository.
sam authored
1 module DataMapper
2 module Resource
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
3 include Extlib::Assertions
ecb4765 Made Resource#save chainable for easier extending in plugins
Dan Kubb authored
4 extend Chainable
d41858a @dkubb Added DM::Deprecate::deprecate() to mark methods as deprecated
dkubb authored
5 extend Deprecate
6
7 deprecate :new_record?, :new?
182e590 Minor code cleanup
Dan Kubb authored
8
27c926a @dkubb Stubbed out @api and other docs for several classes
dkubb authored
9 # @deprecated
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
10 def self.append_inclusions(*inclusions)
36518c9 @dkubb Minor documentation update
dkubb authored
11 warn 'DataMapper::Resource.append_inclusions is deprecated, use DataMapper::Model.append_inclusions instead'
76a7974 Minor code reformatting
Dan Kubb authored
12 Model.append_inclusions(*inclusions)
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
13 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored
14
27c926a @dkubb Stubbed out @api and other docs for several classes
dkubb authored
15 # @deprecated
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
16 def self.extra_inclusions
36518c9 @dkubb Minor documentation update
dkubb authored
17 warn 'DataMapper::Resource.extra_inclusions is deprecated, use DataMapper::Model.extra_inclusions instead'
76a7974 Minor code reformatting
Dan Kubb authored
18 Model.extra_inclusions
19 end
20
27c926a @dkubb Stubbed out @api and other docs for several classes
dkubb authored
21 # @deprecated
76a7974 Minor code reformatting
Dan Kubb authored
22 def self.descendants
36518c9 @dkubb Minor documentation update
dkubb authored
23 warn 'DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead'
76a7974 Minor code reformatting
Dan Kubb authored
24 DataMapper::Model.descendants
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
25 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored
26
fb68f67 Moved logic from Resource.included into Model.extended
Dan Kubb authored
27 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
28 # Deprecated API for updating attributes and saving Resource
29 #
30 # @see #update
31 #
32 # @api public
33 def update_attributes(attributes = {}, *allowed)
34 warn "#{model}#update_attributes is deprecated, use #{model}#update instead"
35
36 if allowed.any?
37 warn "specifying allowed in #{model}#update_attributes is deprecated, " \
38 'use Hash#only to filter the attributes in the caller'
39 attributes = attributes.only(*allowed)
40 end
41
42 update(attributes)
43 end
44
45 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
46 # Makes sure a class gets all the methods when it includes Resource
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
47 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
48 # @api private
74ef620 Modules now push themselves into Resource rather than it pulling them
Dan Kubb authored
49 def self.included(model)
e41f6ab Renamed Resource::ClassMethods to Model
Dan Kubb authored
50 model.extend Model
748b8d9 Enabled DataMapper::Resource to know what classes have included it.
Martin Kihlgren authored
51 end
52
1b0f5a7 @michaelklishin A note on how Resource#collection is used
michaelklishin authored
53 # Collection this resource associated with.
54 # Used by SEL.
55 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
56 # @api private
d294bac Updated Adapter API
Dan Kubb authored
57 attr_writer :collection
58
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
59 # TODO: document
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
60 # @api public
7361467 @michaelklishin Use alias_method so Emacs' ruby-mode don't consider 'class' a new ind…
michaelklishin authored
61 alias_method :model, :class
6db24c7 Small performance optimizations
Dan Kubb authored
62
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
63 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
64 # Repository this resource belongs to in the context of this collection
65 # or of the resource's class.
66 #
67 # @return [Repository]
68 # the respository this resource belongs to, in the context of
69 # a collection OR in the instance's Model's context
70 #
71 # @api semipublic
72 def repository
73 # only set @repository explicitly when persisted
74 defined?(@repository) ? @repository : model.repository
75 end
76
77 ##
78 # Retrieve the key(s) for this resource.
79 #
80 # This always returns the persisted key value,
81 # even if the key is changed and not yet persisted.
82 # This is done so all relations still work.
83 #
84 # @return [Array(Key)]
85 # the key(s) identifying this resource
86 #
87 # @api public
88 def key
89 return @key if defined?(@key)
90
91 key = model.key(repository_name).map do |property|
92 original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil)
93 end
94
95 # set the key if every entry is non-nil
96 @key = key if key.all?
97 end
98
99 ##
100 # Checks if this Resource instance is new
101 #
102 # @return [TrueClass, FalseClass]
103 # true if the resource is new and not saved
104 #
105 # @api public
106 def new?
107 !saved?
108 end
109
110 ##
111 # Checks if this Resource instance is saved
112 #
113 # @return [TrueClass, FalseClass]
114 # true if the resource has been saved
115 #
116 # @api public
117 def saved?
118 @saved == true
119 end
120
121 ##
122 # Checks if the resource has unsaved changes
123 #
124 # @return
125 # [TrueClass, FalseClass] true if resource is new or has any unsaved changes
126 #
127 # @api semipublic
128 def dirty?
129 if dirty_attributes.any?
130 true
131 elsif new?
132 model.identity_field || properties.any? { |p| p.default? }
133 else
134 false
135 end
136 end
137
138 ##
6186835 Stripped whitespace
Dan Kubb authored
139 # Returns the value of the attribute.
140 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
141 # Do not read from instance variables directly, but use this method.
142 # This method handles lazy loading the attribute and returning of
143 # defaults if nessesary.
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
144 #
145 # Class Foo
146 # include DataMapper::Resource
147 #
148 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored
149 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
150 #
151 # def full_name
152 # "#{attribute_get(:first_name)} #{attribute_get(:last_name)}"
153 # end
154 #
155 # # using the shorter syntax
156 # def name_for_address_book
157 # "#{last_name}, #{first_name}"
158 # end
159 # end
160 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
161 # @param [Symbol] name
162 # name of attribute to retrieve
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
163 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
164 # @return [Object]
165 # the value stored at that given attribute
166 # (nil if none, and default if necessary)
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
167 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
168 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
169 def attribute_get(name)
84880f2 Simplified Resource#attribute_get
Dan Kubb authored
170 properties[name].get(self)
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
171 end
172
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
Dan Kubb authored
173 alias [] attribute_get
174
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
175 ##
176 # Sets the value of the attribute and marks the attribute as dirty
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
177 # if it has been changed so that it may be saved. Do not set from
178 # instance variables directly, but use this method. This method
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
179 # handles the lazy loading the property and returning of defaults
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
180 # if nessesary.
181 #
182 # Class Foo
183 # include DataMapper::Resource
184 #
185 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored
186 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
187 #
188 # def full_name(name)
189 # name = name.split(' ')
190 # attribute_set(:first_name, name[0])
191 # attribute_set(:last_name, name[1])
192 # end
193 #
194 # # using the shorter syntax
195 # def name_from_address_book(name)
196 # name = name.split(', ')
197 # first_name = name[1]
198 # last_name = name[0]
199 # end
200 # end
201 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
202 # @param [Symbol] name
203 # name of attribute to set
204 # @param [Object] value
205 # value to store
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
206 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
207 # @return [Object]
208 # the value stored at that given attribute, nil if none,
209 # and default if necessary
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
210 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
211 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
212 def attribute_set(name, value)
213 properties[name].set(self, value)
214 end
215
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
Dan Kubb authored
216 alias []= attribute_set
217
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
218 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
219 # Gets all the attributes of the Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
220 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
221 # @param [Symbol] key_on
222 # Use this attribute of the Property as keys.
223 # defaults to :name. :field is useful for adapters
224 # :property or nil use the actual Property object.
78daf51 @advany add docs to DataMapper::Resource
advany authored
225 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
226 # @return [Hash]
227 # All the attributes
78daf51 @advany add docs to DataMapper::Resource
advany authored
228 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
229 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
230 def attributes(key_on = :name)
231 attributes = {}
232 properties.each do |property|
233 if model.public_method_defined?(name = property.name)
234 key = case key_on
235 when :name then name
236 when :field then property.field
237 else property
238 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
239
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
240 attributes[key] = send(name)
241 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
242 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
243 attributes
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
244 end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
245
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
246 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
247 # Assign values to multiple attributes in one call (mass assignment)
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
248 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
249 # @param [Hash] attributes
250 # names and values of attributes to assign
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
251 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
252 # @return [Hash]
253 # names and values of attributes assigned
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
254 #
255 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
256 def attributes=(attributes)
257 # TODO: update to accept Property and Relationship objects as keys
258 attributes.each do |name, value|
259 case name
260 when String, Symbol
261 if model.public_method_defined?(setter = "#{name}=")
262 send(setter, value)
263 else
264 raise ArgumentError, "The property '#{name}' is not accessible in #{model}"
265 end
266 when Associations::Relationship, Property
267 name.set(self, value)
268 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
269 end
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
270 end
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
271
238682e Added Resource#<=>
Dan Kubb authored
272 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
273 # Reloads association and all child association
238682e Added Resource#<=>
Dan Kubb authored
274 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
275 # @return [Resource]
276 # the receiver, the current Resource instance
238682e Added Resource#<=>
Dan Kubb authored
277 #
278 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
279 def reload
280 if saved?
281 reload_attributes(loaded_properties)
282 child_associations.each { |a| a.reload }
238682e Added Resource#<=>
Dan Kubb authored
283 end
284
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
285 self
7b6845d Minor code refactoring
Dan Kubb authored
286 end
287
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
288 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
289 # Updates attributes and saves this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
290 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
291 # @param [Hash] attributes
292 # attributes to be updated
6186835 Stripped whitespace
Dan Kubb authored
293 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
294 # @return [TrueClass, FalseClass]
295 # true if resource and storage state match
78daf51 @advany add docs to DataMapper::Resource
advany authored
296 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
297 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
298 def update(attributes = {})
299 self.attributes = attributes
300 _update
d409c1f @sam Added and specced LoadedSet.
sam authored
301 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
302
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
303 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
304 # Updates attributes and saves this Resource instance, bypassing hooks
6186835 Stripped whitespace
Dan Kubb authored
305 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
306 # @param [Hash] attributes
307 # attributes to be updated
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored
308 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
309 # @return [TrueClass, FalseClass]
310 # true if resource and storage state match
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored
311 #
312 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
313 def update!(attributes = {})
314 self.attributes = attributes
315 _update
a519fb4 @sam CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
sam authored
316 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
317
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
318 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
319 # Save the instance and associated children to the data-store.
78daf51 @advany add docs to DataMapper::Resource
advany authored
320 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
321 # This saves all children in a has n relationship (if they're dirty).
78daf51 @advany add docs to DataMapper::Resource
advany authored
322 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
323 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
324 # true if Resource instance and all associations were saved
78daf51 @advany add docs to DataMapper::Resource
advany authored
325 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
326 # @see Repository#save
78daf51 @advany add docs to DataMapper::Resource
advany authored
327 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
328 # @api public
329 chainable do
330 def save
331 _save
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
332 end
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored
333 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
334
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
335 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
336 # Save the instance and associated children to the data-store, bypassing hooks
78daf51 @advany add docs to DataMapper::Resource
advany authored
337 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
338 # This saves all children in a has n relationship (if they're dirty).
78daf51 @advany add docs to DataMapper::Resource
advany authored
339 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
340 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
341 # true if Resource instance and all associations were saved
78daf51 @advany add docs to DataMapper::Resource
advany authored
342 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
343 # @see Repository#save
78daf51 @advany add docs to DataMapper::Resource
advany authored
344 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
345 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
346 chainable do
347 def save!
348 _save
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored
349 end
ece0d18 @somebee some small convenient changes. added possibility for iterating throug…
somebee authored
350 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
351
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
352 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
353 # Destroy the instance, remove it from the repository
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
354 #
355 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
356 # true if resource was destroyed
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
357 #
358 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
359 def destroy
360 _destroy
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
361 end
362
363 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
364 # Destroy the instance, remove it from the repository, bypassing hooks
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
365 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
366 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
367 # true if resource was destroyed
805e402 @myabc YARD conversion for dm-core
myabc authored
368 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
369 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
370 def destroy!
371 _destroy
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made …
Bauerpauer authored
372 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
373
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
374 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
375 # Compares another Resource for equality
40fcf73 Stripped whitespace
Dan Kubb authored
376 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
377 # Resource is equal to +other+ if they are the same object (identity)
378 # or if they are both of the *same model* and all of their attributes
379 # are equivalent
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
380 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
381 # @param [Resource] other
382 # the other Resource to compare with
6186835 Stripped whitespace
Dan Kubb authored
383 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
384 # @return [TrueClass, FalseClass]
385 # true if they are equal, false if not
78daf51 @advany add docs to DataMapper::Resource
advany authored
386 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
387 # @api public
388 def eql?(other)
389 if equal?(other)
390 return true
391 end
392
393 unless self.class.equal?(other.class)
394 return false
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
395 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
396
397 cmp?(other, :eql?)
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
398 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored
399
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
400 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
401 # Compares another Resource for equivalency
6186835 Stripped whitespace
Dan Kubb authored
402 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
403 # Resource is equal to +other+ if they are the same object (identity)
404 # or if they are both of the *same base model* and all of their attributes
405 # are equivalent
406 #
407 # @param [Resource] other
408 # the other Resource to compare with
409 #
410 # @return [TrueClass, FalseClass]
411 # true if they are equivalent, false if not
6186835 Stripped whitespace
Dan Kubb authored
412 #
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
413 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
414 def ==(other)
415 if equal?(other)
416 return true
417 end
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored
418
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
419 unless other.respond_to?(:model) && model.base_model.equal?(other.model.base_model)
420 return false
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored
421 end
422
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
423 cmp?(other, :==)
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
424 end
425
426 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
427 # Compares two Resources to allow them to be sorted
72db385 @advany update_attributes should save model
advany authored
428 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
429 # @param [Resource] other
430 # The other Resource to compare with
78daf51 @advany add docs to DataMapper::Resource
advany authored
431 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
432 # @return [Integer]
433 # Return 0 if Resources should be sorted as the same, -1 if the
434 # other Resource should be after self, and 1 if the other Resource
435 # should be before self
72db385 @advany update_attributes should save model
advany authored
436 #
8708119 @myabc Convert docs to YARD-style
myabc authored
437 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
438 def <=>(other)
439 unless other.kind_of?(model)
440 raise ArgumentError, "Cannot compare a #{other.model} instance with a #{model} instance"
441 end
442 cmp = 0
443 model.default_order(repository_name).map do |i|
444 cmp = i.property.get!(self) <=> i.property.get!(other)
445 cmp *= -1 if i.direction == :desc
446 break if cmp != 0
447 end
448 cmp
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
449 end
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
450
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
451 # Returns hash value of the object.
452 # Two objects with the same hash value assumed equal (using eql? method)
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
453 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
454 # DataMapper resources are equal when their models have the same hash
455 # and they have the same set of properties
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
456 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
457 # When used as key in a Hash or Hash subclass, objects are compared
458 # by eql? and thus hash value has direct effect on lookup
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
459 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
460 # @api private
461 def hash
462 key.hash
1019ac3 @sam Added key validation to Resources, and added Resource#update_attributes.
sam authored
463 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
464
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
465 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
466 # Get a Human-readable representation of this Resource instance
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
467 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
468 # Foo.new #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
469 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
470 # @return [String]
471 # Human-readable representation of this Resource instance
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
472 #
473 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
474 def inspect
475 # TODO: display relationship values
476 attrs = properties.map do |property|
477 value = if new? || property.loaded?(self)
478 property.get!(self).inspect
479 else
480 '<not loaded>'
481 end
482
483 "#{property.instance_variable_name}=#{value}"
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
484 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
485
486 "#<#{model.name} #{attrs.join(' ')}>"
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
487 end
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
488
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
489 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
490 # Hash of original values of attributes that have unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
491 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
492 # @return [Hash]
493 # original values of attributes that have unsaved changes
494 #
495 # @api semipublic
496 def original_attributes
497 @original_attributes ||= {}
498 end
499
500 ##
501 # Checks if an attribute has been loaded from the repository
502 #
503 # class Foo
504 # include DataMapper::Resource
505 #
506 # property :name, String
507 # property :description, Text, :lazy => false
508 # end
509 #
510 # Foo.new.attribute_loaded?(:description) #=> false
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
511 #
512 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
513 # true if ivar +name+ has been loaded
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
514 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
515 # @return [TrueClass, FalseClass] true if ivar +name+ has been loaded
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
516 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
517 # @api private
518 def attribute_loaded?(name)
519 properties[name].loaded?(self)
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
520 end
521
522 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
523 # Fetches all the names of the attributes that have been loaded,
524 # even if they are lazy but have been called
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
525 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
526 # class Foo
527 # include DataMapper::Resource
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
528 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
529 # property :name, String
530 # property :description, Text, :lazy => false
531 # end
532 #
533 # Foo.new.loaded_properties #=> [ #<Property @model=Foo @name=:name> ]
534 #
535 # @return [Array(Property)]
536 # names of attributes that have been loaded
537 #
538 # @api private
539 def loaded_properties
540 properties.select { |p| p.loaded?(self) }
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
541 end
542
543 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
544 # Checks if an attribute has unsaved changes
545 #
546 # @param [Symbol] name
547 # name of attribute to check for unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
548 #
549 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
550 # true if attribute has unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
551 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
552 # @api semipublic
553 def attribute_dirty?(name)
554 dirty_attributes.key?(properties[name])
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
555 end
556
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
557 ##
558 # Hash of attributes that have unsaved changes
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
559 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
560 # @return [Hash]
561 # attributes that have unsaved changes
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
562 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
563 # @api semipublic
564 def dirty_attributes
565 dirty_attributes = {}
566
567 original_attributes.each_key do |property|
568 dirty_attributes[property] = property.value(property.get!(self))
569 end
570
571 dirty_attributes
e3ad4a2 Improved the transactional system greatly and introduced two-phase
Martin Kihlgren authored
572 end
573
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
574 ##
5e7d549 @michaelklishin Improve documentation of DataMapper::Resource#reset
michaelklishin authored
575 # Reset the Resource to a similar state as a new record:
576 # removes it from identity map and clears original property
577 # values (thus making all properties non dirty)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
578 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
579 # @api private
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
580 def reset
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
581 @saved = false
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
582 identity_map.delete(key)
8ca9812 @dkubb Renamed Resource#original_values as Resource#original_attributes
dkubb authored
583 original_attributes.clear
3684f20 Minor Resource cleanup
Dan Kubb authored
584 end
585
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
586 # Gets a Collection with the current Resource instance as its only member
587 #
588 # @return [Collection, FalseClass]
589 # nil if this is a new record,
590 # otherwise a Collection with self as its only member
591 #
592 # @api private
593 def collection
594 return @collection if @collection || new? || frozen?
c115888 @dkubb Changed Resource#to_query to #query to match conventions in Model and…
dkubb authored
595 @collection = Collection.new(query, [ self ])
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
596 end
597
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
598 protected
ec84ce4 @myabc Stripped whitespace with "sake strip"
myabc authored
599
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
600 ##
601 # Saves this Resource instance to the repository,
602 # setting default values for any unset properties
603 #
fb1d585 @michaelklishin Improve documentation of DataMapper::Resouce#_create
michaelklishin authored
604 # If resource is not dirty or a new (not yet saved),
605 # this method returns false
606 #
607 # On successful save identity map of the repository is
608 # updated
609 #
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
610 # Needs to be a protected method so that it is hookable
6186835 Stripped whitespace
Dan Kubb authored
611 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
612 # @return [TrueClass, FalseClass]
613 # true if the receiver was successfully created
6186835 Stripped whitespace
Dan Kubb authored
614 #
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored
615 # @api private
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
616 def _create
3558f1d @dbussink Fix that saving a non-dirty object returns true
dbussink authored
617 # Can't create a resource that is not dirty and doesn't have serial keys
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
618 if new? && !dirty?
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
619 return false
620 end
8d88b6f Updated Resource#save to return false if changing the key to nil
Dan Kubb authored
621
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
622 # set defaults for new resource
3684f20 Minor Resource cleanup
Dan Kubb authored
623 properties.each do |property|
05bae27 Refactoring to improve spec coverage
Dan Kubb authored
624 unless property.serial? || property.loaded?(self)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
625 property.set(self, property.default_for(self))
626 end
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
627 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
628
fdaa224 @paul Updated Adapter API
paul authored
629 repository.create([ self ])
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
630
fdaa224 @paul Updated Adapter API
paul authored
631 @repository = repository
632 @saved = true
05bae27 Refactoring to improve spec coverage
Dan Kubb authored
633
8ca9812 @dkubb Renamed Resource#original_values as Resource#original_attributes
dkubb authored
634 original_attributes.clear
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
635
fdaa224 @paul Updated Adapter API
paul authored
636 identity_map[key] = self
637
638 true
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
639 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
640
ee967c0 @michaelklishin Document DataMapper::Resource#_update
michaelklishin authored
641 # Persists dirty attributes
642 #
643 # If object is not dirty, this method returns false.
644 # If there are non-nullable properties with value of nil,
645 # false is returned as well.
646 #
647 # This method updates identitity map of repository
648 # this object was loaded from, and clears cached key
649 # value (instance variable @key)
650 #
c031e9f @michaelklishin Add returned value to documentation of DataMapper::Resource#_update
michaelklishin authored
651 # @return [TrueClass, FalseClass]
652 # true if the receiver was successfully updated
653 #
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored
654 # @api private
986787c @dkubb No need to explicitly pass in dirty_attributes to _update method
dkubb authored
655 def _update
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
656 if dirty_attributes.empty?
657 true
b2858d1 @dkubb Minor formatting change
dkubb authored
658 elsif dirty_attributes.any? { |p, v| !p.nullable? && v.nil? }
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
659 false
660 else
fdaa224 @paul Updated Adapter API
paul authored
661 # remove from the identity map
662 identity_map.delete(key)
05bae27 Refactoring to improve spec coverage
Dan Kubb authored
663
c115888 @dkubb Changed Resource#to_query to #query to match conventions in Model and…
dkubb authored
664 repository.update(dirty_attributes, Collection.new(query, [ self ]))
7f16c10 Updated has(1) and belongs_to specs to be shared
Dan Kubb authored
665
fdaa224 @paul Updated Adapter API
paul authored
666 # remove the cached key in case it is updated
667 remove_instance_variable(:@key)
668
8ca9812 @dkubb Renamed Resource#original_values as Resource#original_attributes
dkubb authored
669 original_attributes.clear
377098b Saving a Resource with a new key updates the Identity Map
Dan Kubb authored
670
fdaa224 @paul Updated Adapter API
paul authored
671 identity_map[key] = self
672
673 true
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
674 end
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
675 end
676
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored
677 private
678
679 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
680 # Initialize a new instance of this Resource using the provided values
681 #
682 # @param [Hash] attributes
683 # attribute values to use for the new instance
684 #
685 # @return [Resource]
686 # the newly initialized resource instance
687 #
688 # @api public
689 def initialize(attributes = {}) # :nodoc:
690 @saved = false
691 self.attributes = attributes
692 end
693
694 ##
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored
695 # Saves the resource
696 #
697 # @api private
698 def _save
9d620e2 @dkubb Minor documentation fix for InMemoryAdapter#update and #delete
dkubb authored
699 (new? ? _create : _update) && save_children
700 end
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored
701
9d620e2 @dkubb Minor documentation fix for InMemoryAdapter#update and #delete
dkubb authored
702 ##
703 # Saves the children resources
704 #
705 # @api private
706 def save_children
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
707 child_associations.all? { |a| a.save }
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored
708 end
709
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
710 ##
711 # Destroys the resource
712 #
713 # @api private
714 def _destroy
715 if saved?
c115888 @dkubb Changed Resource#to_query to #query to match conventions in Model and…
dkubb authored
716 repository.delete(Collection.new(query, [ self ]))
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
717 @collection.delete(self) if @collection
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
718 reset
c22717e @dkubb Fixed bug in many to many when removing intermediaries linking to orp…
dkubb authored
719 freeze
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored
720 true
721 else
722 false
723 end
724 end
725
bb3b8ee @michaelklishin Document DataMapper::Resource#repository_name
michaelklishin authored
726 # Returns name of the repository this object
727 # was loaded from
728 #
729 # @return [String] name of the repository this object was loaded from
730 #
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
731 # @api private
732 def repository_name
733 repository.name
734 end
735
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
736 # Gets this instance's Model's properties
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
737 #
738 # @return [Array(Property)]
739 # List of this Resource's Model's properties
740 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
741 # @api private
742 def properties
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
743 model.properties(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
744 end
745
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
746 # Gets this instance's Model's relationships
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
747 #
748 # @return [Array(Associations::Relationship)]
749 # List of this instance's Model's Relationships
750 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
751 # @api private
752 def relationships
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
753 model.relationships(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
754 end
755
4d319fb @michaelklishin Document DataMapper::Resouce#identity_map
michaelklishin authored
756 # Returns identity map of repository this object
757 # was loaded from
758 #
759 # @return [DataMapper::IdentityMap]
760 # identity map of repository this object was loaded from
761 #
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
762 # @api semipublic
763 def identity_map
764 repository.identity_map(model)
765 end
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
766
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
767 # Reloads attributes that belong to given lazy loading
768 # context, and not yet loaded
769 #
770 # @api private
771 def lazy_load(property_names)
772 reload_attributes(properties.in_context(property_names) - loaded_properties)
773 end
774
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
775 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
776 # Reloads specified attributes
6186835 Stripped whitespace
Dan Kubb authored
777 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
778 # @param [Enumerable(Symbol)] attributes
779 # name(s) of attribute(s) to reload
6186835 Stripped whitespace
Dan Kubb authored
780 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
781 # @return [Resource]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
782 # the receiver, the current Resource instance
6186835 Stripped whitespace
Dan Kubb authored
783 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
784 # @api private
785 def reload_attributes(attributes)
786 unless attributes.empty? || new?
787 collection.reload(:fields => attributes)
788 end
789
790 self
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
791 end
0ac6b42 Stripped whitespace with sake strip
Dan Kubb authored
792
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
793 # Gets a Query that will return this Resource instance
794 #
795 # @return [Query] Query that will retrieve this Resource instance
0ef5ad1 @michaelklishin Document DataMapper::Resource#lazy_load
michaelklishin authored
796 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
797 # @api private
c115888 @dkubb Changed Resource#to_query to #query to match conventions in Model and…
dkubb authored
798 def query
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
799 model.to_query(repository, key)
3684f20 Minor Resource cleanup
Dan Kubb authored
800 end
801
cb2a5da @michaelklishin Document DataMapper::Resource#child_associations
michaelklishin authored
802 # Returns array of child relationships for which this resource is parent and is loaded
803 #
6faedbd @dkubb Updated child_associations to skip non-collection Relationship
dkubb authored
804 # @return [Array<DataMapper::Associations::OneToMany::Relationship>]
cb2a5da @michaelklishin Document DataMapper::Resource#child_associations
michaelklishin authored
805 # array of child relationships for which this resource is parent and is loaded
806 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
807 # @api private
3684f20 Minor Resource cleanup
Dan Kubb authored
808 def child_associations
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored
809 child_associations = []
810
6faedbd @dkubb Updated child_associations to skip non-collection Relationship
dkubb authored
811 relationships.each_value do |relationship|
812 next unless relationship.respond_to?(:collection_for) && relationship.loaded?(self)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
813
814 association = relationship.get!(self)
815 next unless association.loaded? || association.head.any? || association.tail.any?
816
817 child_associations << association
818 end
819
820 many_to_many, other_associations = child_associations.partition do |association|
821 association.kind_of?(DataMapper::Associations::ManyToMany::Collection)
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored
822 end
823
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
824 many_to_many + other_associations
3684f20 Minor Resource cleanup
Dan Kubb authored
825 end
826
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
827 ##
9c54f28 Added Query#update specs
Dan Kubb authored
828 # Return true if +other+'s is equivalent or equal to +self+'s
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
829 #
830 # @param [Resource] other
831 # The Resource whose attributes are to be compared with +self+'s
b88cab2 Added Query semipublic specs
Dan Kubb authored
832 # @param [Symbol] operator
833 # The comparison operator to use to compare the attributes
834 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
835 # @return [TrueClass, FalseClass]
836 # The result of the comparison of +other+'s attributes with +self+'s
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored
837 #
d478c13 Minor documentation update in Resource
Dan Kubb authored
838 # @api private
9c54f28 Added Query#update specs
Dan Kubb authored
839 def cmp?(other, operator)
b88cab2 Added Query semipublic specs
Dan Kubb authored
840 unless key.send(operator, other.key)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
841 return false
842 end
843
b88cab2 Added Query semipublic specs
Dan Kubb authored
844 if repository.send(operator, other.repository) && !dirty? && !other.dirty?
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
845 return true
846 end
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored
847
05bae27 Refactoring to improve spec coverage
Dan Kubb authored
848 # get all the loaded and non-loaded properties that are not keys,
849 # since the key comparison was performed earlier
850 loaded, not_loaded = properties.select { |p| !p.key? }.partition do |property|
4260cb7 Simplified Property and Resource
Dan Kubb authored
851 property.loaded?(self) && property.loaded?(other)
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored
852 end
853
854 # check all loaded properties, and then all unloaded properties
b88cab2 Added Query semipublic specs
Dan Kubb authored
855 (loaded + not_loaded).all? { |p| p.get(self).send(operator, p.get(other)) }
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored
856 end
0ba594d @myabc Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataM…
myabc authored
857 end # module Resource
858 end # module DataMapper
Something went wrong with that request. Please try again.