Skip to content
This repository
Newer
Older
100644 1240 lines (1117 sloc) 32.55 kb
a7f73e0d » dkubb
2010-02-23 Added #raise_on_save_failure to Model metaclass, Model, and Resource
1 # TODO: DRY up raise_on_save_failure with attr_accessor_with_default
2 # once AS branch is merged in
3
e8b4f3bf » sam
2008-03-01 Importing from old repository.
4 module DataMapper
5 module Resource
999e375c » snusnu
2010-02-10 Adds active_support/extlib compatibility and a Gemfile
6 include DataMapper::Assertions
ecb47657 » Dan Kubb
2009-02-13 Made Resource#save chainable for easier extending in plugins
7 extend Chainable
d41858a7 » dkubb
2009-05-01 Added DM::Deprecate::deprecate() to mark methods as deprecated
8 extend Deprecate
9
10 deprecate :new_record?, :new?
182e5904 » Dan Kubb
2008-07-08 Minor code cleanup
11
27c926a0 » dkubb
2009-04-20 Stubbed out @api and other docs for several classes
12 # @deprecated
ffa83a43 » sam
2008-06-25 Fixed autotest support, corrected some issues with reloading models.
13 def self.append_inclusions(*inclusions)
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
14 warn "DataMapper::Resource.append_inclusions is deprecated, use DataMapper::Model.append_inclusions instead (#{caller[0]})"
76a7974d » Dan Kubb
2009-02-13 Minor code reformatting
15 Model.append_inclusions(*inclusions)
ffa83a43 » sam
2008-06-25 Fixed autotest support, corrected some issues with reloading models.
16 end
7e45d1a5 » Dan Kubb
2008-06-27 Stripped whitespace with "sake strip"
17
27c926a0 » dkubb
2009-04-20 Stubbed out @api and other docs for several classes
18 # @deprecated
ffa83a43 » sam
2008-06-25 Fixed autotest support, corrected some issues with reloading models.
19 def self.extra_inclusions
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
20 warn "DataMapper::Resource.extra_inclusions is deprecated, use DataMapper::Model.extra_inclusions instead (#{caller[0]})"
76a7974d » Dan Kubb
2009-02-13 Minor code reformatting
21 Model.extra_inclusions
22 end
23
27c926a0 » dkubb
2009-04-20 Stubbed out @api and other docs for several classes
24 # @deprecated
76a7974d » Dan Kubb
2009-02-13 Minor code reformatting
25 def self.descendants
491bd1e1 » dbussink
2009-06-09 Improve deprecation messages I encountered during upgrading to 0.10
26 warn "DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead (#{caller[0]})"
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
27 Model.descendants
ffa83a43 » sam
2008-06-25 Fixed autotest support, corrected some issues with reloading models.
28 end
7e45d1a5 » Dan Kubb
2008-06-27 Stripped whitespace with "sake strip"
29
a7f73e0d » dkubb
2010-02-23 Added #raise_on_save_failure to Model metaclass, Model, and Resource
30 # Return if Resource#save should raise an exception on save failures (per-resource)
31 #
32 # This delegates to model.raise_on_save_failure by default.
33 #
34 # user.raise_on_save_failure # => false
35 #
36 # @return [Boolean]
37 # true if a failure in Resource#save should raise an exception
38 #
39 # @api public
40 def raise_on_save_failure
41 if defined?(@raise_on_save_failure)
42 @raise_on_save_failure
43 else
44 model.raise_on_save_failure
45 end
46 end
47
48 # Specify if Resource#save should raise an exception on save failures (per-resource)
49 #
50 # @param [Boolean]
51 # a boolean that if true will cause Resource#save to raise an exception
52 #
53 # @return [Boolean]
54 # true if a failure in Resource#save should raise an exception
55 #
56 # @api public
57 def raise_on_save_failure=(raise_on_save_failure)
58 @raise_on_save_failure = raise_on_save_failure
59 end
60
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
61 # Deprecated API for updating attributes and saving Resource
62 #
63 # @see #update
64 #
67fbf33a » dkubb
2009-08-17 Tagged Resource#update_attributes as deprecated
65 # @deprecated
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
66 def update_attributes(attributes = {}, *allowed)
2df32a6f » dkubb
2010-02-05 Fixed error in Resource#update_attributes
67 model = self.model
68 call_stack = caller[0]
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
69
2df32a6f » dkubb
2010-02-05 Fixed error in Resource#update_attributes
70 warn "#{model}#update_attributes is deprecated, use #{model}#update instead (#{call_stack})"
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
71
72 if allowed.any?
73 warn "specifying allowed in #{model}#update_attributes is deprecated, " \
2df32a6f » dkubb
2010-02-05 Fixed error in Resource#update_attributes
74 "use Hash#only to filter the attributes in the caller (#{call_stack})"
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
75 attributes = attributes.only(*allowed)
76 end
77
88f882a7 » dkubb
2009-11-20 Removed most reek "Duplication" warnings
78 assert_update_clean_only(:update_attributes)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
79 update(attributes)
80 end
81
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
82 # Makes sure a class gets all the methods when it includes Resource
c53a6290 » Dan Kubb
2008-06-01 Updated property type to use Integer instead of Fixnum internally
83 #
c5ecb84e » Adam French
2008-06-19 moving/rewriting docs in newest yard style
84 # @api private
74ef6205 » Dan Kubb
2008-05-10 Modules now push themselves into Resource rather than it pulling them
85 def self.included(model)
e41f6abc » Dan Kubb
2008-06-18 Renamed Resource::ClassMethods to Model
86 model.extend Model
748b8d90 » Martin Kihlgren
2008-04-18 Enabled DataMapper::Resource to know what classes have included it.
87 end
88
f0c4d00d » Dan Kubb
2008-11-17 Updated @api tags for Resource methods
89 # @api public
73614673 » michaelklishin
2009-03-20 Use alias_method so Emacs' ruby-mode don't consider 'class' a new ind…
90 alias_method :model, :class
6db24c75 » Dan Kubb
2008-04-04 Small performance optimizations
91
7b16e8ea » dkubb
2010-04-06 Set Resource#persisted_state to default to a transient state
92 # Get the persisted state for the resource
93 #
94 # @return [Resource::State]
95 # the current persisted state for the resource
96 #
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
97 # @api private
98 def persisted_state
7b16e8ea » dkubb
2010-04-06 Set Resource#persisted_state to default to a transient state
99 @_state ||= Resource::State::Transient.new(self)
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
100 end
101
7b16e8ea » dkubb
2010-04-06 Set Resource#persisted_state to default to a transient state
102 # Set the persisted state for the resource
103 #
104 # @param [Resource::State]
105 # the new persisted state for the resource
106 #
107 # @return [undefined]
108 #
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
109 # @api private
110 def persisted_state=(state)
111 @_state = state
112 end
113
7b16e8ea » dkubb
2010-04-06 Set Resource#persisted_state to default to a transient state
114 # Test if the persisted state is set
115 #
116 # @return [Boolean]
117 # true if the persisted state is set
118 #
119 # @api private
120 def persisted_state?
121 defined?(@_state) ? true : false
122 end
123
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
124 # Repository this resource belongs to in the context of this collection
125 # or of the resource's class.
126 #
127 # @return [Repository]
128 # the respository this resource belongs to, in the context of
129 # a collection OR in the instance's Model's context
130 #
131 # @api semipublic
132 def repository
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
133 # only set @_repository explicitly when persisted
134 defined?(@_repository) ? @_repository : model.repository
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
135 end
136
137 # Retrieve the key(s) for this resource.
138 #
139 # This always returns the persisted key value,
140 # even if the key is changed and not yet persisted.
141 # This is done so all relations still work.
142 #
143 # @return [Array(Key)]
144 # the key(s) identifying this resource
145 #
146 # @api public
147 def key
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
148 return @_key if defined?(@_key)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
149
a1057121 » dkubb
2009-10-23 Added PropertySet#valid? and refactored code that tests key validity
150 model_key = model.key(repository_name)
151
152 key = model_key.map do |property|
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
153 original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil)
154 end
155
a1057121 » dkubb
2009-10-23 Added PropertySet#valid? and refactored code that tests key validity
156 # only memoize a valid key
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
157 @_key = key if model_key.valid?(key)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
158 end
159
160 # Checks if this Resource instance is new
161 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
162 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
163 # true if the resource is new and not saved
164 #
165 # @api public
166 def new?
21f07a44 » dkubb
2010-04-06 Do not inherit State::Immutable from State::Transient
167 persisted_state.kind_of?(State::Transient)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
168 end
169
170 # Checks if this Resource instance is saved
171 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
172 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
173 # true if the resource has been saved
174 #
175 # @api public
176 def saved?
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
177 persisted_state.kind_of?(State::Persisted)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
178 end
179
19baffc9 » dkubb
2009-09-20 Added Resource#destroyed?
180 # Checks if this Resource instance is destroyed
181 #
182 # @return [Boolean]
183 # true if the resource has been destroyed
184 #
185 # @api public
186 def destroyed?
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
187 readonly? && !key.nil?
19baffc9 » dkubb
2009-09-20 Added Resource#destroyed?
188 end
189
5dc4838a » dkubb
2009-08-18 Updated Collection#update and #update! to raise an exception when cal…
190 # Checks if the resource has no changes to save
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
191 #
192 # @return [Boolean]
193 # true if the resource may not be persisted
194 #
195 # @api public
196 def clean?
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
197 persisted_state.kind_of?(State::Clean) || persisted_state.kind_of?(State::Immutable)
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
198 end
199
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
200 # Checks if the resource has unsaved changes
201 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
202 # @return [Boolean]
203 # true if resource may be persisted
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
204 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
205 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
206 def dirty?
6a6add48 » dkubb
2009-10-26 Ensure Collection#first and Collection#last always return the same in…
207 run_once(true) do
208 dirty_self? || dirty_parents? || dirty_children?
209 end
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
210 end
211
88fa9a64 » dkubb
2009-10-21 Added Resource#readonly?
212 # Checks if this Resource instance is readonly
213 #
214 # @return [Boolean]
215 # true if the resource cannot be persisted
216 #
217 # @api public
218 def readonly?
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
219 persisted_state.kind_of?(State::Immutable)
88fa9a64 » dkubb
2009-10-21 Added Resource#readonly?
220 end
221
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
222 # Returns the value of the attribute.
223 #
25e6fe5d » emmanuel
2008-11-20 Began updating Resource API docs to YARD format (still in progress).
224 # Do not read from instance variables directly, but use this method.
225 # This method handles lazy loading the attribute and returning of
226 # defaults if nessesary.
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
227 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
228 # @example
229 # class Foo
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
230 # include DataMapper::Resource
231 #
232 # property :first_name, String
76a7974d » Dan Kubb
2009-02-13 Minor code reformatting
233 # property :last_name, String
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
234 #
235 # def full_name
236 # "#{attribute_get(:first_name)} #{attribute_get(:last_name)}"
237 # end
238 #
239 # # using the shorter syntax
240 # def name_for_address_book
241 # "#{last_name}, #{first_name}"
242 # end
243 # end
244 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
245 # @param [Symbol] name
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
246 # name of attribute to retrieve
25e6fe5d » emmanuel
2008-11-20 Began updating Resource API docs to YARD format (still in progress).
247 #
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
248 # @return [Object]
249 # the value stored at that given attribute
250 # (nil if none, and default if necessary)
25e6fe5d » emmanuel
2008-11-20 Began updating Resource API docs to YARD format (still in progress).
251 #
f0c4d00d » Dan Kubb
2008-11-17 Updated @api tags for Resource methods
252 # @api public
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
253 def attribute_get(name)
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
254 persisted_state.get(properties[name])
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
255 end
256
930c01c4 » dkubb
2010-09-07 Changed usage of alias to alias_method
257 alias_method :[], :attribute_get
1c119a24 » Dan Kubb
2009-02-05 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
258
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
259 # Sets the value of the attribute and marks the attribute as dirty
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
260 # if it has been changed so that it may be saved. Do not set from
261 # instance variables directly, but use this method. This method
25e6fe5d » emmanuel
2008-11-20 Began updating Resource API docs to YARD format (still in progress).
262 # handles the lazy loading the property and returning of defaults
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
263 # if nessesary.
264 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
265 # @example
266 # class Foo
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
267 # include DataMapper::Resource
268 #
269 # property :first_name, String
76a7974d » Dan Kubb
2009-02-13 Minor code reformatting
270 # property :last_name, String
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
271 #
272 # def full_name(name)
273 # name = name.split(' ')
274 # attribute_set(:first_name, name[0])
275 # attribute_set(:last_name, name[1])
276 # end
277 #
278 # # using the shorter syntax
279 # def name_from_address_book(name)
280 # name = name.split(', ')
281 # first_name = name[1]
282 # last_name = name[0]
283 # end
284 # end
285 #
2e14f815 » Emmanuel Gomez
2008-11-21 Small cleanup of YARD doc formatting in Resource.
286 # @param [Symbol] name
287 # name of attribute to set
288 # @param [Object] value
289 # value to store
25e6fe5d » emmanuel
2008-11-20 Began updating Resource API docs to YARD format (still in progress).
290 #
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
291 # @return [undefined]
25e6fe5d » emmanuel
2008-11-20 Began updating Resource API docs to YARD format (still in progress).
292 #
f0c4d00d » Dan Kubb
2008-11-17 Updated @api tags for Resource methods
293 # @api public
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
294 def attribute_set(name, value)
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
295 self.persisted_state = persisted_state.set(properties[name], value)
fa3f914b » Dan Kubb
2008-06-16 Updated Resource#attribute_get and #attribute_set be public again
296 end
297
930c01c4 » dkubb
2010-09-07 Changed usage of alias to alias_method
298 alias_method :[]=, :attribute_set
1c119a24 » Dan Kubb
2009-02-05 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
299
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
300 # Gets all the attributes of the Resource instance
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
301 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
302 # @param [Symbol] key_on
303 # Use this attribute of the Property as keys.
304 # defaults to :name. :field is useful for adapters
305 # :property or nil use the actual Property object.
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
306 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
307 # @return [Hash]
308 # All the attributes
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
309 #
c5ecb84e » Adam French
2008-06-19 moving/rewriting docs in newest yard style
310 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
311 def attributes(key_on = :name)
312 attributes = {}
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
313
314 lazy_load(properties)
315 fields.each do |property|
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
316 if model.public_method_defined?(name = property.name)
317 key = case key_on
318 when :name then name
319 when :field then property.field
320 else property
321 end
b050d8f0 » Dan Kubb
2009-01-15 Unrolled if/unless conditions to allow easier coverage testing
322
c1e78c6c » dkubb
2009-10-06 Use Resource#__send__ instead of #send in case it is overridden
323 attributes[key] = __send__(name)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
324 end
b050d8f0 » Dan Kubb
2009-01-15 Unrolled if/unless conditions to allow easier coverage testing
325 end
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
326
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
327 attributes
e5847b99 » Dan Kubb
2008-04-28 Updated Collection to implement an interface similar to Array
328 end
c53a6290 » Dan Kubb
2008-06-01 Updated property type to use Integer instead of Fixnum internally
329
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
330 # Assign values to multiple attributes in one call (mass assignment)
0cdf41ac » Dan Kubb
2008-11-24 Updated Resource#eql? to be strict match
331 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
332 # @param [Hash] attributes
333 # names and values of attributes to assign
0cdf41ac » Dan Kubb
2008-11-24 Updated Resource#eql? to be strict match
334 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
335 # @return [Hash]
336 # names and values of attributes assigned
0cdf41ac » Dan Kubb
2008-11-24 Updated Resource#eql? to be strict match
337 #
338 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
339 def attributes=(attributes)
88f882a7 » dkubb
2009-11-20 Removed most reek "Duplication" warnings
340 model = self.model
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
341 attributes.each do |name, value|
342 case name
343 when String, Symbol
344 if model.public_method_defined?(setter = "#{name}=")
c1e78c6c » dkubb
2009-10-06 Use Resource#__send__ instead of #send in case it is overridden
345 __send__(setter, value)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
346 else
978c81ad » dkubb
2009-07-01 Minor update to error message for unknown attribute in Resource#attri…
347 raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}"
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
348 end
349 when Associations::Relationship, Property
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
350 self.persisted_state = persisted_state.set(name, value)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
351 end
b050d8f0 » Dan Kubb
2009-01-15 Unrolled if/unless conditions to allow easier coverage testing
352 end
0cdf41ac » Dan Kubb
2008-11-24 Updated Resource#eql? to be strict match
353 end
e5847b99 » Dan Kubb
2008-04-28 Updated Collection to implement an interface similar to Array
354
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
355 # Reloads association and all child association
238682e3 » Dan Kubb
2008-11-20 Added Resource#<=>
356 #
6f574839 » dkubb
2009-11-06 Updated Resource#reload to clear non-key property and relationship ivars
357 # This is accomplished by resetting the Resource key to it's
358 # original value, and then removing all the ivars for properties
359 # and relationships. On the next access of those ivars, the
360 # resource will eager load what it needs. While this is more of
361 # a lazy reload, it should result is more consistent behavior
362 # since no cached results will remain from the initial load.
363 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
364 # @return [Resource]
365 # the receiver, the current Resource instance
238682e3 » Dan Kubb
2008-11-20 Added Resource#<=>
366 #
367 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
368 def reload
6f574839 » dkubb
2009-11-06 Updated Resource#reload to clear non-key property and relationship ivars
369 if key
370 reset_key
371 clear_subjects
238682e3 » Dan Kubb
2008-11-20 Added Resource#<=>
372 end
373
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
374 self.persisted_state = persisted_state.rollback
375
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
376 self
7b6845d5 » Dan Kubb
2009-02-08 Minor code refactoring
377 end
378
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
379 # Updates attributes and saves this Resource instance
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
380 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
381 # @param [Hash] attributes
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
382 # attributes to be updated
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
383 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
384 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
385 # true if resource and storage state match
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
386 #
c5ecb84e » Adam French
2008-06-19 moving/rewriting docs in newest yard style
387 # @api public
256d69ba » dkubb
2010-02-05 Removed default attributes from #update and #update! in Resource and …
388 def update(attributes)
639f435e » dkubb
2009-10-22 Minor simplifications of Resource
389 assert_update_clean_only(:update)
390 self.attributes = attributes
391 save
d409c1f2 » sam
2008-03-15 Added and specced LoadedSet.
392 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
393
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
394 # Updates attributes and saves this Resource instance, bypassing hooks
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
395 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
396 # @param [Hash] attributes
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
397 # attributes to be updated
dfac1c52 » dbussink
2008-11-12 Add more shared Resource specs
398 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
399 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
400 # true if resource and storage state match
dfac1c52 » dbussink
2008-11-12 Add more shared Resource specs
401 #
402 # @api public
256d69ba » dkubb
2010-02-05 Removed default attributes from #update and #update! in Resource and …
403 def update!(attributes)
d85c0af6 » dkubb
2009-08-16 Updated Resource#assert_update_clean_only to include the class/method…
404 assert_update_clean_only(:update!)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
405 self.attributes = attributes
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
406 save!
a519fb49 » sam
2008-03-23 CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
407 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
408
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
409 # Save the instance and loaded, dirty associations to the data-store
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
410 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
411 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
412 # true if Resource instance and all associations were saved
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
413 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
414 # @api public
1f9eed1d » dkubb
2009-10-25 Refactored Resource#save and Resource#dirty to handle circular depend…
415 def save
7b1b917f » dkubb
2009-10-26 Moved assertions closer to the methods they should be called in
416 assert_not_destroyed(:save)
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
417 retval = _save
418 assert_save_successful(:save, retval)
419 retval
041d9f17 » sam
2008-03-23 DataObjectAdapter sqlite3 integration spec passes for #create.
420 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
421
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
422 # Save the instance and loaded, dirty associations to the data-store, bypassing hooks
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
423 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
424 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
425 # true if Resource instance and all associations were saved
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
426 #
c5ecb84e » Adam French
2008-06-19 moving/rewriting docs in newest yard style
427 # @api public
1f9eed1d » dkubb
2009-10-25 Refactored Resource#save and Resource#dirty to handle circular depend…
428 def save!
7b1b917f » dkubb
2009-10-26 Moved assertions closer to the methods they should be called in
429 assert_not_destroyed(:save!)
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
430 retval = _save(false)
431 assert_save_successful(:save!, retval)
432 retval
ece0d18c » somebee
2008-05-23 some small convenient changes. added possibility for iterating throug…
433 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
434
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
435 # Destroy the instance, remove it from the repository
4d8e324a » Dan Kubb
2009-01-29 Added Resource#new? and Resource#saved?
436 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
437 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
438 # true if resource was destroyed
4d8e324a » Dan Kubb
2009-01-29 Added Resource#new? and Resource#saved?
439 #
440 # @api public
639f435e » dkubb
2009-10-22 Minor simplifications of Resource
441 def destroy
c65fd076 » dkubb
2010-02-04 Fixed paranoid types
442 return true if destroyed?
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
443 catch :halt do
444 before_destroy_hook
f5f25999 » dkubb
2010-05-28 Refactored resource destruction
445 _destroy
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
446 after_destroy_hook
447 end
f5f25999 » dkubb
2010-05-28 Refactored resource destruction
448 destroyed?
4d8e324a » Dan Kubb
2009-01-29 Added Resource#new? and Resource#saved?
449 end
450
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
451 # Destroy the instance, remove it from the repository, bypassing hooks
4d8e324a » Dan Kubb
2009-01-29 Added Resource#new? and Resource#saved?
452 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
453 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
454 # true if resource was destroyed
805e402f » myabc
2008-05-18 YARD conversion for dm-core
455 #
c5ecb84e » Adam French
2008-06-19 moving/rewriting docs in newest yard style
456 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
457 def destroy!
88f882a7 » dkubb
2009-11-20 Removed most reek "Duplication" warnings
458 return true if destroyed?
c65fd076 » dkubb
2010-02-04 Fixed paranoid types
459 _destroy(false)
f5f25999 » dkubb
2010-05-28 Refactored resource destruction
460 destroyed?
320fb840 » Bauerpauer
2008-03-23 Added a couple api-check specs. Restored Resource#new_record?. Made t…
461 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
462
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
463 # Compares another Resource for equality
40fcf73c » Dan Kubb
2008-11-26 Stripped whitespace
464 #
567d41c1 » dkubb
2009-10-19 Updated Resource#== to not test the type
465 # Resource is equal to +other+ if they are the same object
466 # (identical object_id) or if they are both of the *same model* and
467 # all of their attributes are equivalent
c53a6290 » Dan Kubb
2008-06-01 Updated property type to use Integer instead of Fixnum internally
468 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
469 # @param [Resource] other
470 # the other Resource to compare with
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
471 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
472 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
473 # true if they are equal, false if not
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
474 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
475 # @api public
476 def eql?(other)
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
477 return true if equal?(other)
30bda1f4 » dkubb
2009-09-12 Simplified comparison methods
478 instance_of?(other.class) && cmp?(other, :eql?)
e58ed2eb » sam
2008-03-03 Lots and lots of changes to move things over to the new Resource modu…
479 end
1260c126 » Dan Kubb
2008-05-08 Updated Resource#initialize to always validate the resource
480
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
481 # Compares another Resource for equivalency
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
482 #
567d41c1 » dkubb
2009-10-19 Updated Resource#== to not test the type
483 # Resource is equivalent to +other+ if they are the same object
484 # (identical object_id) or all of their attribute are equivalent
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
485 #
486 # @param [Resource] other
487 # the other Resource to compare with
488 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
489 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
490 # true if they are equivalent, false if not
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
491 #
d3183e6f » Dan Kubb
2008-11-19 Deprecated Resource#update_attributes and renamed as Resource#update
492 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
493 def ==(other)
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
494 return true if equal?(other)
23e7831b » dkubb
2010-04-23 Simplified Resource#== further
495 return false unless other.kind_of?(Resource) && model.base_model.equal?(other.model.base_model)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
496 cmp?(other, :==)
d3183e6f » Dan Kubb
2008-11-19 Deprecated Resource#update_attributes and renamed as Resource#update
497 end
498
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
499 # Compares two Resources to allow them to be sorted
72db3851 » advany
2008-05-19 update_attributes should save model
500 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
501 # @param [Resource] other
502 # The other Resource to compare with
78daf516 » advany
2008-05-30 add docs to DataMapper::Resource
503 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
504 # @return [Integer]
505 # Return 0 if Resources should be sorted as the same, -1 if the
506 # other Resource should be after self, and 1 if the other Resource
507 # should be before self
72db3851 » advany
2008-05-19 update_attributes should save model
508 #
87081193 » myabc
2008-05-20 Convert docs to YARD-style
509 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
510 def <=>(other)
88f882a7 » dkubb
2009-11-20 Removed most reek "Duplication" warnings
511 model = self.model
5bfb4f34 » dkubb
2009-06-15 Allow instances of the same base model to be comparable
512 unless other.kind_of?(model.base_model)
f0b11d6b » dkubb
2010-04-23 Do not assume the other object responds to #model
513 raise ArgumentError, "Cannot compare a #{other.class} instance with a #{model} instance"
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
514 end
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
515 model.default_order(repository_name).each do |direction|
e2121001 » dkubb
2009-06-09 Use Query::Sort when sorting resources
516 cmp = direction.get(self) <=> direction.get(other)
9207bb4f » dkubb
2010-02-02 Minor simplification to Resource#<=>
517 return cmp if cmp.nonzero?
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
518 end
9207bb4f » dkubb
2010-02-02 Minor simplification to Resource#<=>
519 0
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
520 end
d3183e6f » Dan Kubb
2008-11-19 Deprecated Resource#update_attributes and renamed as Resource#update
521
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
522 # Returns hash value of the object.
523 # Two objects with the same hash value assumed equal (using eql? method)
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
524 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
525 # DataMapper resources are equal when their models have the same hash
526 # and they have the same set of properties
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
527 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
528 # When used as key in a Hash or Hash subclass, objects are compared
529 # by eql? and thus hash value has direct effect on lookup
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
530 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
531 # @api private
532 def hash
533 key.hash
1019ac35 » sam
2008-05-08 Added key validation to Resources, and added Resource#update_attributes.
534 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
535
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
536 # Get a Human-readable representation of this Resource instance
45bbaf16 » Dan Kubb
2008-11-23 Group Resource#save and Resource#destroy near other persistence methods
537 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
538 # Foo.new #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>
45bbaf16 » Dan Kubb
2008-11-23 Group Resource#save and Resource#destroy near other persistence methods
539 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
540 # @return [String]
541 # Human-readable representation of this Resource instance
45bbaf16 » Dan Kubb
2008-11-23 Group Resource#save and Resource#destroy near other persistence methods
542 #
543 # @api public
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
544 def inspect
545 # TODO: display relationship values
546 attrs = properties.map do |property|
547 value = if new? || property.loaded?(self)
548 property.get!(self).inspect
549 else
550 '<not loaded>'
551 end
552
553 "#{property.instance_variable_name}=#{value}"
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
554 end
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
555
556 "#<#{model.name} #{attrs.join(' ')}>"
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
557 end
45bbaf16 » Dan Kubb
2008-11-23 Group Resource#save and Resource#destroy near other persistence methods
558
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
559 # Hash of original values of attributes that have unsaved changes
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
560 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
561 # @return [Hash]
562 # original values of attributes that have unsaved changes
563 #
564 # @api semipublic
565 def original_attributes
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
566 if persisted_state.respond_to?(:original_attributes)
ccea2115 » dkubb
2010-04-15 Resource#original_attributes is immutable
567 persisted_state.original_attributes.dup.freeze
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
568 else
ccea2115 » dkubb
2010-04-15 Resource#original_attributes is immutable
569 {}.freeze
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
570 end
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
571 end
572
573 # Checks if an attribute has been loaded from the repository
574 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
575 # @example
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
576 # class Foo
577 # include DataMapper::Resource
578 #
579 # property :name, String
580 # property :description, Text, :lazy => false
581 # end
582 #
583 # Foo.new.attribute_loaded?(:description) #=> false
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
584 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
585 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
586 # true if ivar +name+ has been loaded
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
587 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
588 # @return [Boolean]
589 # true if ivar +name+ has been loaded
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
590 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
591 # @api private
592 def attribute_loaded?(name)
593 properties[name].loaded?(self)
45bbaf16 » Dan Kubb
2008-11-23 Group Resource#save and Resource#destroy near other persistence methods
594 end
595
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
596 # Checks if an attribute has unsaved changes
597 #
598 # @param [Symbol] name
599 # name of attribute to check for unsaved changes
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
600 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
601 # @return [Boolean]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
602 # true if attribute has unsaved changes
65c11743 » dkubb
2009-05-15 Updated Collection#update! and #destroy! to work with limited collect…
603 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
604 # @api semipublic
605 def attribute_dirty?(name)
606 dirty_attributes.key?(properties[name])
45bbaf16 » Dan Kubb
2008-11-23 Group Resource#save and Resource#destroy near other persistence methods
607 end
608
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
609 # Hash of attributes that have unsaved changes
f132afbe » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
610 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
611 # @return [Hash]
612 # attributes that have unsaved changes
f132afbe » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
613 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
614 # @api semipublic
615 def dirty_attributes
616 dirty_attributes = {}
617
618 original_attributes.each_key do |property|
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
619 next unless property.respond_to?(:value)
7dfa9bef » dkubb
2010-04-21 Deprecated Property#value in favor of Property#dump
620 dirty_attributes[property] = property.dump(property.get!(self))
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
621 end
622
623 dirty_attributes
e3ad4a21 » Martin Kihlgren
2008-04-25 Improved the transactional system greatly and introduced two-phase
624 end
625
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
626 # Returns the Collection the Resource is associated with
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
627 #
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
628 # @return [nil]
629 # nil if this is a new record
630 # @return [Collection]
631 # a Collection that self belongs to
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
632 #
633 # @api private
634 def collection
ef52c839 » dbussink
2010-10-24 Don't try to do collection matching on using the related collection
635 return @_collection if @_collection || new? || readonly?
cdb918f4 » dkubb
2009-10-23 Refactored SEL behavior of Collections
636 collection_for_self
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
637 end
638
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
639 # Associates a Resource to a Collection
640 #
641 # @param [Collection, nil] collection
642 # the collection to associate the resource with
643 #
644 # @return [nil]
645 # nil if this is a new record
646 # @return [Collection]
647 # a Collection that self belongs to
648 #
649 # @api private
650 def collection=(collection)
651 @_collection = collection
652 end
653
e41cc420 » dkubb
2009-11-18 Updated Resource#query to be semipublic
654 # Return a collection including the current resource only
655 #
656 # @return [Collection]
657 # a collection containing self
658 #
659 # @api private
660 def collection_for_self
661 Collection.new(query, [ self ])
662 end
663
664 # Returns a Query that will match the resource
665 #
666 # @return [Query]
667 # Query that will match the resource
668 #
669 # @api semipublic
670 def query
bcb61908 » shanna
2010-04-12 Adapters act as query factory
671 repository.new_query(model, :fields => fields, :conditions => conditions)
e41cc420 » dkubb
2009-11-18 Updated Resource#query to be semipublic
672 end
673
06e66e6f » Dan Kubb
2008-11-20 Refactored Resource resetting code after destruction
674 protected
ec84ce4b » myabc
2008-06-20 Stripped whitespace with "sake strip"
675
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
676 # Method for hooking callbacks before resource saving
a108799b » dkubb
2009-06-13 Delegate Resource#save and #update to #save! and #update!
677 #
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
678 # @return [undefined]
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
679 #
dc22e45e » dkubb
2009-05-16 Updated ManyToMany::Collection#update! to act more like Collection#up…
680 # @api private
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
681 def before_save_hook
682 execute_hooks_for(:before, :save)
e58ed2eb » sam
2008-03-03 Lots and lots of changes to move things over to the new Resource modu…
683 end
15987f0d » david
2008-03-28 Merge branch 'master' of git@github.com:sam/dm-core
684
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
685 # Method for hooking callbacks after resource saving
ee967c0b » michaelklishin
2009-03-20 Document DataMapper::Resource#_update
686 #
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
687 # @return [undefined]
c031e9fe » michaelklishin
2009-03-20 Add returned value to documentation of DataMapper::Resource#_update
688 #
dc22e45e » dkubb
2009-05-16 Updated ManyToMany::Collection#update! to act more like Collection#up…
689 # @api private
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
690 def after_save_hook
691 execute_hooks_for(:after, :save)
bc3dd47f » Dan Kubb
2008-12-27 Initial commit of new Many To Many Relationship
692 end
693
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
694 # Method for hooking callbacks before resource creation
c65fd076 » dkubb
2010-02-04 Fixed paranoid types
695 #
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
696 # @return [undefined]
697 #
698 # @api private
699 def before_create_hook
700 execute_hooks_for(:before, :create)
701 end
702
703 # Method for hooking callbacks after resource creation
704 #
705 # @return [undefined]
706 #
707 # @api private
708 def after_create_hook
709 execute_hooks_for(:after, :create)
710 end
711
712 # Method for hooking callbacks before resource updating
713 #
714 # @return [undefined]
715 #
716 # @api private
717 def before_update_hook
718 execute_hooks_for(:before, :update)
719 end
720
721 # Method for hooking callbacks after resource updating
722 #
723 # @return [undefined]
724 #
725 # @api private
726 def after_update_hook
727 execute_hooks_for(:after, :update)
728 end
729
730 # Method for hooking callbacks before resource destruction
731 #
732 # @return [undefined]
c65fd076 » dkubb
2010-02-04 Fixed paranoid types
733 #
734 # @api private
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
735 def before_destroy_hook
736 execute_hooks_for(:before, :destroy)
737 end
738
739 # Method for hooking callbacks after resource destruction
740 #
741 # @return [undefined]
742 #
743 # @api private
744 def after_destroy_hook
745 execute_hooks_for(:after, :destroy)
c65fd076 » dkubb
2010-02-04 Fixed paranoid types
746 end
747
dc22e45e » dkubb
2009-05-16 Updated ManyToMany::Collection#update! to act more like Collection#up…
748 private
749
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
750 # Initialize a new instance of this Resource using the provided values
751 #
ba3d0cac » dkubb
2009-07-09 Pass through arguments/block in Model#new to allow custom #initialize
752 # @param [Hash] attributes
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
753 # attribute values to use for the new instance
754 #
ba3d0cac » dkubb
2009-07-09 Pass through arguments/block in Model#new to allow custom #initialize
755 # @return [Hash]
756 # attribute values used in the new instance
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
757 #
758 # @api public
125fe507 » dbussink
2010-07-15 Performance improvement for initializing a resource without arguments
759 def initialize(attributes = nil) # :nodoc:
760 self.attributes = attributes if attributes
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
761 end
762
763 # @api private
764 def initialize_copy(original)
765 instance_variables.each do |ivar|
766 instance_variable_set(ivar, instance_variable_get(ivar).try_dup)
767 end
768
769 self.persisted_state = persisted_state.class.new(self)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
770 end
771
bb3b8ee8 » michaelklishin
2009-03-20 Document DataMapper::Resource#repository_name
772 # Returns name of the repository this object
773 # was loaded from
774 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
775 # @return [String]
776 # name of the repository this object was loaded from
bb3b8ee8 » michaelklishin
2009-03-20 Document DataMapper::Resource#repository_name
777 #
8a1894df » Dan Kubb
2009-02-13 Updated specs to pass with all adapters, even the Yaml adapter
778 # @api private
779 def repository_name
780 repository.name
781 end
782
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
783 # Gets this instance's Model's properties
77c0e333 » Emmanuel Gomez
2008-11-28 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
784 #
785 # @return [Array(Property)]
786 # List of this Resource's Model's properties
787 #
06e66e6f » Dan Kubb
2008-11-20 Refactored Resource resetting code after destruction
788 # @api private
789 def properties
8a1894df » Dan Kubb
2009-02-13 Updated specs to pass with all adapters, even the Yaml adapter
790 model.properties(repository_name)
06e66e6f » Dan Kubb
2008-11-20 Refactored Resource resetting code after destruction
791 end
792
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
793 # Gets this instance's Model's relationships
77c0e333 » Emmanuel Gomez
2008-11-28 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
794 #
795 # @return [Array(Associations::Relationship)]
796 # List of this instance's Model's Relationships
797 #
06e66e6f » Dan Kubb
2008-11-20 Refactored Resource resetting code after destruction
798 # @api private
799 def relationships
8a1894df » Dan Kubb
2009-02-13 Updated specs to pass with all adapters, even the Yaml adapter
800 model.relationships(repository_name)
06e66e6f » Dan Kubb
2008-11-20 Refactored Resource resetting code after destruction
801 end
802
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
803 # Returns the identity map for the model from the repository
4d319fbb » michaelklishin
2009-03-20 Document DataMapper::Resouce#identity_map
804 #
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
805 # @return [IdentityMap]
4d319fbb » michaelklishin
2009-03-20 Document DataMapper::Resouce#identity_map
806 # identity map of repository this object was loaded from
807 #
88f882a7 » dkubb
2009-11-20 Removed most reek "Duplication" warnings
808 # @api private
8a1894df » Dan Kubb
2009-02-13 Updated specs to pass with all adapters, even the Yaml adapter
809 def identity_map
810 repository.identity_map(model)
811 end
887befdd » Carl Lerche
2008-06-19 Removed Hook - It has been moved to extlib
812
88f882a7 » dkubb
2009-11-20 Removed most reek "Duplication" warnings
813 # @api private
814 def add_to_identity_map
815 identity_map[key] = self
816 end
817
818 # @api private
819 def remove_from_identity_map
820 identity_map.delete(key)
821 end
822
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
823 # Fetches all the names of the attributes that have been loaded,
824 # even if they are lazy but have been called
825 #
826 # @return [Array<Property>]
827 # names of attributes that have been loaded
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
828 #
829 # @api private
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
830 def fields
831 properties.select do |property|
832 property.loaded?(self) || (new? && property.default?)
833 end
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
834 end
835
6f574839 » dkubb
2009-11-06 Updated Resource#reload to clear non-key property and relationship ivars
836 # Reset the key to the original value
837 #
838 # @return [undefined]
839 #
840 # @api private
841 def reset_key
842 properties.key.zip(key) do |property, value|
843 property.set!(self, value)
844 end
845 end
846
847 # Remove all the ivars for properties and relationships
848 #
849 # @return [undefined]
850 #
851 # @api private
852 def clear_subjects
853 model_properties = properties
854
e97e9af2 » snusnu
2011-01-13 Always access relationships in order of definition
855 (model_properties - model_properties.key | relationships).each do |subject|
6f574839 » dkubb
2009-11-06 Updated Resource#reload to clear non-key property and relationship ivars
856 next unless subject.loaded?(self)
857 remove_instance_variable(subject.instance_variable_name)
858 end
859 end
860
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
861 # Lazy loads attributes not yet loaded
862 #
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
863 # @param [Array<Property>] properties
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
864 # the properties to reload
865 #
866 # @return [self]
867 #
868 # @api private
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
869 def lazy_load(properties)
870 eager_load(properties - fields)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
871 end
872
873 # Reloads specified attributes
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
874 #
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
875 # @param [Array<Property>] properties
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
876 # the properties to reload
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
877 #
43ce436e » emmanuel
2008-11-21 Updated Resource API docs to YARD format.
878 # @return [Resource]
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
879 # the receiver, the current Resource instance
6186835f » Dan Kubb
2008-11-23 Stripped whitespace
880 #
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
881 # @api private
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
882 def eager_load(properties)
3332db6c » dbussink
2009-12-20 Small bugfix for calling attributes on a new Resource without a Seria…
883 unless properties.empty? || key.nil? || collection.nil?
a449ec9f » dkubb
2010-04-21 Updated State specs to use a custom type for the key
884 # set an initial value to prevent recursive lazy loads
885 properties.each { |property| property.set!(self, nil) }
886
f0f86621 » dkubb
2009-10-24 Refactored Resource and Collection reloading
887 collection.reload(:fields => properties)
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
888 end
889
890 self
887befdd » Carl Lerche
2008-06-19 Removed Hook - It has been moved to extlib
891 end
0ac6b42c » Dan Kubb
2008-09-11 Stripped whitespace with sake strip
892
e41cc420 » dkubb
2009-11-18 Updated Resource#query to be semipublic
893 # Return conditions to match the Resource
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
894 #
e41cc420 » dkubb
2009-11-18 Updated Resource#query to be semipublic
895 # @return [Hash]
896 # query conditions
0ef5ad1b » michaelklishin
2009-03-20 Document DataMapper::Resource#lazy_load
897 #
f0c4d00d » Dan Kubb
2008-11-17 Updated @api tags for Resource methods
898 # @api private
e41cc420 » dkubb
2009-11-18 Updated Resource#query to be semipublic
899 def conditions
5b4be323 » dkubb
2009-11-19 Use loaded properties for Resource#conditions when no key
900 key = self.key
901 if key
902 model.key_conditions(repository, key)
903 else
904 conditions = {}
905 properties.each do |property|
906 next unless property.loaded?(self)
907 conditions[property] = property.get!(self)
908 end
909 conditions
910 end
ca8aa598 » dkubb
2009-10-15 Resource#update and Resource#destroy should be idempotent
911 end
912
2e1fd219 » dkubb
2009-06-13 Persist dirty parent resources when saving a Resource
913 # @api private
914 def parent_relationships
915 parent_relationships = []
916
e97e9af2 » snusnu
2011-01-13 Always access relationships in order of definition
917 relationships.each do |relationship|
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
918 next unless relationship.respond_to?(:resource_for)
919 set_default_value(relationship)
920 next unless relationship.loaded?(self) && relationship.get!(self)
921
2e1fd219 » dkubb
2009-06-13 Persist dirty parent resources when saving a Resource
922 parent_relationships << relationship
923 end
924
925 parent_relationships
926 end
927
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
928 # Returns loaded child relationships
cb2a5da7 » michaelklishin
2009-03-20 Document DataMapper::Resource#child_associations
929 #
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
930 # @return [Array<Associations::OneToMany::Relationship>]
cb2a5da7 » michaelklishin
2009-03-20 Document DataMapper::Resource#child_associations
931 # array of child relationships for which this resource is parent and is loaded
932 #
f0c4d00d » Dan Kubb
2008-11-17 Updated @api tags for Resource methods
933 # @api private
2e1fd219 » dkubb
2009-06-13 Persist dirty parent resources when saving a Resource
934 def child_relationships
935 child_relationships = []
9aa83de3 » Dan Kubb
2009-02-24 Refactored OneToMany and ManyToMany relationships
936
e97e9af2 » snusnu
2011-01-13 Always access relationships in order of definition
937 relationships.each do |relationship|
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
938 next unless relationship.respond_to?(:collection_for)
939 set_default_value(relationship)
940 next unless relationship.loaded?(self)
941
2e1fd219 » dkubb
2009-06-13 Persist dirty parent resources when saving a Resource
942 child_relationships << relationship
d54fbe51 » dkubb
2009-05-25 Updated ManyToMany::Collection#save to properly save intermediaries
943 end
944
2e1fd219 » dkubb
2009-06-13 Persist dirty parent resources when saving a Resource
945 many_to_many, other = child_relationships.partition do |relationship|
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
946 relationship.kind_of?(Associations::ManyToMany::Relationship)
9aa83de3 » Dan Kubb
2009-02-24 Refactored OneToMany and ManyToMany relationships
947 end
948
2e1fd219 » dkubb
2009-06-13 Persist dirty parent resources when saving a Resource
949 many_to_many + other
3684f208 » Dan Kubb
2008-06-16 Minor Resource cleanup
950 end
951
7ed6e370 » dkubb
2009-10-02 Updated Resource to be dirty when associations are dirty
952 # @api private
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
953 def parent_associations
7ed6e370 » dkubb
2009-10-02 Updated Resource to be dirty when associations are dirty
954 parent_relationships.map { |relationship| relationship.get!(self) }
955 end
956
957 # @api private
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
958 def child_associations
959 child_relationships.map { |relationship| relationship.get_collection(self) }
7ed6e370 » dkubb
2009-10-02 Updated Resource to be dirty when associations are dirty
960 end
961
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
962 # Commit the persisted state
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
963 #
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
964 # @return [undefined]
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
965 #
966 # @api private
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
967 def _persist
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
968 self.persisted_state = persisted_state.commit
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
969 end
970
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
971 # This method executes the hooks before and after resource creation
972 #
973 # @return [Boolean]
974 #
975 # @see Resource#_create
976 #
977 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
978 def create_with_hooks
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
979 catch :halt do
980 before_save_hook
981 before_create_hook
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
982 _persist
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
983 after_create_hook
984 after_save_hook
985 end
986 end
987
988 # This method executes the hooks before and after resource updating
989 #
990 # @return [Boolean]
991 #
992 # @see Resource#_update
993 #
994 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
995 def update_with_hooks
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
996 catch :halt do
997 before_save_hook
998 before_update_hook
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
999 _persist
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
1000 after_update_hook
1001 after_save_hook
1002 end
1003 end
1004
f5f25999 » dkubb
2010-05-28 Refactored resource destruction
1005 # Destroy the resource
1006 #
1007 # @return [undefined]
1008 #
1f9eed1d » dkubb
2009-10-25 Refactored Resource#save and Resource#dirty to handle circular depend…
1009 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1010 def _destroy(execute_hooks = true)
f5f25999 » dkubb
2010-05-28 Refactored resource destruction
1011 self.persisted_state = persisted_state.delete
1012 _persist
c65fd076 » dkubb
2010-02-04 Fixed paranoid types
1013 end
1014
1015 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1016 def _save(execute_hooks = true)
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1017 run_once(true) do
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1018 save_parents(execute_hooks) && save_self(execute_hooks) && save_children(execute_hooks)
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1019 end
6b9ab6d8 » dkubb
2009-10-04 A dirty sibling should not make a resource dirty
1020 end
1021
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1022 # Saves the resource
1023 #
1024 # @return [Boolean]
1025 # true if the resource was successfully saved
1026 #
1027 # @api semipublic
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1028 def save_self(execute_hooks = true)
1b766937 » dkubb
2010-02-01 Skip saving if the dirty test returns false
1029 # short-circuit if the resource is not dirty
1030 return saved? unless dirty_self?
1031
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1032 if execute_hooks
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
1033 new? ? create_with_hooks : update_with_hooks
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1034 else
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
1035 _persist
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1036 end
e63dd70e » dkubb
2010-05-28 Refactored how persistence is handled internally
1037 clean?
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1038 end
1039
1040 # Saves the parent resources
1041 #
1042 # @return [Boolean]
1043 # true if the parents were successfully saved
1044 #
1045 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1046 def save_parents(execute_hooks)
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1047 run_once(true) do
39e131fb » dkubb
2010-04-02 Make sure saving parent/children is not short circuited
1048 parent_relationships.map do |relationship|
045b9074 » solnic
2011-01-12 Support changing value of a foreign key in m:1 relationship
1049 parent = relationship.get(self)
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1050
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1051 if parent.__send__(:save_parents, execute_hooks) && parent.__send__(:save_self, execute_hooks)
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1052 relationship.set(self, parent) # set the FK values
1053 end
39e131fb » dkubb
2010-04-02 Make sure saving parent/children is not short circuited
1054 end.all?
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1055 end
1056 end
1057
1058 # Saves the children resources
1059 #
1060 # @return [Boolean]
1061 # true if the children were successfully saved
1062 #
1063 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1064 def save_children(execute_hooks)
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
1065 child_associations.map do |association|
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1066 association.__send__(execute_hooks ? :save : :save!)
39e131fb » dkubb
2010-04-02 Make sure saving parent/children is not short circuited
1067 end.all?
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1068 end
1069
1070 # Checks if the resource has unsaved changes
1071 #
1072 # @return [Boolean]
1073 # true if the resource has unsaged changes
1074 #
273f48b1 » dkubb
2010-02-02 Update Resource#dirty_self? to be semipublic
1075 # @api semipublic
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1076 def dirty_self?
1077 if original_attributes.any?
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1078 true
1079 elsif new?
1080 !model.serial.nil? || properties.any? { |property| property.default? }
1081 else
1082 false
1083 end
1084 end
1085
1086 # Checks if the parents have unsaved changes
1087 #
1088 # @return [Boolean]
1089 # true if the parents have unsaved changes
1090 #
1091 # @api private
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1092 def dirty_parents?
1093 run_once(false) do
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
1094 parent_associations.any? do |association|
1095 association.__send__(:dirty_self?) || association.__send__(:dirty_parents?)
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1096 end
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1097 end
1098 end
1099
1100 # Checks if the children have unsaved changes
1101 #
1102 # @param [Hash] resources
1103 # resources that have already been tested
1104 #
1105 # @return [Boolean]
1106 # true if the children have unsaved changes
1107 #
1108 # @api private
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1109 def dirty_children?
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
1110 child_associations.any? { |association| association.dirty? }
f5e8c5f5 » dkubb
2009-10-26 Updated saving and dirtyness internal methods to be private
1111 end
1112
9c54f280 » Dan Kubb
2009-01-31 Added Query#update specs
1113 # Return true if +other+'s is equivalent or equal to +self+'s
77c0e333 » Emmanuel Gomez
2008-11-28 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
1114 #
1115 # @param [Resource] other
1116 # The Resource whose attributes are to be compared with +self+'s
b88cab22 » Dan Kubb
2009-01-24 Added Query semipublic specs
1117 # @param [Symbol] operator
1118 # The comparison operator to use to compare the attributes
1119 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
1120 # @return [Boolean]
77c0e333 » Emmanuel Gomez
2008-11-28 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
1121 # The result of the comparison of +other+'s attributes with +self+'s
7d1befec » Dan Kubb
2008-11-25 Refactored equivalent property checks from Resource#eql? and Resource#==
1122 #
d478c135 » Dan Kubb
2009-01-24 Minor documentation update in Resource
1123 # @api private
9c54f280 » Dan Kubb
2009-01-31 Added Query#update specs
1124 def cmp?(other, operator)
020646c9 » dkubb
2010-04-23 Refactored Resource#cmp? to not use #clean?
1125 return false unless repository.send(operator, other.repository) &&
1126 key.send(operator, other.key)
03806160 » Dan Kubb
2008-12-12 Removed ManyToMany::Proxy (to be rebuilt)
1127
020646c9 » dkubb
2010-04-23 Refactored Resource#cmp? to not use #clean?
1128 if saved? && other.saved?
1129 # if dirty attributes match then they are the same resource
1130 dirty_attributes == other.dirty_attributes
1131 else
1132 # compare properties for unsaved resources
1133 properties.all? do |property|
1134 __send__(property.name).send(operator, other.__send__(property.name))
1135 end
03806160 » Dan Kubb
2008-12-12 Removed ManyToMany::Proxy (to be rebuilt)
1136 end
53c52723 » dkubb
2010-03-24 Refactored persistence layer to use a state machine
1137 end
1138
1139 # @api private
1140 def set_default_value(subject)
1141 return unless persisted_state.respond_to?(:set_default_value, true)
1142 persisted_state.__send__(:set_default_value, subject)
7d1befec » Dan Kubb
2008-11-25 Refactored equivalent property checks from Resource#eql? and Resource#==
1143 end
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
1144
28bdb271 » dkubb
2010-03-22 Split up hooks into explicit methods
1145 # Execute all the queued up hooks for a given type and name
1146 #
1147 # @param [Symbol] type
1148 # the type of hook to execute (before or after)
1149 # @param [Symbol] name
1150 # the name of the hook to execute
1151 #
1152 # @return [undefined]
1153 #
1154 # @api private
1155 def execute_hooks_for(type, name)
1156 model.hooks[name][type].each { |hook| hook.call(self) }
1157 end
1158
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
1159 # Raises an exception if #update is performed on a dirty resource
1160 #
f127bada » dkubb
2009-09-14 Minor cleanup to Resource lazy/eager loading logic
1161 # @param [Symbol] method
1162 # the name of the method to use in the exception
1163 #
1164 # @return [undefined]
1165 #
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
1166 # @raise [UpdateConflictError]
1167 # raise if the resource is dirty
1168 #
1169 # @api private
d85c0af6 » dkubb
2009-08-16 Updated Resource#assert_update_clean_only to include the class/method…
1170 def assert_update_clean_only(method)
fd88599e » dkubb
2009-10-02 Use Resource#dirty? to test for dirtyness prior to executing Resource…
1171 if dirty?
0a374fd6 » dkubb
2010-02-24 Clarified error message for calling Resource#update on a new resource
1172 raise UpdateConflictError, "#{model}##{method} cannot be called on a #{new? ? 'new' : 'dirty'} resource"
d85c0af6 » dkubb
2009-08-16 Updated Resource#assert_update_clean_only to include the class/method…
1173 end
25ace545 » dkubb
2009-08-16 Modified Resource#update and #update! to raise an exception on modifi…
1174 end
92e8a588 » dkubb
2009-10-07 Assert that Resource#save cannot be called on a destroyed resource
1175
1176 # Raises an exception if #save is performed on a destroyed resource
1177 #
1178 # @param [Symbol] method
1179 # the name of the method to use in the exception
1180 #
1181 # @return [undefined]
1182 #
1183 # @raise [PersistenceError]
1184 # raise if the resource is destroyed
1185 #
1186 # @api private
1187 def assert_not_destroyed(method)
1188 if destroyed?
1189 raise PersistenceError, "#{model}##{method} cannot be called on a destroyed resource"
1190 end
1191 end
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1192
434ef85b » dkubb
2010-02-23 Updated Resource#save and #save! to optionally raise an exception on …
1193 # Raises an exception if #save returns false
1194 #
1195 # @param [Symbol] method
1196 # the name of the method to use in the exception
1197 # @param [Boolean] save_result
1198 # the result of the #save call
1199 #
1200 # @return [undefined]
1201 #
1202 # @raise [SaveFailureError]
1203 # raise if the resource was not saved
1204 #
1205 # @api private
548c178f » dkubb
2010-04-23 Update private _create, _update, _destroy, _save to have similar nami…
1206 def assert_save_successful(method, save_retval)
1207 if save_retval != true && raise_on_save_failure
f34b5f89 » dkubb
2010-05-28 Ensure the SaveFailureError exception has a reference to the resource
1208 raise SaveFailureError.new("#{model}##{method} returned #{save_retval.inspect}, #{model} was not saved", self)
434ef85b » dkubb
2010-02-23 Updated Resource#save and #save! to optionally raise an exception on …
1209 end
1210 end
1211
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1212 # Prevent a method from being in the stack more than once
1213 #
1214 # The purpose of this method is to prevent SystemStackError from
1215 # being thrown from methods from encountering infinite recursion
1216 # when called on resources having circular dependencies.
1217 #
1218 # @param [Object] default
1219 # default return value
1220 #
1221 # @yield The block of code to run once
1222 #
1223 # @return [Object]
1224 # block return value
1225 #
1226 # @api private
1227 def run_once(default)
e886e3f1 » dkubb
2009-11-09 Changed usage of caller() into Kernel::caller()
1228 caller_method = Kernel.caller(1).first[/`([^'?!]+)[?!]?'/, 1]
337f9d05 » dkubb
2009-11-06 Prefix all Resource internal ivars with an underscore
1229 sentinel = "@_#{caller_method}_sentinel"
9115894c » dkubb
2009-10-26 Simplified SystemStackError prevention in saving and dirtyness testing
1230 return instance_variable_get(sentinel) if instance_variable_defined?(sentinel)
1231
1232 begin
1233 instance_variable_set(sentinel, default)
1234 yield
1235 ensure
1236 remove_instance_variable(sentinel)
1237 end
1238 end
0ba594da » myabc
2008-04-03 Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataM…
1239 end # module Resource
1240 end # module DataMapper
Something went wrong with that request. Please try again.