Skip to content
Newer
Older
100644 859 lines (769 sloc) 21.8 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
36518c9 @dkubb Minor documentation update
dkubb authored May 27, 2009
23 warn 'DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead'
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
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 Nov 23, 2008
139 # Returns the value of the attribute.
140 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
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 Jun 16, 2008
144 #
145 # Class Foo
146 # include DataMapper::Resource
147 #
148 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
149 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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 Nov 21, 2008
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 Nov 20, 2008
163 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Nov 20, 2008
167 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
168 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
169 def attribute_get(name)
84880f2 Simplified Resource#attribute_get
Dan Kubb authored Jan 13, 2009
170 properties[name].get(self)
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
171 end
172
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
Dan Kubb authored Feb 5, 2009
173 alias [] attribute_get
174
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Jun 17, 2008
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 Nov 20, 2008
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 Jun 17, 2008
180 # if nessesary.
181 #
182 # Class Foo
183 # include DataMapper::Resource
184 #
185 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
186 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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 Nov 21, 2008
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 Nov 20, 2008
206 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
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 Nov 20, 2008
210 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
211 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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 Feb 5, 2009
216 alias []= attribute_set
217
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
218 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
219 # Gets all the attributes of the Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
220 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 30, 2008
225 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
226 # @return [Hash]
227 # All the attributes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
228 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
229 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jan 15, 2009
239
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
240 attributes[key] = send(name)
241 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
242 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
243 attributes
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 28, 2008
244 end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 2, 2008
245
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
246 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
247 # Assign values to multiple attributes in one call (mass assignment)
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
248 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
249 # @param [Hash] attributes
250 # names and values of attributes to assign
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
251 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
252 # @return [Hash]
253 # names and values of attributes assigned
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
254 #
255 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jan 15, 2009
269 end
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
270 end
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 29, 2008
271
238682e Added Resource#<=>
Dan Kubb authored Nov 20, 2008
272 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
273 # Reloads association and all child association
238682e Added Resource#<=>
Dan Kubb authored Nov 21, 2008
274 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
275 # @return [Resource]
276 # the receiver, the current Resource instance
238682e Added Resource#<=>
Dan Kubb authored Nov 21, 2008
277 #
278 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
279 def reload
280 if saved?
281 reload_attributes(loaded_properties)
282 child_associations.each { |a| a.reload }
238682e Added Resource#<=>
Dan Kubb authored Nov 21, 2008
283 end
284
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
285 self
7b6845d Minor code refactoring
Dan Kubb authored Feb 8, 2009
286 end
287
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
288 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
289 # Updates attributes and saves this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
290 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
291 # @param [Hash] attributes
292 # attributes to be updated
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
293 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
294 # @return [TrueClass, FalseClass]
295 # true if resource and storage state match
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
296 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
297 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
298 def update(attributes = {})
299 self.attributes = attributes
300 _update
d409c1f @sam Added and specced LoadedSet.
sam authored Mar 15, 2008
301 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
302
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
303 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
304 # Updates attributes and saves this Resource instance, bypassing hooks
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
305 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
306 # @param [Hash] attributes
307 # attributes to be updated
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
308 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
309 # @return [TrueClass, FalseClass]
310 # true if resource and storage state match
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
311 #
312 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Mar 23, 2008
316 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
317
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
318 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
319 # Save the instance and associated children to the data-store.
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
320 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
321 # 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
322 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
323 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
324 # true if Resource instance and all associations were saved
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
325 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
326 # @see Repository#save
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
327 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
328 # @api public
329 chainable do
330 def save
331 _save
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
332 end
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored Mar 23, 2008
333 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
334
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
335 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
336 # Save the instance and associated children to the data-store, bypassing hooks
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
337 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
338 # 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
339 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
340 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
341 # true if Resource instance and all associations were saved
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
342 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
343 # @see Repository#save
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
344 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
345 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jun 18, 2008
349 end
ece0d18 @somebee some small convenient changes. added possibility for iterating throug…
somebee authored May 23, 2008
350 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
351
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
352 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
353 # Destroy the instance, remove it from the repository
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 29, 2009
354 #
355 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
356 # true if resource was destroyed
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
357 #
358 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
359 def destroy
360 _destroy
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
361 end
362
363 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
364 # Destroy the instance, remove it from the repository, bypassing hooks
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
365 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
366 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
367 # true if resource was destroyed
805e402 @myabc YARD conversion for dm-core
myabc authored May 18, 2008
368 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
369 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
370 def destroy!
371 _destroy
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made t…
Bauerpauer authored Mar 23, 2008
372 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
373
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
374 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
375 # Compares another Resource for equality
40fcf73 Stripped whitespace
Dan Kubb authored Nov 26, 2008
376 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jun 2, 2008
380 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
381 # @param [Resource] other
382 # the other Resource to compare with
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
383 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
384 # @return [TrueClass, FalseClass]
385 # true if they are equal, false if not
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
386 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Mar 3, 2008
395 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
396
397 cmp?(other, :eql?)
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored Mar 3, 2008
398 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored May 8, 2008
399
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
400 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
401 # Compares another Resource for equivalency
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
402 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 24, 2008
412 #
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
413 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jan 15, 2009
418
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jan 15, 2009
421 end
422
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
423 cmp?(other, :==)
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
424 end
425
426 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
427 # Compares two Resources to allow them to be sorted
72db385 @advany update_attributes should save model
advany authored May 19, 2008
428 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
429 # @param [Resource] other
430 # The other Resource to compare with
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
431 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 19, 2008
436 #
8708119 @myabc Convert docs to YARD-style
myabc authored May 20, 2008
437 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 15, 2009
449 end
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
450
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
453 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
456 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
459 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
460 # @api private
461 def hash
462 key.hash
1019ac3 @sam Added key validation to Resources, and added Resource#update_attributes.
sam authored May 8, 2008
463 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
464
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 23, 2008
465 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
466 # 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
467 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 24, 2008
469 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 24, 2008
472 #
473 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
484 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
485
486 "#<#{model.name} #{attrs.join(' ')}>"
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
487 end
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
488
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
489 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
491 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
511 #
512 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
513 # true if ivar +name+ has been loaded
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
514 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
516 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 24, 2008
520 end
521
522 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 24, 2008
525 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
526 # class Foo
527 # include DataMapper::Resource
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
528 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
541 end
542
543 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 16, 2009
548 #
549 # @return [TrueClass, FalseClass]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
550 # true if attribute has unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
551 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 24, 2008
555 end
556
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
557 ##
558 # Hash of attributes that have unsaved changes
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
559 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
560 # @return [Hash]
561 # attributes that have unsaved changes
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
562 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Apr 25, 2008
572 end
573
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 20, 2008
574 ##
5e7d549 @michaelklishin Improve documentation of DataMapper::Resource#reset
michaelklishin authored Mar 20, 2009
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 Nov 21, 2008
578 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
579 # @api private
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
580 def reset
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
581 @saved = false
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
582 identity_map.delete(key)
8ca9812 @dkubb Renamed Resource#original_values as Resource#original_attributes
dkubb authored May 19, 2009
583 original_attributes.clear
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
584 end
585
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 28, 2009
595 @collection = Collection.new(query, [ self ])
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
596 end
597
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
598 protected
ec84ce4 @myabc Stripped whitespace with "sake strip"
myabc authored Jun 20, 2008
599
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Mar 20, 2009
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 Jun 19, 2008
610 # Needs to be a protected method so that it is hookable
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
611 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
612 # @return [TrueClass, FalseClass]
613 # true if the receiver was successfully created
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
614 #
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 16, 2009
615 # @api private
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 28, 2008
616 def _create
3558f1d @dbussink Fix that saving a non-dirty object returns true
dbussink authored Oct 9, 2008
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 Jan 30, 2009
618 if new? && !dirty?
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
619 return false
620 end
8d88b6f Updated Resource#save to return false if changing the key to nil
Dan Kubb authored Nov 20, 2008
621
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored Jun 11, 2008
622 # set defaults for new resource
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
623 properties.each do |property|
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
624 unless property.serial? || property.loaded?(self)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
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 Mar 3, 2008
627 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
628
fdaa224 @paul Updated Adapter API
paul authored Mar 25, 2009
629 repository.create([ self ])
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored Jun 12, 2008
630
fdaa224 @paul Updated Adapter API
paul authored Mar 25, 2009
631 @repository = repository
632 @saved = true
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
633
8ca9812 @dkubb Renamed Resource#original_values as Resource#original_attributes
dkubb authored May 19, 2009
634 original_attributes.clear
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored Jun 12, 2008
635
fdaa224 @paul Updated Adapter API
paul authored Mar 25, 2009
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 Mar 3, 2008
639 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
640
ee967c0 @michaelklishin Document DataMapper::Resource#_update
michaelklishin authored Mar 20, 2009
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 Mar 20, 2009
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 May 17, 2009
654 # @api private
986787c @dkubb No need to explicitly pass in dirty_attributes to _update method
dkubb authored May 19, 2009
655 def _update
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
656 if dirty_attributes.empty?
657 true
b2858d1 @dkubb Minor formatting change
dkubb authored Apr 27, 2009
658 elsif dirty_attributes.any? { |p, v| !p.nullable? && v.nil? }
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
659 false
660 else
fdaa224 @paul Updated Adapter API
paul authored Mar 25, 2009
661 # remove from the identity map
662 identity_map.delete(key)
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
663
c115888 @dkubb Changed Resource#to_query to #query to match conventions in Model and…
dkubb authored May 28, 2009
664 repository.update(dirty_attributes, Collection.new(query, [ self ]))
7f16c10 Updated has(1) and belongs_to specs to be shared
Dan Kubb authored Feb 17, 2009
665
fdaa224 @paul Updated Adapter API
paul authored Mar 25, 2009
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 May 19, 2009
669 original_attributes.clear
377098b Saving a Resource with a new key updates the Identity Map
Dan Kubb authored Jan 11, 2009
670
fdaa224 @paul Updated Adapter API
paul authored Mar 25, 2009
671 identity_map[key] = self
672
673 true
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
674 end
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 28, 2008
675 end
676
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 17, 2009
677 private
678
679 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 May 17, 2009
695 # Saves the resource
696 #
697 # @api private
698 def _save
9d620e2 @dkubb Minor documentation fix for InMemoryAdapter#update and #delete
dkubb authored May 26, 2009
699 (new? ? _create : _update) && save_children
700 end
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 17, 2009
701
9d620e2 @dkubb Minor documentation fix for InMemoryAdapter#update and #delete
dkubb authored May 26, 2009
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 May 26, 2009
707 child_associations.all? { |a| a.save }
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 17, 2009
708 end
709
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
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 May 28, 2009
716 repository.delete(Collection.new(query, [ self ]))
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
717 @collection.delete(self) if @collection
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
718 reset
c22717e @dkubb Fixed bug in many to many when removing intermediaries linking to orp…
dkubb authored May 16, 2009
719 freeze
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
720 true
721 else
722 false
723 end
724 end
725
bb3b8ee @michaelklishin Document DataMapper::Resource#repository_name
michaelklishin authored Mar 20, 2009
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 Feb 13, 2009
731 # @api private
732 def repository_name
733 repository.name
734 end
735
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
736 # Gets this instance's Model's properties
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
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 Nov 21, 2008
741 # @api private
742 def properties
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
743 model.properties(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
744 end
745
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
746 # Gets this instance's Model's relationships
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
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 Nov 21, 2008
751 # @api private
752 def relationships
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
753 model.relationships(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
754 end
755
4d319fb @michaelklishin Document DataMapper::Resouce#identity_map
michaelklishin authored Mar 20, 2009
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 Feb 13, 2009
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 Jun 19, 2008
766
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Nov 21, 2008
775 ##
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
776 # Reloads specified attributes
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
777 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
778 # @param [Enumerable(Symbol)] attributes
779 # name(s) of attribute(s) to reload
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
780 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
781 # @return [Resource]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
782 # the receiver, the current Resource instance
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
783 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Jun 19, 2008
791 end
0ac6b42 Stripped whitespace with sake strip
Dan Kubb authored Sep 11, 2008
792
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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 Mar 20, 2009
796 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
797 # @api private
c115888 @dkubb Changed Resource#to_query to #query to match conventions in Model and…
dkubb authored May 28, 2009
798 def query
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
799 model.to_query(repository, key)
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
800 end
801
cb2a5da @michaelklishin Document DataMapper::Resource#child_associations
michaelklishin authored Mar 20, 2009
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 May 23, 2009
804 # @return [Array<DataMapper::Associations::OneToMany::Relationship>]
cb2a5da @michaelklishin Document DataMapper::Resource#child_associations
michaelklishin authored Mar 20, 2009
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 Nov 17, 2008
807 # @api private
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
808 def child_associations
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored Feb 24, 2009
809 child_associations = []
810
6faedbd @dkubb Updated child_associations to skip non-collection Relationship
dkubb authored May 23, 2009
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 May 26, 2009
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 Feb 24, 2009
822 end
823
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
824 many_to_many + other_associations
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
825 end
826
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
827 ##
9c54f28 Added Query#update specs
Dan Kubb authored Jan 31, 2009
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 Nov 28, 2008
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 Jan 24, 2009
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 Nov 28, 2008
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 Nov 25, 2008
837 #
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
838 # @api private
9c54f28 Added Query#update specs
Dan Kubb authored Feb 1, 2009
839 def cmp?(other, operator)
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
840 unless key.send(operator, other.key)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
841 return false
842 end
843
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
844 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
845 return true
846 end
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored Dec 12, 2008
847
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
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 Jan 6, 2009
851 property.loaded?(self) && property.loaded?(other)
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored Dec 12, 2008
852 end
853
854 # check all loaded properties, and then all unloaded properties
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
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 Nov 25, 2008
856 end
0ba594d @myabc Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataM…
myabc authored Apr 3, 2008
857 end # module Resource
858 end # module DataMapper
Something went wrong with that request. Please try again.