Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 1073 lines (961 sloc) 28.397 kb
e8b4f3b Sam Smoot Importing from old repository.
sam authored
1 module DataMapper
2 module Resource
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
3 include Extlib::Assertions
ecb4765 Made Resource#save chainable for easier extending in plugins
Dan Kubb authored
4 extend Chainable
d41858a Dan Kubb Added DM::Deprecate::deprecate() to mark methods as deprecated
dkubb authored
5 extend Deprecate
6
7 deprecate :new_record?, :new?
182e590 Minor code cleanup
Dan Kubb authored
8
27c926a Dan Kubb Stubbed out @api and other docs for several classes
dkubb authored
9 # @deprecated
ffa83a4 Sam Smoot Fixed autotest support, corrected some issues with reloading models.
sam authored
10 def self.append_inclusions(*inclusions)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
11 warn "DataMapper::Resource.append_inclusions is deprecated, use DataMapper::Model.append_inclusions instead (#{caller[0]})"
76a7974 Minor code reformatting
Dan Kubb authored
12 Model.append_inclusions(*inclusions)
ffa83a4 Sam Smoot Fixed autotest support, corrected some issues with reloading models.
sam authored
13 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored
14
27c926a Dan Kubb Stubbed out @api and other docs for several classes
dkubb authored
15 # @deprecated
ffa83a4 Sam Smoot Fixed autotest support, corrected some issues with reloading models.
sam authored
16 def self.extra_inclusions
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
17 warn "DataMapper::Resource.extra_inclusions is deprecated, use DataMapper::Model.extra_inclusions instead (#{caller[0]})"
76a7974 Minor code reformatting
Dan Kubb authored
18 Model.extra_inclusions
19 end
20
27c926a Dan Kubb Stubbed out @api and other docs for several classes
dkubb authored
21 # @deprecated
76a7974 Minor code reformatting
Dan Kubb authored
22 def self.descendants
491bd1e Dirkjan Bussink Improve deprecation messages I encountered during upgrading to 0.10
dbussink authored
23 warn "DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead (#{caller[0]})"
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
24 Model.descendants
ffa83a4 Sam Smoot Fixed autotest support, corrected some issues with reloading models.
sam authored
25 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored
26
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
27 # Deprecated API for updating attributes and saving Resource
28 #
29 # @see #update
30 #
67fbf33 Dan Kubb Tagged Resource#update_attributes as deprecated
dkubb authored
31 # @deprecated
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
32 def update_attributes(attributes = {}, *allowed)
d85c0af Dan Kubb Updated Resource#assert_update_clean_only to include the class/method na...
dkubb authored
33 assert_update_clean_only(:update_attributes)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
34
35 warn "#{model}#update_attributes is deprecated, use #{model}#update instead (#{caller[0]})"
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
36
37 if allowed.any?
38 warn "specifying allowed in #{model}#update_attributes is deprecated, " \
88612ef Dan Kubb Add caller information to all deprecation warnings
dkubb authored
39 "use Hash#only to filter the attributes in the caller (#{caller[0]})"
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
40 attributes = attributes.only(*allowed)
41 end
42
43 update(attributes)
44 end
45
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
46 # Makes sure a class gets all the methods when it includes Resource
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
47 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
48 # @api private
74ef620 Modules now push themselves into Resource rather than it pulling them
Dan Kubb authored
49 def self.included(model)
e41f6ab Renamed Resource::ClassMethods to Model
Dan Kubb authored
50 model.extend Model
748b8d9 Enabled DataMapper::Resource to know what classes have included it.
Martin Kihlgren authored
51 end
52
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
53 # @api public
7361467 Michael Klishin Use alias_method so Emacs' ruby-mode don't consider 'class' a new indent...
michaelklishin authored
54 alias_method :model, :class
6db24c7 Small performance optimizations
Dan Kubb authored
55
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
56 # Repository this resource belongs to in the context of this collection
57 # or of the resource's class.
58 #
59 # @return [Repository]
60 # the respository this resource belongs to, in the context of
61 # a collection OR in the instance's Model's context
62 #
63 # @api semipublic
64 def repository
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
65 # only set @_repository explicitly when persisted
66 defined?(@_repository) ? @_repository : model.repository
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
67 end
68
69 # Retrieve the key(s) for this resource.
70 #
71 # This always returns the persisted key value,
72 # even if the key is changed and not yet persisted.
73 # This is done so all relations still work.
74 #
75 # @return [Array(Key)]
76 # the key(s) identifying this resource
77 #
78 # @api public
79 def key
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
80 return @_key if defined?(@_key)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
81
a105712 Dan Kubb Added PropertySet#valid? and refactored code that tests key validity
dkubb authored
82 model_key = model.key(repository_name)
83
84 key = model_key.map do |property|
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
85 original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil)
86 end
87
a105712 Dan Kubb Added PropertySet#valid? and refactored code that tests key validity
dkubb authored
88 # only memoize a valid key
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
89 @_key = key if model_key.valid?(key)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
90 end
91
92 # Checks if this Resource instance is new
93 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
94 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
95 # true if the resource is new and not saved
96 #
97 # @api public
98 def new?
99 !saved?
100 end
101
102 # Checks if this Resource instance is saved
103 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
104 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
105 # true if the resource has been saved
106 #
107 # @api public
108 def saved?
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
109 @_saved == true
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
110 end
111
19baffc Dan Kubb Added Resource#destroyed?
dkubb authored
112 # Checks if this Resource instance is destroyed
113 #
114 # @return [Boolean]
115 # true if the resource has been destroyed
116 #
117 # @api public
118 def destroyed?
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
119 @_destroyed == true
19baffc Dan Kubb Added Resource#destroyed?
dkubb authored
120 end
121
5dc4838 Dan Kubb Updated Collection#update and #update! to raise an exception when called...
dkubb authored
122 # Checks if the resource has no changes to save
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
123 #
124 # @return [Boolean]
125 # true if the resource may not be persisted
126 #
127 # @api public
128 def clean?
129 !dirty?
130 end
131
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
132 # Checks if the resource has unsaved changes
133 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
134 # @return [Boolean]
135 # true if resource may be persisted
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
136 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
137 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
138 def dirty?
6a6add4 Dan Kubb Ensure Collection#first and Collection#last always return the same insta...
dkubb authored
139 run_once(true) do
140 dirty_self? || dirty_parents? || dirty_children?
141 end
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
142 end
143
88fa9a6 Dan Kubb Added Resource#readonly?
dkubb authored
144 # Checks if this Resource instance is readonly
145 #
146 # @return [Boolean]
147 # true if the resource cannot be persisted
148 #
149 # @api public
150 def readonly?
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
151 @_readonly == true
88fa9a6 Dan Kubb Added Resource#readonly?
dkubb authored
152 end
153
6186835 Stripped whitespace
Dan Kubb authored
154 # Returns the value of the attribute.
155 #
25e6fe5 Emmanuel Gomez Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
156 # Do not read from instance variables directly, but use this method.
157 # This method handles lazy loading the attribute and returning of
158 # defaults if nessesary.
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
159 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
160 # @example
161 # class Foo
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
162 # include DataMapper::Resource
163 #
164 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored
165 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
166 #
167 # def full_name
168 # "#{attribute_get(:first_name)} #{attribute_get(:last_name)}"
169 # end
170 #
171 # # using the shorter syntax
172 # def name_for_address_book
173 # "#{last_name}, #{first_name}"
174 # end
175 # end
176 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
177 # @param [Symbol] name
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
178 # name of attribute to retrieve
25e6fe5 Emmanuel Gomez Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
179 #
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
180 # @return [Object]
181 # the value stored at that given attribute
182 # (nil if none, and default if necessary)
25e6fe5 Emmanuel Gomez Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
183 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
184 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
185 def attribute_get(name)
84880f2 Simplified Resource#attribute_get
Dan Kubb authored
186 properties[name].get(self)
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
187 end
188
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribute_...
Dan Kubb authored
189 alias [] attribute_get
190
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
191 # 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
192 # if it has been changed so that it may be saved. Do not set from
193 # instance variables directly, but use this method. This method
25e6fe5 Emmanuel Gomez Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
194 # handles the lazy loading the property and returning of defaults
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
195 # if nessesary.
196 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
197 # @example
198 # class Foo
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
199 # include DataMapper::Resource
200 #
201 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored
202 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
203 #
204 # def full_name(name)
205 # name = name.split(' ')
206 # attribute_set(:first_name, name[0])
207 # attribute_set(:last_name, name[1])
208 # end
209 #
210 # # using the shorter syntax
211 # def name_from_address_book(name)
212 # name = name.split(', ')
213 # first_name = name[1]
214 # last_name = name[0]
215 # end
216 # end
217 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
218 # @param [Symbol] name
219 # name of attribute to set
220 # @param [Object] value
221 # value to store
25e6fe5 Emmanuel Gomez Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
222 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
223 # @return [Object]
224 # the value stored at that given attribute, nil if none,
225 # and default if necessary
25e6fe5 Emmanuel Gomez Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
226 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
227 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
228 def attribute_set(name, value)
229 properties[name].set(self, value)
230 end
231
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribute_...
Dan Kubb authored
232 alias []= attribute_set
233
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
234 # Gets all the attributes of the Resource instance
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
235 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
236 # @param [Symbol] key_on
237 # Use this attribute of the Property as keys.
238 # defaults to :name. :field is useful for adapters
239 # :property or nil use the actual Property object.
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
240 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
241 # @return [Hash]
242 # All the attributes
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
243 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
244 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
245 def attributes(key_on = :name)
246 attributes = {}
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
247
248 lazy_load(properties)
249 fields.each do |property|
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
250 if model.public_method_defined?(name = property.name)
251 key = case key_on
252 when :name then name
253 when :field then property.field
254 else property
255 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
256
c1e78c6 Dan Kubb Use Resource#__send__ instead of #send in case it is overridden
dkubb authored
257 attributes[key] = __send__(name)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
258 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
259 end
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
260
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
261 attributes
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
262 end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
263
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
264 # Assign values to multiple attributes in one call (mass assignment)
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
265 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
266 # @param [Hash] attributes
267 # names and values of attributes to assign
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
268 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
269 # @return [Hash]
270 # names and values of attributes assigned
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
271 #
272 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
273 def attributes=(attributes)
274 attributes.each do |name, value|
275 case name
276 when String, Symbol
277 if model.public_method_defined?(setter = "#{name}=")
c1e78c6 Dan Kubb Use Resource#__send__ instead of #send in case it is overridden
dkubb authored
278 __send__(setter, value)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
279 else
978c81a Dan Kubb Minor update to error message for unknown attribute in Resource#attribut...
dkubb authored
280 raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}"
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
281 end
282 when Associations::Relationship, Property
283 name.set(self, value)
284 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
285 end
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
286 end
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
287
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
288 # Reloads association and all child association
238682e Added Resource#<=>
Dan Kubb authored
289 #
6f57483 Dan Kubb Updated Resource#reload to clear non-key property and relationship ivars
dkubb authored
290 # This is accomplished by resetting the Resource key to it's
291 # original value, and then removing all the ivars for properties
292 # and relationships. On the next access of those ivars, the
293 # resource will eager load what it needs. While this is more of
294 # a lazy reload, it should result is more consistent behavior
295 # since no cached results will remain from the initial load.
296 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
297 # @return [Resource]
298 # the receiver, the current Resource instance
238682e Added Resource#<=>
Dan Kubb authored
299 #
300 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
301 def reload
6f57483 Dan Kubb Updated Resource#reload to clear non-key property and relationship ivars
dkubb authored
302 if key
303 reset_key
304 clear_subjects
238682e Added Resource#<=>
Dan Kubb authored
305 end
306
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
307 self
7b6845d Minor code refactoring
Dan Kubb authored
308 end
309
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
310 # Updates attributes and saves this Resource instance
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
311 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
312 # @param [Hash] attributes
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
313 # attributes to be updated
6186835 Stripped whitespace
Dan Kubb authored
314 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
315 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
316 # true if resource and storage state match
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
317 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
318 # @api public
639f435 Dan Kubb Minor simplifications of Resource
dkubb authored
319 def update(attributes = {})
320 assert_update_clean_only(:update)
321 self.attributes = attributes
322 save
d409c1f Sam Smoot Added and specced LoadedSet.
sam authored
323 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
324
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
325 # Updates attributes and saves this Resource instance, bypassing hooks
6186835 Stripped whitespace
Dan Kubb authored
326 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
327 # @param [Hash] attributes
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
328 # attributes to be updated
dfac1c5 Dirkjan Bussink Add more shared Resource specs
dbussink authored
329 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
330 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
331 # true if resource and storage state match
dfac1c5 Dirkjan Bussink Add more shared Resource specs
dbussink authored
332 #
333 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
334 def update!(attributes = {})
d85c0af Dan Kubb Updated Resource#assert_update_clean_only to include the class/method na...
dkubb authored
335 assert_update_clean_only(:update!)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
336 self.attributes = attributes
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
337 save!
a519fb4 Sam Smoot CRUD in DataObjectAdapter is mostly done. There appears to be a bug in d...
sam authored
338 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
339
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
340 # Save the instance and loaded, dirty associations to the data-store
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
341 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
342 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
343 # true if Resource instance and all associations were saved
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
344 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
345 # @api public
1f9eed1 Dan Kubb Refactored Resource#save and Resource#dirty to handle circular dependenc...
dkubb authored
346 def save
7b1b917 Dan Kubb Moved assertions closer to the methods they should be called in
dkubb authored
347 assert_not_destroyed(:save)
1f9eed1 Dan Kubb Refactored Resource#save and Resource#dirty to handle circular dependenc...
dkubb authored
348 _save(true)
041d9f1 Sam Smoot DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored
349 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
350
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
351 # Save the instance and loaded, dirty associations to the data-store, bypassing hooks
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
352 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
353 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
354 # true if Resource instance and all associations were saved
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
355 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
356 # @api public
1f9eed1 Dan Kubb Refactored Resource#save and Resource#dirty to handle circular dependenc...
dkubb authored
357 def save!
7b1b917 Dan Kubb Moved assertions closer to the methods they should be called in
dkubb authored
358 assert_not_destroyed(:save!)
1f9eed1 Dan Kubb Refactored Resource#save and Resource#dirty to handle circular dependenc...
dkubb authored
359 _save(false)
ece0d18 Sindre Aarsaether some small convenient changes. added possibility for iterating through t...
somebee authored
360 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
361
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
362 # Destroy the instance, remove it from the repository
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
363 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
364 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
365 # true if resource was destroyed
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
366 #
367 # @api public
639f435 Dan Kubb Minor simplifications of Resource
dkubb authored
368 def destroy
369 destroy!
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
370 end
371
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
372 # Destroy the instance, remove it from the repository, bypassing hooks
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored
373 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
374 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
375 # true if resource was destroyed
805e402 Alex Coles YARD conversion for dm-core
myabc authored
376 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
377 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
378 def destroy!
ca8aa59 Dan Kubb Resource#update and Resource#destroy should be idempotent
dkubb authored
379 if saved? && !destroyed?
380 repository.delete(collection_for_self)
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
381 @_destroyed = true
382 @_readonly = true
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
383 reset
384 end
19baffc Dan Kubb Added Resource#destroyed?
dkubb authored
385
386 destroyed?
320fb84 Scott Bauer Added a couple api-check specs. Restored Resource#new_record?. Made the...
Bauerpauer authored
387 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
388
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
389 # Compares another Resource for equality
40fcf73 Stripped whitespace
Dan Kubb authored
390 #
567d41c Dan Kubb Updated Resource#== to not test the type
dkubb authored
391 # Resource is equal to +other+ if they are the same object
392 # (identical object_id) or if they are both of the *same model* and
393 # all of their attributes are equivalent
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
394 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
395 # @param [Resource] other
396 # the other Resource to compare with
6186835 Stripped whitespace
Dan Kubb authored
397 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
398 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
399 # true if they are equal, false if not
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
400 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
401 # @api public
402 def eql?(other)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
403 return true if equal?(other)
30bda1f Dan Kubb Simplified comparison methods
dkubb authored
404 instance_of?(other.class) && cmp?(other, :eql?)
e58ed2e Sam Smoot Lots and lots of changes to move things over to the new Resource module,...
sam authored
405 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored
406
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
407 # Compares another Resource for equivalency
6186835 Stripped whitespace
Dan Kubb authored
408 #
567d41c Dan Kubb Updated Resource#== to not test the type
dkubb authored
409 # Resource is equivalent to +other+ if they are the same object
410 # (identical object_id) or all of their attribute are equivalent
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
411 #
412 # @param [Resource] other
413 # the other Resource to compare with
414 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
415 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
416 # true if they are equivalent, false if not
6186835 Stripped whitespace
Dan Kubb authored
417 #
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
418 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
419 def ==(other)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
420 return true if equal?(other)
567d41c Dan Kubb Updated Resource#== to not test the type
dkubb authored
421 other.respond_to?(:repository) &&
422 other.respond_to?(:key) &&
423 other.respond_to?(:clean?) &&
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
424 cmp?(other, :==)
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
425 end
426
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
427 # Compares two Resources to allow them to be sorted
72db385 Abdul-Rahman Advany update_attributes should save model
advany authored
428 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
429 # @param [Resource] other
430 # The other Resource to compare with
78daf51 Abdul-Rahman Advany add docs to DataMapper::Resource
advany authored
431 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
432 # @return [Integer]
433 # Return 0 if Resources should be sorted as the same, -1 if the
434 # other Resource should be after self, and 1 if the other Resource
435 # should be before self
72db385 Abdul-Rahman Advany update_attributes should save model
advany authored
436 #
8708119 Alex Coles Convert docs to YARD-style
myabc authored
437 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
438 def <=>(other)
5bfb4f3 Dan Kubb Allow instances of the same base model to be comparable
dkubb authored
439 unless other.kind_of?(model.base_model)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
440 raise ArgumentError, "Cannot compare a #{other.model} instance with a #{model} instance"
441 end
442 cmp = 0
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
443 model.default_order(repository_name).each do |direction|
e212100 Dan Kubb Use Query::Sort when sorting resources
dkubb authored
444 cmp = direction.get(self) <=> direction.get(other)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
445 break if cmp != 0
446 end
447 cmp
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
448 end
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
449
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
450 # Returns hash value of the object.
451 # Two objects with the same hash value assumed equal (using eql? method)
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
452 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
453 # DataMapper resources are equal when their models have the same hash
454 # and they have the same set of properties
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
455 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
456 # When used as key in a Hash or Hash subclass, objects are compared
457 # by eql? and thus hash value has direct effect on lookup
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
458 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
459 # @api private
460 def hash
461 key.hash
1019ac3 Sam Smoot Added key validation to Resources, and added Resource#update_attributes.
sam authored
462 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
463
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
464 # Get a Human-readable representation of this Resource instance
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
465 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
466 # 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
467 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
468 # @return [String]
469 # Human-readable representation of this Resource instance
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
470 #
471 # @api public
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
472 def inspect
473 # TODO: display relationship values
474 attrs = properties.map do |property|
475 value = if new? || property.loaded?(self)
476 property.get!(self).inspect
477 else
478 '<not loaded>'
479 end
480
481 "#{property.instance_variable_name}=#{value}"
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
482 end
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
483
484 "#<#{model.name} #{attrs.join(' ')}>"
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
485 end
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
486
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
487 # Hash of original values of attributes that have unsaved changes
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
488 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
489 # @return [Hash]
490 # original values of attributes that have unsaved changes
491 #
492 # @api semipublic
493 def original_attributes
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
494 @_original_attributes ||= {}
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
495 end
496
497 # Checks if an attribute has been loaded from the repository
498 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
499 # @example
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
500 # class Foo
501 # include DataMapper::Resource
502 #
503 # property :name, String
504 # property :description, Text, :lazy => false
505 # end
506 #
507 # Foo.new.attribute_loaded?(:description) #=> false
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
508 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
509 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
510 # true if ivar +name+ has been loaded
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
511 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
512 # @return [Boolean]
513 # true if ivar +name+ has been loaded
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
514 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
515 # @api private
516 def attribute_loaded?(name)
517 properties[name].loaded?(self)
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
518 end
519
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
520 # Checks if an attribute has unsaved changes
521 #
522 # @param [Symbol] name
523 # name of attribute to check for unsaved changes
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
524 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
525 # @return [Boolean]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
526 # true if attribute has unsaved changes
65c1174 Dan Kubb Updated Collection#update! and #destroy! to work with limited collection...
dkubb authored
527 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
528 # @api semipublic
529 def attribute_dirty?(name)
530 dirty_attributes.key?(properties[name])
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
531 end
532
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
533 # Hash of attributes that have unsaved changes
f132afb Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
534 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
535 # @return [Hash]
536 # attributes that have unsaved changes
f132afb Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
537 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
538 # @api semipublic
539 def dirty_attributes
540 dirty_attributes = {}
541
542 original_attributes.each_key do |property|
543 dirty_attributes[property] = property.value(property.get!(self))
544 end
545
546 dirty_attributes
e3ad4a2 Improved the transactional system greatly and introduced two-phase
Martin Kihlgren authored
547 end
548
5e7d549 Michael Klishin Improve documentation of DataMapper::Resource#reset
michaelklishin authored
549 # Reset the Resource to a similar state as a new record:
550 # removes it from identity map and clears original property
551 # values (thus making all properties non dirty)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
552 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
553 # @api private
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
554 def reset
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
555 @_saved = false
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
556 identity_map.delete(key)
8ca9812 Dan Kubb Renamed Resource#original_values as Resource#original_attributes
dkubb authored
557 original_attributes.clear
28321b3 Dan Kubb Refactored ManyToMany relationship
dkubb authored
558 self
3684f20 Minor Resource cleanup
Dan Kubb authored
559 end
560
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
561 # Returns the Collection the Resource is associated with
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
562 #
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
563 # @return [nil]
564 # nil if this is a new record
565 # @return [Collection]
566 # a Collection that self belongs to
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
567 #
568 # @api private
569 def collection
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
570 return @_collection if (@_collection && @_collection.query.conditions.matches?(self)) || new? || readonly?
cdb918f Dan Kubb Refactored SEL behavior of Collections
dkubb authored
571 collection_for_self
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
572 end
573
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
574 # Associates a Resource to a Collection
575 #
576 # @param [Collection, nil] collection
577 # the collection to associate the resource with
578 #
579 # @return [nil]
580 # nil if this is a new record
581 # @return [Collection]
582 # a Collection that self belongs to
583 #
584 # @api private
585 def collection=(collection)
586 @_collection = collection
587 end
588
e41cc42 Dan Kubb Updated Resource#query to be semipublic
dkubb authored
589 # Return a collection including the current resource only
590 #
591 # @return [Collection]
592 # a collection containing self
593 #
594 # @api private
595 def collection_for_self
596 Collection.new(query, [ self ])
597 end
598
599 # Returns a Query that will match the resource
600 #
601 # @return [Query]
602 # Query that will match the resource
603 #
604 # @api semipublic
605 def query
606 Query.new(repository, model, :fields => fields, :conditions => conditions)
607 end
608
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
609 protected
ec84ce4 Alex Coles Stripped whitespace with "sake strip"
myabc authored
610
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
611 # Method for hooking callbacks on resource creation
a108799 Dan Kubb Delegate Resource#save and #update to #save! and #update!
dkubb authored
612 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
613 # @return [Boolean]
614 # true if the create was successful, false if not
6186835 Stripped whitespace
Dan Kubb authored
615 #
dc22e45 Dan Kubb Updated ManyToMany::Collection#update! to act more like Collection#updat...
dkubb authored
616 # @api private
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
617 def create_hook
618 _create
e58ed2e Sam Smoot Lots and lots of changes to move things over to the new Resource module,...
sam authored
619 end
15987f0 David Leal Merge branch 'master' of git@github.com:sam/dm-core
david authored
620
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
621 # Method for hooking callbacks on resource updates
ee967c0 Michael Klishin Document DataMapper::Resource#_update
michaelklishin authored
622 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
623 # @return [Boolean]
624 # true if the update was successful, false if not
c031e9f Michael Klishin Add returned value to documentation of DataMapper::Resource#_update
michaelklishin authored
625 #
dc22e45 Dan Kubb Updated ManyToMany::Collection#update! to act more like Collection#updat...
dkubb authored
626 # @api private
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
627 def update_hook
628 _update
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
629 end
630
dc22e45 Dan Kubb Updated ManyToMany::Collection#update! to act more like Collection#updat...
dkubb authored
631 private
632
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
633 # Initialize a new instance of this Resource using the provided values
634 #
ba3d0ca Dan Kubb Pass through arguments/block in Model#new to allow custom #initialize
dkubb authored
635 # @param [Hash] attributes
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
636 # attribute values to use for the new instance
637 #
ba3d0ca Dan Kubb Pass through arguments/block in Model#new to allow custom #initialize
dkubb authored
638 # @return [Hash]
639 # attribute values used in the new instance
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
640 #
641 # @api public
ba3d0ca Dan Kubb Pass through arguments/block in Model#new to allow custom #initialize
dkubb authored
642 def initialize(attributes = {}, &block) # :nodoc:
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
643 self.attributes = attributes
644 end
645
bb3b8ee Michael Klishin Document DataMapper::Resource#repository_name
michaelklishin authored
646 # Returns name of the repository this object
647 # was loaded from
648 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
649 # @return [String]
650 # name of the repository this object was loaded from
bb3b8ee Michael Klishin Document DataMapper::Resource#repository_name
michaelklishin authored
651 #
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
652 # @api private
653 def repository_name
654 repository.name
655 end
656
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
657 # Gets this instance's Model's properties
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, Clas...
Emmanuel Gomez authored
658 #
659 # @return [Array(Property)]
660 # List of this Resource's Model's properties
661 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
662 # @api private
663 def properties
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
664 model.properties(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
665 end
666
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
667 # Gets this instance's Model's relationships
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, Clas...
Emmanuel Gomez authored
668 #
669 # @return [Array(Associations::Relationship)]
670 # List of this instance's Model's Relationships
671 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
672 # @api private
673 def relationships
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
674 model.relationships(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
675 end
676
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
677 # Returns the identity map for the model from the repository
4d319fb Michael Klishin Document DataMapper::Resouce#identity_map
michaelklishin authored
678 #
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
679 # @return [IdentityMap]
4d319fb Michael Klishin Document DataMapper::Resouce#identity_map
michaelklishin authored
680 # identity map of repository this object was loaded from
681 #
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored
682 # @api semipublic
683 def identity_map
684 repository.identity_map(model)
685 end
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
686
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
687 # Fetches all the names of the attributes that have been loaded,
688 # even if they are lazy but have been called
689 #
690 # @return [Array<Property>]
691 # names of attributes that have been loaded
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
692 #
693 # @api private
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
694 def fields
695 properties.select do |property|
696 property.loaded?(self) || (new? && property.default?)
697 end
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
698 end
699
6f57483 Dan Kubb Updated Resource#reload to clear non-key property and relationship ivars
dkubb authored
700 # Reset the key to the original value
701 #
702 # @return [undefined]
703 #
704 # @api private
705 def reset_key
706 properties.key.zip(key) do |property, value|
707 property.set!(self, value)
708 original_attributes.delete(property)
709 end
710 end
711
712 # Remove all the ivars for properties and relationships
713 #
714 # @return [undefined]
715 #
716 # @api private
717 def clear_subjects
718 model_properties = properties
719
720 (model_properties - model_properties.key | relationships.values).each do |subject|
721 next unless subject.loaded?(self)
722 remove_instance_variable(subject.instance_variable_name)
723 original_attributes.delete(subject)
724 end
725 end
726
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
727 # Lazy loads attributes not yet loaded
728 #
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
729 # @param [Array<Property>] properties
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
730 # the properties to reload
731 #
732 # @return [self]
733 #
734 # @api private
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
735 def lazy_load(properties)
736 eager_load(properties - fields)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
737 end
738
739 # Reloads specified attributes
6186835 Stripped whitespace
Dan Kubb authored
740 #
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
741 # @param [Array<Property>] properties
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
742 # the properties to reload
6186835 Stripped whitespace
Dan Kubb authored
743 #
43ce436 Emmanuel Gomez Updated Resource API docs to YARD format.
emmanuel authored
744 # @return [Resource]
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
745 # the receiver, the current Resource instance
6186835 Stripped whitespace
Dan Kubb authored
746 #
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
747 # @api private
f0f8662 Dan Kubb Refactored Resource and Collection reloading
dkubb authored
748 def eager_load(properties)
749 unless properties.empty? || key.nil?
750 collection.reload(:fields => properties)
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
751 end
752
753 self
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
754 end
0ac6b42 Stripped whitespace with sake strip
Dan Kubb authored
755
e41cc42 Dan Kubb Updated Resource#query to be semipublic
dkubb authored
756 # Return conditions to match the Resource
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
757 #
e41cc42 Dan Kubb Updated Resource#query to be semipublic
dkubb authored
758 # @return [Hash]
759 # query conditions
0ef5ad1 Michael Klishin Document DataMapper::Resource#lazy_load
michaelklishin authored
760 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
761 # @api private
e41cc42 Dan Kubb Updated Resource#query to be semipublic
dkubb authored
762 def conditions
5b4be32 Dan Kubb Use loaded properties for Resource#conditions when no key
dkubb authored
763 key = self.key
764 if key
765 model.key_conditions(repository, key)
766 else
767 conditions = {}
768 properties.each do |property|
769 next unless property.loaded?(self)
770 conditions[property] = property.get!(self)
771 end
772 conditions
773 end
ca8aa59 Dan Kubb Resource#update and Resource#destroy should be idempotent
dkubb authored
774 end
775
2e1fd21 Dan Kubb Persist dirty parent resources when saving a Resource
dkubb authored
776 # @api private
777 def parent_relationships
778 parent_relationships = []
779
780 relationships.each_value do |relationship|
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
781 next unless relationship.respond_to?(:resource_for) && relationship.loaded?(self) && relationship.get!(self)
2e1fd21 Dan Kubb Persist dirty parent resources when saving a Resource
dkubb authored
782 parent_relationships << relationship
783 end
784
785 parent_relationships
786 end
787
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
788 # Returns loaded child relationships
cb2a5da Michael Klishin Document DataMapper::Resource#child_associations
michaelklishin authored
789 #
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
790 # @return [Array<Associations::OneToMany::Relationship>]
cb2a5da Michael Klishin Document DataMapper::Resource#child_associations
michaelklishin authored
791 # array of child relationships for which this resource is parent and is loaded
792 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
793 # @api private
2e1fd21 Dan Kubb Persist dirty parent resources when saving a Resource
dkubb authored
794 def child_relationships
795 child_relationships = []
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored
796
6faedbd Dan Kubb Updated child_associations to skip non-collection Relationship
dkubb authored
797 relationships.each_value do |relationship|
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
798 next unless relationship.respond_to?(:collection_for) && relationship.loaded?(self) && relationship.get!(self)
2e1fd21 Dan Kubb Persist dirty parent resources when saving a Resource
dkubb authored
799 child_relationships << relationship
d54fbe5 Dan Kubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored
800 end
801
2e1fd21 Dan Kubb Persist dirty parent resources when saving a Resource
dkubb authored
802 many_to_many, other = child_relationships.partition do |relationship|
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
803 relationship.kind_of?(Associations::ManyToMany::Relationship)
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored
804 end
805
2e1fd21 Dan Kubb Persist dirty parent resources when saving a Resource
dkubb authored
806 many_to_many + other
3684f20 Minor Resource cleanup
Dan Kubb authored
807 end
808
7ed6e37 Dan Kubb Updated Resource to be dirty when associations are dirty
dkubb authored
809 # @api private
810 def parent_resources
811 parent_relationships.map { |relationship| relationship.get!(self) }
812 end
813
814 # @api private
815 def child_collections
816 child_relationships.map { |relationship| relationship.get!(self) }
817 end
818
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
819 # Creates the resource with default values
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
820 #
821 # If resource is not dirty or a new (not yet saved),
822 # this method returns false
823 #
824 # On successful save identity map of the repository is
825 # updated
826 #
827 # Needs to be a protected method so that it is hookable
828 #
829 # The primary purpose of this method is to allow before :create
830 # hooks to fire at a point just before/after resource creation
831 #
832 # @return [Boolean]
833 # true if the receiver was successfully created
834 #
835 # @api private
836 def _create
837 # Can't create a resource that is not dirty and doesn't have serial keys
08df3a1 Dan Kubb Changed usage of !dirty? to clean? in Resource
dkubb authored
838 return false if new? && clean?
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
839
840 # set defaults for new resource
841 properties.each do |property|
842 unless property.serial? || property.loaded?(self)
843 property.set(self, property.default_for(self))
844 end
845 end
846
847 repository.create([ self ])
848
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
849 @_repository = repository
850 @_saved = true
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
851
852 original_attributes.clear
853
854 identity_map[key] = self
855
856 true
857 end
858
859 # Updates resource state
860 #
861 # The primary purpose of this method is to allow before :update
862 # hooks to fire at a point just before/after resource update whether
863 # it is the result of Resource#save, or using Resource#update
864 #
865 # @return [Boolean]
866 # true if the receiver was successfully created
867 #
868 # @api private
869 def _update
e66c1bd Dan Kubb Fixed problem with Resource#save failing with custom properties
dkubb authored
870 if original_attributes.empty?
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
871 true
e66c1bd Dan Kubb Fixed problem with Resource#save failing with custom properties
dkubb authored
872 elsif original_attributes.any? { |property, _| !property.valid?(property.get!(self)) }
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
873 false
874 else
875 # remove from the identity map
876 identity_map.delete(key)
877
ca8aa59 Dan Kubb Resource#update and Resource#destroy should be idempotent
dkubb authored
878 repository.update(dirty_attributes, collection_for_self)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
879
e66c1bd Dan Kubb Fixed problem with Resource#save failing with custom properties
dkubb authored
880 original_attributes.clear
881
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
882 # remove the cached key in case it is updated
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
883 remove_instance_variable(:@_key)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
884
885 identity_map[key] = self
886
887 true
888 end
889 end
890
1f9eed1 Dan Kubb Refactored Resource#save and Resource#dirty to handle circular dependenc...
dkubb authored
891 # @api private
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
892 def _save(safe)
893 run_once(true) do
894 save_parents(safe) && save_self(safe) && save_children(safe)
895 end
6b9ab6d Dan Kubb A dirty sibling should not make a resource dirty
dkubb authored
896 end
897
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
898 # Saves the resource
899 #
900 # @return [Boolean]
901 # true if the resource was successfully saved
902 #
903 # @api semipublic
3f1c177 Dirkjan Bussink Make safe the default if people call save_self directly
dbussink authored
904 def save_self(safe = true)
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
905 if safe
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
906 new? ? create_hook : update_hook
907 else
908 new? ? _create : _update
909 end
910 end
911
912 # Saves the parent resources
913 #
914 # @return [Boolean]
915 # true if the parents were successfully saved
916 #
917 # @api private
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
918 def save_parents(safe)
919 run_once(true) do
920 parent_relationships.all? do |relationship|
921 parent = relationship.get!(self)
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
922
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
923 if parent.__send__(:save_parents, safe) && parent.__send__(:save_self, safe)
924 relationship.set(self, parent) # set the FK values
925 end
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
926 end
927 end
928 end
929
930 # Saves the children resources
931 #
932 # @return [Boolean]
933 # true if the children were successfully saved
934 #
935 # @api private
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
936 def save_children(safe)
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
937 child_collections.all? do |collection|
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
938 collection.send(safe ? :save : :save!)
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
939 end
940 end
941
942 # Checks if the resource has unsaved changes
943 #
944 # @return [Boolean]
945 # true if the resource has unsaged changes
946 #
947 # @api private
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
948 def dirty_self?
949 if original_attributes.any?
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
950 true
951 elsif new?
952 !model.serial.nil? || properties.any? { |property| property.default? }
953 else
954 false
955 end
956 end
957
958 # Checks if the parents have unsaved changes
959 #
960 # @return [Boolean]
961 # true if the parents have unsaved changes
962 #
963 # @api private
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
964 def dirty_parents?
965 run_once(false) do
966 parent_resources.any? do |parent|
967 parent.__send__(:dirty_self?) || parent.__send__(:dirty_parents?)
968 end
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
969 end
970 end
971
972 # Checks if the children have unsaved changes
973 #
974 # @param [Hash] resources
975 # resources that have already been tested
976 #
977 # @return [Boolean]
978 # true if the children have unsaved changes
979 #
980 # @api private
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
981 def dirty_children?
982 child_collections.any? { |children| children.dirty? }
f5e8c5f Dan Kubb Updated saving and dirtyness internal methods to be private
dkubb authored
983 end
984
9c54f28 Added Query#update specs
Dan Kubb authored
985 # Return true if +other+'s is equivalent or equal to +self+'s
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, Clas...
Emmanuel Gomez authored
986 #
987 # @param [Resource] other
988 # The Resource whose attributes are to be compared with +self+'s
b88cab2 Added Query semipublic specs
Dan Kubb authored
989 # @param [Symbol] operator
990 # The comparison operator to use to compare the attributes
991 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
992 # @return [Boolean]
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, Clas...
Emmanuel Gomez authored
993 # 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
994 #
d478c13 Minor documentation update in Resource
Dan Kubb authored
995 # @api private
9c54f28 Added Query#update specs
Dan Kubb authored
996 def cmp?(other, operator)
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
997 return false unless key.send(operator, other.key)
08df3a1 Dan Kubb Changed usage of !dirty? to clean? in Resource
dkubb authored
998 return true if repository.send(operator, other.repository) && clean? && other.clean?
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored
999
05bae27 Refactoring to improve spec coverage
Dan Kubb authored
1000 # get all the loaded and non-loaded properties that are not keys,
1001 # since the key comparison was performed earlier
fe2efae Dan Kubb Removed most Uncommunicative Names from source
dkubb authored
1002 loaded, not_loaded = properties.select { |property| !property.key? }.partition do |property|
4260cb7 Simplified Property and Resource
Dan Kubb authored
1003 property.loaded?(self) && property.loaded?(other)
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored
1004 end
1005
1006 # check all loaded properties, and then all unloaded properties
fe2efae Dan Kubb Removed most Uncommunicative Names from source
dkubb authored
1007 (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
1008 end
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
1009
1010 # Raises an exception if #update is performed on a dirty resource
1011 #
f127bad Dan Kubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored
1012 # @param [Symbol] method
1013 # the name of the method to use in the exception
1014 #
1015 # @return [undefined]
1016 #
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
1017 # @raise [UpdateConflictError]
1018 # raise if the resource is dirty
1019 #
1020 # @api private
d85c0af Dan Kubb Updated Resource#assert_update_clean_only to include the class/method na...
dkubb authored
1021 def assert_update_clean_only(method)
fd88599 Dan Kubb Use Resource#dirty? to test for dirtyness prior to executing Resource#up...
dkubb authored
1022 if dirty?
d85c0af Dan Kubb Updated Resource#assert_update_clean_only to include the class/method na...
dkubb authored
1023 raise UpdateConflictError, "#{model}##{method} cannot be called on a dirty resource"
1024 end
25ace54 Dan Kubb Modified Resource#update and #update! to raise an exception on modificat...
dkubb authored
1025 end
92e8a58 Dan Kubb Assert that Resource#save cannot be called on a destroyed resource
dkubb authored
1026
1027 # Raises an exception if #save is performed on a destroyed resource
1028 #
1029 # @param [Symbol] method
1030 # the name of the method to use in the exception
1031 #
1032 # @return [undefined]
1033 #
1034 # @raise [PersistenceError]
1035 # raise if the resource is destroyed
1036 #
1037 # @api private
1038 def assert_not_destroyed(method)
1039 if destroyed?
1040 raise PersistenceError, "#{model}##{method} cannot be called on a destroyed resource"
1041 end
1042 end
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
1043
1044 # Prevent a method from being in the stack more than once
1045 #
1046 # The purpose of this method is to prevent SystemStackError from
1047 # being thrown from methods from encountering infinite recursion
1048 # when called on resources having circular dependencies.
1049 #
1050 # @param [Object] default
1051 # default return value
1052 #
1053 # @yield The block of code to run once
1054 #
1055 # @return [Object]
1056 # block return value
1057 #
1058 # @api private
1059 def run_once(default)
e886e3f Dan Kubb Changed usage of caller() into Kernel::caller()
dkubb authored
1060 caller_method = Kernel.caller(1).first[/`([^'?!]+)[?!]?'/, 1]
337f9d0 Dan Kubb Prefix all Resource internal ivars with an underscore
dkubb authored
1061 sentinel = "@_#{caller_method}_sentinel"
9115894 Dan Kubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored
1062 return instance_variable_get(sentinel) if instance_variable_defined?(sentinel)
1063
1064 begin
1065 instance_variable_set(sentinel, default)
1066 yield
1067 ensure
1068 remove_instance_variable(sentinel)
1069 end
1070 end
0ba594d Alex Coles Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataMapp...
myabc authored
1071 end # module Resource
1072 end # module DataMapper
Something went wrong with that request. Please try again.