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