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