Skip to content
Newer
Older
100644 1241 lines (1117 sloc) 31.9 KB
a7f73e0 @dkubb Added #raise_on_save_failure to Model metaclass, Model, and Resource
dkubb authored Feb 23, 2010
1 # TODO: DRY up raise_on_save_failure with attr_accessor_with_default
2 # once AS branch is merged in
3
e8b4f3b @sam Importing from old repository.
sam authored Mar 1, 2008
4 module DataMapper
5 module Resource
999e375 @snusnu Adds active_support/extlib compatibility and a Gemfile
snusnu authored Feb 10, 2010
6 include DataMapper::Assertions
ecb4765 Made Resource#save chainable for easier extending in plugins
Dan Kubb authored Feb 13, 2009
7 extend Chainable
d41858a @dkubb Added DM::Deprecate::deprecate() to mark methods as deprecated
dkubb authored May 1, 2009
8 extend Deprecate
9
10 deprecate :new_record?, :new?
182e590 Minor code cleanup
Dan Kubb authored Jul 8, 2008
11
27c926a @dkubb Stubbed out @api and other docs for several classes
dkubb authored Apr 20, 2009
12 # @deprecated
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 25, 2008
13 def self.append_inclusions(*inclusions)
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
14 warn "DataMapper::Resource.append_inclusions is deprecated, use DataMapper::Model.append_inclusions instead (#{caller[0]})"
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
15 Model.append_inclusions(*inclusions)
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 26, 2008
16 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored Jun 27, 2008
17
27c926a @dkubb Stubbed out @api and other docs for several classes
dkubb authored Apr 20, 2009
18 # @deprecated
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 26, 2008
19 def self.extra_inclusions
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
20 warn "DataMapper::Resource.extra_inclusions is deprecated, use DataMapper::Model.extra_inclusions instead (#{caller[0]})"
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
21 Model.extra_inclusions
22 end
23
27c926a @dkubb Stubbed out @api and other docs for several classes
dkubb authored Apr 20, 2009
24 # @deprecated
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
25 def self.descendants
491bd1e @dbussink Improve deprecation messages I encountered during upgrading to 0.10
dbussink authored Jun 9, 2009
26 warn "DataMapper::Resource.descendants is deprecated, use DataMapper::Model.descendants instead (#{caller[0]})"
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 14, 2009
27 Model.descendants
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 26, 2008
28 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored Jun 27, 2008
29
a7f73e0 @dkubb Added #raise_on_save_failure to Model metaclass, Model, and Resource
dkubb authored Feb 23, 2010
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
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 25, 2009
61 # Deprecated API for updating attributes and saving Resource
62 #
63 # @see #update
64 #
67fbf33 @dkubb Tagged Resource#update_attributes as deprecated
dkubb authored Aug 17, 2009
65 # @deprecated
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
66 def update_attributes(attributes = {}, *allowed)
2df32a6 @dkubb Fixed error in Resource#update_attributes
dkubb authored Feb 5, 2010
67 model = self.model
68 call_stack = caller[0]
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
69
2df32a6 @dkubb Fixed error in Resource#update_attributes
dkubb authored Feb 5, 2010
70 warn "#{model}#update_attributes is deprecated, use #{model}#update instead (#{call_stack})"
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
71
72 if allowed.any?
73 warn "specifying allowed in #{model}#update_attributes is deprecated, " \
2df32a6 @dkubb Fixed error in Resource#update_attributes
dkubb authored Feb 5, 2010
74 "use Hash#only to filter the attributes in the caller (#{call_stack})"
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
75 attributes = attributes.only(*allowed)
76 end
77
88f882a @dkubb Removed most reek "Duplication" warnings
dkubb authored Nov 20, 2009
78 assert_update_clean_only(:update_attributes)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
79 update(attributes)
80 end
81
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
82 # Makes sure a class gets all the methods when it includes Resource
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 1, 2008
83 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
84 # @api private
74ef620 Modules now push themselves into Resource rather than it pulling them
Dan Kubb authored May 10, 2008
85 def self.included(model)
e41f6ab Renamed Resource::ClassMethods to Model
Dan Kubb authored Jun 18, 2008
86 model.extend Model
748b8d9 Enabled DataMapper::Resource to know what classes have included it.
Martin Kihlgren authored Apr 18, 2008
87 end
88
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
89 # @api public
7361467 @michaelklishin Use alias_method so Emacs' ruby-mode don't consider 'class' a new ind…
michaelklishin authored Mar 20, 2009
90 alias_method :model, :class
6db24c7 Small performance optimizations
Dan Kubb authored Apr 4, 2008
91
7b16e8e @dkubb Set Resource#persisted_state to default to a transient state
dkubb authored Apr 6, 2010
92 # Get the persisted state for the resource
93 #
94 # @return [Resource::State]
95 # the current persisted state for the resource
96 #
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
97 # @api private
98 def persisted_state
7b16e8e @dkubb Set Resource#persisted_state to default to a transient state
dkubb authored Apr 6, 2010
99 @_state ||= Resource::State::Transient.new(self)
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
100 end
101
7b16e8e @dkubb Set Resource#persisted_state to default to a transient state
dkubb authored Apr 6, 2010
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 #
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
109 # @api private
110 def persisted_state=(state)
111 @_state = state
112 end
113
7b16e8e @dkubb Set Resource#persisted_state to default to a transient state
dkubb authored Apr 6, 2010
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
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
133 # only set @_repository explicitly when persisted
134 defined?(@_repository) ? @_repository : model.repository
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
148 return @_key if defined?(@_key)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
149
a105712 @dkubb Added PropertySet#valid? and refactored code that tests key validity
dkubb authored Oct 23, 2009
150 model_key = model.key(repository_name)
151
152 key = model_key.map do |property|
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
153 original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil)
154 end
155
a105712 @dkubb Added PropertySet#valid? and refactored code that tests key validity
dkubb authored Oct 23, 2009
156 # only memoize a valid key
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
157 @_key = key if model_key.valid?(key)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
158 end
159
160 # Checks if this Resource instance is new
161 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
162 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
163 # true if the resource is new and not saved
164 #
165 # @api public
166 def new?
21f07a4 @dkubb Do not inherit State::Immutable from State::Transient
dkubb authored Apr 6, 2010
167 persisted_state.kind_of?(State::Transient)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
168 end
169
170 # Checks if this Resource instance is saved
171 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
172 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
173 # true if the resource has been saved
174 #
175 # @api public
176 def saved?
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
177 persisted_state.kind_of?(State::Persisted)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
178 end
179
19baffc @dkubb Added Resource#destroyed?
dkubb authored Sep 20, 2009
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?
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
187 readonly? && !key.nil?
19baffc @dkubb Added Resource#destroyed?
dkubb authored Sep 21, 2009
188 end
189
5dc4838 @dkubb Updated Collection#update and #update! to raise an exception when cal…
dkubb authored Aug 18, 2009
190 # Checks if the resource has no changes to save
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
191 #
192 # @return [Boolean]
193 # true if the resource may not be persisted
194 #
195 # @api public
196 def clean?
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
197 persisted_state.kind_of?(State::Clean) || persisted_state.kind_of?(State::Immutable)
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
198 end
199
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
200 # Checks if the resource has unsaved changes
201 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
202 # @return [Boolean]
203 # true if resource may be persisted
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
204 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
205 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
206 def dirty?
6a6add4 @dkubb Ensure Collection#first and Collection#last always return the same in…
dkubb authored Oct 26, 2009
207 run_once(true) do
208 dirty_self? || dirty_parents? || dirty_children?
209 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
210 end
211
88fa9a6 @dkubb Added Resource#readonly?
dkubb authored Oct 21, 2009
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?
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
219 persisted_state.kind_of?(State::Immutable)
88fa9a6 @dkubb Added Resource#readonly?
dkubb authored Oct 22, 2009
220 end
221
6186835 Stripped whitespace
Dan Kubb authored Nov 23, 2008
222 # Returns the value of the attribute.
223 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
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.
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 16, 2008
227 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
228 # @example
229 # class Foo
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
230 # include DataMapper::Resource
231 #
232 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
233 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
245 # @param [Symbol] name
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
246 # name of attribute to retrieve
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
247 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
248 # @return [Object]
249 # the value stored at that given attribute
250 # (nil if none, and default if necessary)
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
251 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
252 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
253 def attribute_get(name)
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
254 persisted_state.get(properties[name])
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
255 end
256
930c01c @dkubb Changed usage of alias to alias_method
dkubb authored Sep 7, 2010
257 alias_method :[], :attribute_get
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
Dan Kubb authored Feb 5, 2009
258
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
259 # Sets the value of the attribute and marks the attribute as dirty
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
262 # handles the lazy loading the property and returning of defaults
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
263 # if nessesary.
264 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
265 # @example
266 # class Foo
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
267 # include DataMapper::Resource
268 #
269 # property :first_name, String
76a7974 Minor code reformatting
Dan Kubb authored Feb 13, 2009
270 # property :last_name, String
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
286 # @param [Symbol] name
287 # name of attribute to set
288 # @param [Object] value
289 # value to store
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
290 #
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
291 # @return [undefined]
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
292 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
293 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
294 def attribute_set(name, value)
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
295 self.persisted_state = persisted_state.set(properties[name], value)
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
296 end
297
930c01c @dkubb Changed usage of alias to alias_method
dkubb authored Sep 7, 2010
298 alias_method :[]=, :attribute_set
1c119a2 Added Resource#[] and #[]= as aliases for #attribute_get and #attribu…
Dan Kubb authored Feb 5, 2009
299
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
300 # Gets all the attributes of the Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
301 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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.
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
306 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
307 # @return [Hash]
308 # All the attributes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
309 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
310 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
311 def attributes(key_on = :name)
312 attributes = {}
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
313
314 lazy_load(properties)
315 fields.each do |property|
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
322
c1e78c6 @dkubb Use Resource#__send__ instead of #send in case it is overridden
dkubb authored Oct 6, 2009
323 attributes[key] = __send__(name)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
324 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
325 end
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
326
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
327 attributes
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 28, 2008
328 end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 2, 2008
329
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
330 # Assign values to multiple attributes in one call (mass assignment)
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
331 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
332 # @param [Hash] attributes
333 # names and values of attributes to assign
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
334 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
335 # @return [Hash]
336 # names and values of attributes assigned
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
337 #
338 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
339 def attributes=(attributes)
88f882a @dkubb Removed most reek "Duplication" warnings
dkubb authored Nov 20, 2009
340 model = self.model
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
341 attributes.each do |name, value|
342 case name
343 when String, Symbol
344 if model.public_method_defined?(setter = "#{name}=")
c1e78c6 @dkubb Use Resource#__send__ instead of #send in case it is overridden
dkubb authored Oct 6, 2009
345 __send__(setter, value)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
346 else
978c81a @dkubb Minor update to error message for unknown attribute in Resource#attri…
dkubb authored Jul 1, 2009
347 raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}"
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
348 end
349 when Associations::Relationship, Property
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
350 self.persisted_state = persisted_state.set(name, value)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
351 end
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
352 end
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
353 end
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 29, 2008
354
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
355 # Reloads association and all child association
238682e Added Resource#<=>
Dan Kubb authored Nov 20, 2008
356 #
6f57483 @dkubb Updated Resource#reload to clear non-key property and relationship ivars
dkubb authored Nov 6, 2009
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 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
364 # @return [Resource]
365 # the receiver, the current Resource instance
238682e Added Resource#<=>
Dan Kubb authored Nov 21, 2008
366 #
367 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
368 def reload
6f57483 @dkubb Updated Resource#reload to clear non-key property and relationship ivars
dkubb authored Nov 7, 2009
369 if key
370 reset_key
371 clear_subjects
238682e Added Resource#<=>
Dan Kubb authored Nov 21, 2008
372 end
373
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
374 self.persisted_state = persisted_state.rollback
375
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
376 self
7b6845d Minor code refactoring
Dan Kubb authored Feb 8, 2009
377 end
378
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
379 # Updates attributes and saves this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
380 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
381 # @param [Hash] attributes
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
382 # attributes to be updated
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
383 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
384 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
385 # true if resource and storage state match
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
386 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
387 # @api public
256d69b @dkubb Removed default attributes from #update and #update! in Resource and …
dkubb authored Feb 5, 2010
388 def update(attributes)
639f435 @dkubb Minor simplifications of Resource
dkubb authored Oct 22, 2009
389 assert_update_clean_only(:update)
390 self.attributes = attributes
391 save
d409c1f @sam Added and specced LoadedSet.
sam authored Mar 15, 2008
392 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
393
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
394 # Updates attributes and saves this Resource instance, bypassing hooks
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
395 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
396 # @param [Hash] attributes
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
397 # attributes to be updated
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
398 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
399 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
400 # true if resource and storage state match
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
401 #
402 # @api public
256d69b @dkubb Removed default attributes from #update and #update! in Resource and …
dkubb authored Feb 4, 2010
403 def update!(attributes)
d85c0af @dkubb Updated Resource#assert_update_clean_only to include the class/method…
dkubb authored Aug 16, 2009
404 assert_update_clean_only(:update!)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
405 self.attributes = attributes
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
406 save!
a519fb4 @sam CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
sam authored Mar 23, 2008
407 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
408
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
409 # Save the instance and loaded, dirty associations to the data-store
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
410 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
411 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
412 # true if Resource instance and all associations were saved
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
413 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
414 # @api public
1f9eed1 @dkubb Refactored Resource#save and Resource#dirty to handle circular depend…
dkubb authored Oct 25, 2009
415 def save
7b1b917 @dkubb Moved assertions closer to the methods they should be called in
dkubb authored Oct 26, 2009
416 assert_not_destroyed(:save)
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 23, 2010
417 retval = _save
418 assert_save_successful(:save, retval)
419 retval
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored Mar 23, 2008
420 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
421
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
422 # Save the instance and loaded, dirty associations to the data-store, bypassing hooks
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
423 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
424 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
425 # true if Resource instance and all associations were saved
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
426 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
427 # @api public
1f9eed1 @dkubb Refactored Resource#save and Resource#dirty to handle circular depend…
dkubb authored Oct 26, 2009
428 def save!
7b1b917 @dkubb Moved assertions closer to the methods they should be called in
dkubb authored Oct 26, 2009
429 assert_not_destroyed(:save!)
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
430 retval = _save(false)
431 assert_save_successful(:save!, retval)
432 retval
ece0d18 @somebee some small convenient changes. added possibility for iterating throug…
somebee authored May 23, 2008
433 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
434
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
435 # Destroy the instance, remove it from the repository
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 29, 2009
436 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
437 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
438 # true if resource was destroyed
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
439 #
440 # @api public
639f435 @dkubb Minor simplifications of Resource
dkubb authored Oct 23, 2009
441 def destroy
c65fd07 @dkubb Fixed paranoid types
dkubb authored Feb 4, 2010
442 return true if destroyed?
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
443 catch :halt do
444 before_destroy_hook
f5f2599 @dkubb Refactored resource destruction
dkubb authored May 28, 2010
445 _destroy
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
446 after_destroy_hook
447 end
f5f2599 @dkubb Refactored resource destruction
dkubb authored May 29, 2010
448 destroyed?
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
449 end
450
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
451 # Destroy the instance, remove it from the repository, bypassing hooks
4d8e324 Added Resource#new? and Resource#saved?
Dan Kubb authored Jan 30, 2009
452 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
453 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
454 # true if resource was destroyed
805e402 @myabc YARD conversion for dm-core
myabc authored May 18, 2008
455 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
456 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
457 def destroy!
88f882a @dkubb Removed most reek "Duplication" warnings
dkubb authored Nov 20, 2009
458 return true if destroyed?
c65fd07 @dkubb Fixed paranoid types
dkubb authored Feb 4, 2010
459 _destroy(false)
f5f2599 @dkubb Refactored resource destruction
dkubb authored May 29, 2010
460 destroyed?
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made t…
Bauerpauer authored Mar 23, 2008
461 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
462
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
463 # Compares another Resource for equality
40fcf73 Stripped whitespace
Dan Kubb authored Nov 26, 2008
464 #
567d41c @dkubb Updated Resource#== to not test the type
dkubb authored Oct 19, 2009
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
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 2, 2008
468 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
469 # @param [Resource] other
470 # the other Resource to compare with
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
471 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
472 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
473 # true if they are equal, false if not
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
474 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
475 # @api public
476 def eql?(other)
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
477 return true if equal?(other)
30bda1f @dkubb Simplified comparison methods
dkubb authored Sep 12, 2009
478 instance_of?(other.class) && cmp?(other, :eql?)
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored Mar 3, 2008
479 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored May 8, 2008
480
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
481 # Compares another Resource for equivalency
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
482 #
567d41c @dkubb Updated Resource#== to not test the type
dkubb authored Oct 19, 2009
483 # Resource is equivalent to +other+ if they are the same object
484 # (identical object_id) or all of their attribute are equivalent
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
485 #
486 # @param [Resource] other
487 # the other Resource to compare with
488 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
489 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
490 # true if they are equivalent, false if not
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
491 #
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
492 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
493 def ==(other)
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
494 return true if equal?(other)
23e7831 @dkubb Simplified Resource#== further
dkubb authored Apr 23, 2010
495 return false unless other.kind_of?(Resource) && model.base_model.equal?(other.model.base_model)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
496 cmp?(other, :==)
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
497 end
498
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
499 # Compares two Resources to allow them to be sorted
72db385 @advany update_attributes should save model
advany authored May 19, 2008
500 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
501 # @param [Resource] other
502 # The other Resource to compare with
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
503 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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
72db385 @advany update_attributes should save model
advany authored May 19, 2008
508 #
8708119 @myabc Convert docs to YARD-style
myabc authored May 20, 2008
509 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
510 def <=>(other)
88f882a @dkubb Removed most reek "Duplication" warnings
dkubb authored Nov 20, 2009
511 model = self.model
5bfb4f3 @dkubb Allow instances of the same base model to be comparable
dkubb authored Jun 15, 2009
512 unless other.kind_of?(model.base_model)
f0b11d6 @dkubb Do not assume the other object responds to #model
dkubb authored Apr 23, 2010
513 raise ArgumentError, "Cannot compare a #{other.class} instance with a #{model} instance"
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
514 end
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
515 model.default_order(repository_name).each do |direction|
e212100 @dkubb Use Query::Sort when sorting resources
dkubb authored Jun 9, 2009
516 cmp = direction.get(self) <=> direction.get(other)
9207bb4 @dkubb Minor simplification to Resource#<=>
dkubb authored Feb 2, 2010
517 return cmp if cmp.nonzero?
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
518 end
9207bb4 @dkubb Minor simplification to Resource#<=>
dkubb authored Feb 2, 2010
519 0
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 15, 2009
520 end
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
521
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
522 # Returns hash value of the object.
523 # Two objects with the same hash value assumed equal (using eql? method)
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
524 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
525 # DataMapper resources are equal when their models have the same hash
526 # and they have the same set of properties
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
527 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
530 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
531 # @api private
532 def hash
533 key.hash
1019ac3 @sam Added key validation to Resources, and added Resource#update_attributes.
sam authored May 8, 2008
534 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
535
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
536 # Get a Human-readable representation of this Resource instance
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 23, 2008
537 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
538 # Foo.new #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
539 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
540 # @return [String]
541 # Human-readable representation of this Resource instance
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
542 #
543 # @api public
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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}"
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
554 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
555
556 "#<#{model.name} #{attrs.join(' ')}>"
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
557 end
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
558
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
559 # Hash of original values of attributes that have unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
560 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
561 # @return [Hash]
562 # original values of attributes that have unsaved changes
563 #
564 # @api semipublic
565 def original_attributes
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
566 if persisted_state.respond_to?(:original_attributes)
ccea211 @dkubb Resource#original_attributes is immutable
dkubb authored Apr 15, 2010
567 persisted_state.original_attributes.dup.freeze
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
568 else
ccea211 @dkubb Resource#original_attributes is immutable
dkubb authored Apr 15, 2010
569 {}.freeze
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
570 end
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
571 end
572
573 # Checks if an attribute has been loaded from the repository
574 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
575 # @example
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
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
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
584 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
585 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
586 # true if ivar +name+ has been loaded
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
587 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
588 # @return [Boolean]
589 # true if ivar +name+ has been loaded
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
590 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
591 # @api private
592 def attribute_loaded?(name)
593 properties[name].loaded?(self)
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
594 end
595
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
596 # Checks if an attribute has unsaved changes
597 #
598 # @param [Symbol] name
599 # name of attribute to check for unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
600 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
601 # @return [Boolean]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
602 # true if attribute has unsaved changes
65c1174 @dkubb Updated Collection#update! and #destroy! to work with limited collect…
dkubb authored May 16, 2009
603 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
604 # @api semipublic
605 def attribute_dirty?(name)
606 dirty_attributes.key?(properties[name])
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
607 end
608
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
609 # Hash of attributes that have unsaved changes
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
610 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
611 # @return [Hash]
612 # attributes that have unsaved changes
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
613 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
614 # @api semipublic
615 def dirty_attributes
616 dirty_attributes = {}
617
618 original_attributes.each_key do |property|
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
619 next unless property.respond_to?(:value)
7dfa9be @dkubb Deprecated Property#value in favor of Property#dump
dkubb authored Apr 21, 2010
620 dirty_attributes[property] = property.dump(property.get!(self))
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
621 end
622
623 dirty_attributes
e3ad4a2 Improved the transactional system greatly and introduced two-phase
Martin Kihlgren authored Apr 25, 2008
624 end
625
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
626 # Returns the Collection the Resource is associated with
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
627 #
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
628 # @return [nil]
629 # nil if this is a new record
630 # @return [Collection]
631 # a Collection that self belongs to
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
632 #
633 # @api private
634 def collection
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
635 return @_collection if (@_collection && @_collection.query.conditions.matches?(self)) || new? || readonly?
cdb918f @dkubb Refactored SEL behavior of Collections
dkubb authored Oct 23, 2009
636 collection_for_self
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
637 end
638
337f9d0 @dkubb Prefix all Resource internal ivars with an underscore
dkubb authored Nov 6, 2009
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
e41cc42 @dkubb Updated Resource#query to be semipublic
dkubb authored Nov 18, 2009
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
bcb6190 @shanna Adapters act as query factory
shanna authored Apr 12, 2010
671 repository.new_query(model, :fields => fields, :conditions => conditions)
e41cc42 @dkubb Updated Resource#query to be semipublic
dkubb authored Nov 18, 2009
672 end
673
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 20, 2008
674 protected
ec84ce4 @myabc Stripped whitespace with "sake strip"
myabc authored Jun 20, 2008
675
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
676 # Method for hooking callbacks before resource saving
a108799 @dkubb Delegate Resource#save and #update to #save! and #update!
dkubb authored Jun 13, 2009
677 #
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
678 # @return [undefined]
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
679 #
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 16, 2009
680 # @api private
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
681 def before_save_hook
682 execute_hooks_for(:before, :save)
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored Mar 3, 2008
683 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
684
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
685 # Method for hooking callbacks after resource saving
ee967c0 @michaelklishin Document DataMapper::Resource#_update
michaelklishin authored Mar 20, 2009
686 #
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
687 # @return [undefined]
c031e9f @michaelklishin Add returned value to documentation of DataMapper::Resource#_update
michaelklishin authored Mar 20, 2009
688 #
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 17, 2009
689 # @api private
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
690 def after_save_hook
691 execute_hooks_for(:after, :save)
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 27, 2008
692 end
693
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
694 # Method for hooking callbacks before resource creation
c65fd07 @dkubb Fixed paranoid types
dkubb authored Feb 4, 2010
695 #
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
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]
c65fd07 @dkubb Fixed paranoid types
dkubb authored Feb 4, 2010
733 #
734 # @api private
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
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)
c65fd07 @dkubb Fixed paranoid types
dkubb authored Feb 4, 2010
746 end
747
dc22e45 @dkubb Updated ManyToMany::Collection#update! to act more like Collection#up…
dkubb authored May 17, 2009
748 private
749
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
750 # Initialize a new instance of this Resource using the provided values
751 #
ba3d0ca @dkubb Pass through arguments/block in Model#new to allow custom #initialize
dkubb authored Jul 9, 2009
752 # @param [Hash] attributes
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
753 # attribute values to use for the new instance
754 #
ba3d0ca @dkubb Pass through arguments/block in Model#new to allow custom #initialize
dkubb authored Jul 10, 2009
755 # @return [Hash]
756 # attribute values used in the new instance
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
757 #
758 # @api public
125fe50 @dbussink Performance improvement for initializing a resource without arguments
dbussink authored Jul 15, 2010
759 def initialize(attributes = nil) # :nodoc:
760 self.attributes = attributes if attributes
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
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)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
770 end
771
bb3b8ee @michaelklishin Document DataMapper::Resource#repository_name
michaelklishin authored Mar 20, 2009
772 # Returns name of the repository this object
773 # was loaded from
774 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
775 # @return [String]
776 # name of the repository this object was loaded from
bb3b8ee @michaelklishin Document DataMapper::Resource#repository_name
michaelklishin authored Mar 20, 2009
777 #
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
778 # @api private
779 def repository_name
780 repository.name
781 end
782
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
783 # Gets this instance's Model's properties
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
784 #
785 # @return [Array(Property)]
786 # List of this Resource's Model's properties
787 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
788 # @api private
789 def properties
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
790 model.properties(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
791 end
792
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
793 # Gets this instance's Model's relationships
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
794 #
795 # @return [Array(Associations::Relationship)]
796 # List of this instance's Model's Relationships
797 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
798 # @api private
799 def relationships
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
800 model.relationships(repository_name)
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
801 end
802
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
803 # Returns the identity map for the model from the repository
4d319fb @michaelklishin Document DataMapper::Resouce#identity_map
michaelklishin authored Mar 20, 2009
804 #
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
805 # @return [IdentityMap]
4d319fb @michaelklishin Document DataMapper::Resouce#identity_map
michaelklishin authored Mar 20, 2009
806 # identity map of repository this object was loaded from
807 #
88f882a @dkubb Removed most reek "Duplication" warnings
dkubb authored Nov 20, 2009
808 # @api private
8a1894d Updated specs to pass with all adapters, even the Yaml adapter
Dan Kubb authored Feb 13, 2009
809 def identity_map
810 repository.identity_map(model)
811 end
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
812
88f882a @dkubb Removed most reek "Duplication" warnings
dkubb authored Nov 20, 2009
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
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
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
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
828 #
829 # @api private
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
830 def fields
831 properties.select do |property|
832 property.loaded?(self) || (new? && property.default?)
833 end
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
834 end
835
6f57483 @dkubb Updated Resource#reload to clear non-key property and relationship ivars
dkubb authored Nov 7, 2009
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
855 (model_properties - model_properties.key | relationships.values).each do |subject|
856 next unless subject.loaded?(self)
857 remove_instance_variable(subject.instance_variable_name)
858 end
859 end
860
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
861 # Lazy loads attributes not yet loaded
862 #
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
863 # @param [Array<Property>] properties
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
864 # the properties to reload
865 #
866 # @return [self]
867 #
868 # @api private
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
869 def lazy_load(properties)
870 eager_load(properties - fields)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
871 end
872
873 # Reloads specified attributes
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
874 #
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
875 # @param [Array<Property>] properties
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
876 # the properties to reload
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
877 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
878 # @return [Resource]
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
879 # the receiver, the current Resource instance
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
880 #
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
881 # @api private
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
882 def eager_load(properties)
3332db6 @dbussink Small bugfix for calling attributes on a new Resource without a Seria…
dbussink authored Dec 20, 2009
883 unless properties.empty? || key.nil? || collection.nil?
a449ec9 @dkubb Updated State specs to use a custom type for the key
dkubb authored Apr 21, 2010
884 # set an initial value to prevent recursive lazy loads
885 properties.each { |property| property.set!(self, nil) }
886
f0f8662 @dkubb Refactored Resource and Collection reloading
dkubb authored Oct 24, 2009
887 collection.reload(:fields => properties)
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
888 end
889
890 self
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
891 end
0ac6b42 Stripped whitespace with sake strip
Dan Kubb authored Sep 11, 2008
892
e41cc42 @dkubb Updated Resource#query to be semipublic
dkubb authored Nov 18, 2009
893 # Return conditions to match the Resource
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
894 #
e41cc42 @dkubb Updated Resource#query to be semipublic
dkubb authored Nov 18, 2009
895 # @return [Hash]
896 # query conditions
0ef5ad1 @michaelklishin Document DataMapper::Resource#lazy_load
michaelklishin authored Mar 20, 2009
897 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
898 # @api private
e41cc42 @dkubb Updated Resource#query to be semipublic
dkubb authored Nov 18, 2009
899 def conditions
5b4be32 @dkubb Use loaded properties for Resource#conditions when no key
dkubb authored Nov 19, 2009
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
ca8aa59 @dkubb Resource#update and Resource#destroy should be idempotent
dkubb authored Oct 15, 2009
911 end
912
2e1fd21 @dkubb Persist dirty parent resources when saving a Resource
dkubb authored Jun 13, 2009
913 # @api private
914 def parent_relationships
915 parent_relationships = []
916
917 relationships.each_value do |relationship|
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
918 next unless relationship.respond_to?(:resource_for)
919 set_default_value(relationship)
920 next unless relationship.loaded?(self) && relationship.get!(self)
921
2e1fd21 @dkubb Persist dirty parent resources when saving a Resource
dkubb authored Jun 13, 2009
922 parent_relationships << relationship
923 end
924
925 parent_relationships
926 end
927
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
928 # Returns loaded child relationships
cb2a5da @michaelklishin Document DataMapper::Resource#child_associations
michaelklishin authored Mar 20, 2009
929 #
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
930 # @return [Array<Associations::OneToMany::Relationship>]
cb2a5da @michaelklishin Document DataMapper::Resource#child_associations
michaelklishin authored Mar 20, 2009
931 # array of child relationships for which this resource is parent and is loaded
932 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
933 # @api private
2e1fd21 @dkubb Persist dirty parent resources when saving a Resource
dkubb authored Jun 13, 2009
934 def child_relationships
935 child_relationships = []
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored Feb 24, 2009
936
6faedbd @dkubb Updated child_associations to skip non-collection Relationship
dkubb authored May 23, 2009
937 relationships.each_value do |relationship|
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
938 next unless relationship.respond_to?(:collection_for)
939 set_default_value(relationship)
940 next unless relationship.loaded?(self)
941
2e1fd21 @dkubb Persist dirty parent resources when saving a Resource
dkubb authored Jun 13, 2009
942 child_relationships << relationship
d54fbe5 @dkubb Updated ManyToMany::Collection#save to properly save intermediaries
dkubb authored May 26, 2009
943 end
944
2e1fd21 @dkubb Persist dirty parent resources when saving a Resource
dkubb authored Jun 13, 2009
945 many_to_many, other = child_relationships.partition do |relationship|
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
946 relationship.kind_of?(Associations::ManyToMany::Relationship)
9aa83de Refactored OneToMany and ManyToMany relationships
Dan Kubb authored Feb 24, 2009
947 end
948
2e1fd21 @dkubb Persist dirty parent resources when saving a Resource
dkubb authored Jun 13, 2009
949 many_to_many + other
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
950 end
951
7ed6e37 @dkubb Updated Resource to be dirty when associations are dirty
dkubb authored Oct 2, 2009
952 # @api private
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
953 def parent_associations
7ed6e37 @dkubb Updated Resource to be dirty when associations are dirty
dkubb authored Oct 2, 2009
954 parent_relationships.map { |relationship| relationship.get!(self) }
955 end
956
957 # @api private
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
958 def child_associations
959 child_relationships.map { |relationship| relationship.get_collection(self) }
7ed6e37 @dkubb Updated Resource to be dirty when associations are dirty
dkubb authored Oct 2, 2009
960 end
961
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 28, 2010
962 # Commit the persisted state
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
963 #
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
964 # @return [undefined]
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
965 #
966 # @api private
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
967 def _persist
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
968 self.persisted_state = persisted_state.commit
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
969 end
970
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
971 # This method executes the hooks before and after resource creation
972 #
973 # @return [Boolean]
974 #
975 # @see Resource#_create
976 #
977 # @api private
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
978 def create_with_hooks
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
979 catch :halt do
980 before_save_hook
981 before_create_hook
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
982 _persist
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
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
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
995 def update_with_hooks
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
996 catch :halt do
997 before_save_hook
998 before_update_hook
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
999 _persist
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
1000 after_update_hook
1001 after_save_hook
1002 end
1003 end
1004
f5f2599 @dkubb Refactored resource destruction
dkubb authored May 29, 2010
1005 # Destroy the resource
1006 #
1007 # @return [undefined]
1008 #
1f9eed1 @dkubb Refactored Resource#save and Resource#dirty to handle circular depend…
dkubb authored Oct 26, 2009
1009 # @api private
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1010 def _destroy(execute_hooks = true)
f5f2599 @dkubb Refactored resource destruction
dkubb authored May 29, 2010
1011 self.persisted_state = persisted_state.delete
1012 _persist
c65fd07 @dkubb Fixed paranoid types
dkubb authored Feb 4, 2010
1013 end
1014
1015 # @api private
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1016 def _save(execute_hooks = true)
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1017 run_once(true) do
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1018 save_parents(execute_hooks) && save_self(execute_hooks) && save_children(execute_hooks)
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1019 end
6b9ab6d @dkubb A dirty sibling should not make a resource dirty
dkubb authored Oct 4, 2009
1020 end
1021
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1022 # Saves the resource
1023 #
1024 # @return [Boolean]
1025 # true if the resource was successfully saved
1026 #
1027 # @api semipublic
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1028 def save_self(execute_hooks = true)
1b76693 @dkubb Skip saving if the dirty test returns false
dkubb authored Feb 1, 2010
1029 # short-circuit if the resource is not dirty
1030 return saved? unless dirty_self?
1031
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1032 if execute_hooks
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
1033 new? ? create_with_hooks : update_with_hooks
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1034 else
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
1035 _persist
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1036 end
e63dd70 @dkubb Refactored how persistence is handled internally
dkubb authored May 29, 2010
1037 clean?
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1038 end
1039
1040 # Saves the parent resources
1041 #
1042 # @return [Boolean]
1043 # true if the parents were successfully saved
1044 #
1045 # @api private
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1046 def save_parents(execute_hooks)
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1047 run_once(true) do
39e131f @dkubb Make sure saving parent/children is not short circuited
dkubb authored Apr 2, 2010
1048 parent_relationships.map do |relationship|
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1049 parent = relationship.get!(self)
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1050
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1051 if parent.__send__(:save_parents, execute_hooks) && parent.__send__(:save_self, execute_hooks)
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1052 relationship.set(self, parent) # set the FK values
1053 end
39e131f @dkubb Make sure saving parent/children is not short circuited
dkubb authored Apr 3, 2010
1054 end.all?
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
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
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1064 def save_children(execute_hooks)
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
1065 child_associations.map do |association|
548c178 @dkubb Update private _create, _update, _destroy, _save to have similar nami…
dkubb authored Apr 24, 2010
1066 association.__send__(execute_hooks ? :save : :save!)
39e131f @dkubb Make sure saving parent/children is not short circuited
dkubb authored Apr 3, 2010
1067 end.all?
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1068 end
1069
1070 # Checks if the resource has unsaved changes
1071 #
1072 # @return [Boolean]
1073 # true if the resource has unsaged changes
1074 #
273f48b @dkubb Update Resource#dirty_self? to be semipublic
dkubb authored Feb 2, 2010
1075 # @api semipublic
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1076 def dirty_self?
1077 if original_attributes.any?
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
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
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1092 def dirty_parents?
1093 run_once(false) do
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
1094 parent_associations.any? do |association|
1095 association.__send__(:dirty_self?) || association.__send__(:dirty_parents?)
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1096 end
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
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
9115894 @dkubb Simplified SystemStackError prevention in saving and dirtyness testing
dkubb authored Oct 26, 2009
1109 def dirty_children?
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
1110 child_associations.any? { |association| association.dirty? }
f5e8c5f @dkubb Updated saving and dirtyness internal methods to be private
dkubb authored Oct 26, 2009
1111 end
1112
9c54f28 Added Query#update specs
Dan Kubb authored Jan 31, 2009
1113 # Return true if +other+'s is equivalent or equal to +self+'s
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
1114 #
1115 # @param [Resource] other
1116 # The Resource whose attributes are to be compared with +self+'s
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 24, 2009
1117 # @param [Symbol] operator
1118 # The comparison operator to use to compare the attributes
1119 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
1120 # @return [Boolean]
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
1121 # The result of the comparison of +other+'s attributes with +self+'s
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored Nov 25, 2008
1122 #
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
1123 # @api private
9c54f28 Added Query#update specs
Dan Kubb authored Feb 1, 2009
1124 def cmp?(other, operator)
020646c @dkubb Refactored Resource#cmp? to not use #clean?
dkubb authored Apr 23, 2010
1125 return false unless repository.send(operator, other.repository) &&
1126 key.send(operator, other.key)
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored Dec 12, 2008
1127
020646c @dkubb Refactored Resource#cmp? to not use #clean?
dkubb authored Apr 23, 2010
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
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored Dec 12, 2008
1136 end
53c5272 @dkubb Refactored persistence layer to use a state machine
dkubb authored Mar 24, 2010
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)
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored Nov 25, 2008
1143 end
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
1144
28bdb27 @dkubb Split up hooks into explicit methods
dkubb authored Mar 22, 2010
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
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
1159 # Raises an exception if #update is performed on a dirty resource
1160 #
f127bad @dkubb Minor cleanup to Resource lazy/eager loading logic
dkubb authored Sep 15, 2009
1161 # @param [Symbol] method
1162 # the name of the method to use in the exception
1163 #
1164 # @return [undefined]
1165 #
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
1166 # @raise [UpdateConflictError]
1167 # raise if the resource is dirty
1168 #
1169 # @api private
d85c0af @dkubb Updated Resource#assert_update_clean_only to include the class/method…
dkubb authored Aug 17, 2009
1170 def assert_update_clean_only(method)
fd88599 @dkubb Use Resource#dirty? to test for dirtyness prior to executing Resource…
dkubb authored Oct 2, 2009
1171 if dirty?
0a374fd @dkubb Clarified error message for calling Resource#update on a new resource
dkubb authored Feb 24, 2010
1172 raise UpdateConflictError, "#{model}##{method} cannot be called on a #{new? ? 'new' : 'dirty'} resource"
d85c0af @dkubb Updated Resource#assert_update_clean_only to include the class/method…
dkubb authored Aug 17, 2009
1173 end
25ace54 @dkubb Modified Resource#update and #update! to raise an exception on modifi…
dkubb authored Aug 16, 2009
1174 end
92e8a58 @dkubb Assert that Resource#save cannot be called on a destroyed resource
dkubb authored Oct 7, 2009
1175
1176 # Raises an exception if #save is performed on a destroyed resource
1177 #
1178 # @param [Symbol] method
1179