Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 755 lines (674 sloc) 19.424 kb
eeb1702 Resolved ticket #212.
Martin Kihlgren authored
1 require 'set'
e8b4f3b @sam Importing from old repository.
sam authored
2
3 module DataMapper
4 module Resource
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
5 include Extlib::Assertions
182e590 Minor code cleanup
Dan Kubb authored
6
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
7 ##
8 # Appends a module for inclusion into the model class after
9 # DataMapper::Resource.
10 #
11 # This is a useful way to extend DataMapper::Resource while still retaining
12 # a self.included method.
13 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
14 # @param [Module] inclusions
15 # the module that is to be appended to the module after DataMapper::Resource
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
16 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
17 # @return [TrueClass, FalseClass]
18 # true if the inclusions have been successfully appended to the list
c8b0824 Removing unecessary punctuation/formatting within comments
Dan Kubb authored
19 #
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored
20 # @api semipublic
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
21 def self.append_inclusions(*inclusions)
22 extra_inclusions.concat inclusions
23 true
24 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored
25
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored
26 # The current registered extra inclusions
27 # @api private
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
28 def self.extra_inclusions
29 @extra_inclusions ||= []
30 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored
31
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
32 # 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
33 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
34 # @api private
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
35 # TODO: move logic to Model#extended
74ef620 Modules now push themselves into Resource rather than it pulling them
Dan Kubb authored
36 def self.included(model)
e41f6ab Renamed Resource::ClassMethods to Model
Dan Kubb authored
37 model.extend Model
bface80 Updated DataMapper singleton methods to only mix-in on demand
Dan Kubb authored
38
39 if defined?(ClassMethods)
40 warn "#{ClassMethods} is deprecated, add methods to #{Model} instead"
41 model.extend ClassMethods
42 end
43
44 unless model.const_defined?('Resource')
45 model.const_set('Resource', self)
46 end
47
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored
48 extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
3684f20 Minor Resource cleanup
Dan Kubb authored
49 descendants << model
16f99f5 Only #assert_valid_model once per model, rather than once per #new. A…
Paul Sadauskas authored
50 class << model
51 @_valid_model = false
52 attr_reader :_valid_model
53 end
748b8d9 Enabled DataMapper::Resource to know what classes have included it.
Martin Kihlgren authored
54 end
55
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
56 ##
10b3266 Added better documentation for Resource.including_classes.
Martin Kihlgren authored
57 # Return all classes that include the DataMapper::Resource module
58 #
78daf51 @advany add docs to DataMapper::Resource
advany authored
59 # Class Foo
60 # include DataMapper::Resource
61 # end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
62 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
63 # DataMapper::Resource.descendants.to_a.first #=> Foo
64 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
65 # @return [Set]
66 # Set containing the including classes
78daf51 @advany add docs to DataMapper::Resource
advany authored
67 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
68 # @api semipublic
aa7c427 @wzph -1 Resource::descendents ; +1 Resource::descendants
wzph authored
69 def self.descendants
3684f20 Minor Resource cleanup
Dan Kubb authored
70 @descendants ||= Set.new
bd0410f @sam resource_spec passes now.
sam authored
71 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
72
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
73 # +---------------
74 # Instance methods
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
75
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
76 # TODO: document
77 # @api private
d294bac Updated Adapter API
Dan Kubb authored
78 attr_writer :collection
79
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
80 # TODO: document
81 # @api public
d294bac Updated Adapter API
Dan Kubb authored
82 alias model class
6db24c7 Small performance optimizations
Dan Kubb authored
83
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
84 ##
6186835 Stripped whitespace
Dan Kubb authored
85 # Returns the value of the attribute.
86 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
87 # Do not read from instance variables directly, but use this method.
88 # This method handles lazy loading the attribute and returning of
89 # defaults if nessesary.
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
90 #
91 # Class Foo
92 # include DataMapper::Resource
93 #
94 # property :first_name, String
95 # property :last_name, String
96 #
97 # def full_name
98 # "#{attribute_get(:first_name)} #{attribute_get(:last_name)}"
99 # end
100 #
101 # # using the shorter syntax
102 # def name_for_address_book
103 # "#{last_name}, #{first_name}"
104 # end
105 # end
106 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
107 # @param [Symbol] name
108 # name of attribute to retrieve
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
109 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
110 # @return [Object]
111 # the value stored at that given attribute
112 # (nil if none, and default if necessary)
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
113 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
114 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
115 def attribute_get(name)
84880f2 Simplified Resource#attribute_get
Dan Kubb authored
116 properties[name].get(self)
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
117 end
118
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
119 ##
120 # 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
121 # if it has been changed so that it may be saved. Do not set from
122 # instance variables directly, but use this method. This method
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
123 # handles the lazy loading the property and returning of defaults
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
124 # if nessesary.
125 #
126 # Class Foo
127 # include DataMapper::Resource
128 #
129 # property :first_name, String
130 # property :last_name, String
131 #
132 # def full_name(name)
133 # name = name.split(' ')
134 # attribute_set(:first_name, name[0])
135 # attribute_set(:last_name, name[1])
136 # end
137 #
138 # # using the shorter syntax
139 # def name_from_address_book(name)
140 # name = name.split(', ')
141 # first_name = name[1]
142 # last_name = name[0]
143 # end
144 # end
145 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
146 # @param [Symbol] name
147 # name of attribute to set
148 # @param [Object] value
149 # value to store
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
150 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
151 # @return [Object]
152 # the value stored at that given attribute, nil if none,
153 # and default if necessary
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
154 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
155 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored
156 def attribute_set(name, value)
157 properties[name].set(self, value)
158 end
159
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
160 ##
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
161 # Tests the equality with another Resource
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
162 #
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
163 # Resource is equal to +other+ if they are the same object (identity)
164 # or if they are both of the *same model* and all of their attributes
165 # are equal
78daf51 @advany add docs to DataMapper::Resource
advany authored
166 #
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
167 # @param [DataMapper::Resource] other
168 # Resource to compare to
78daf51 @advany add docs to DataMapper::Resource
advany authored
169 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
170 # @return [TrueClass, FalseClass]
171 # the outcome of the comparison as a boolean
78daf51 @advany add docs to DataMapper::Resource
advany authored
172 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
173 # @api public
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
174 def eql?(other)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
175 if equal?(other)
176 return true
177 end
178
179 unless other.respond_to?(:model) && model.equal?(other.model)
180 return false
181 end
182
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored
183 eql_attributes?(other)
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
184 end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
185
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
186 ##
187 # Tests the equality with another Resource
188 #
189 # Resource is equal to +other+ if they are the same object (identity)
190 # or if they are both of the *same base model* and all of their attributes
191 # are equal
192 #
193 # @param [DataMapper::Resource] other
194 # Resource to compare to
195 #
196 # @return [TrueClass, FalseClass]
197 # the outcome of the comparison as a boolean
198 #
199 # @api public
200 def ==(other)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
201 if equal?(other)
202 return true
203 end
204
205 unless other.respond_to?(:model) && model.base_model.equal?(other.model.base_model)
206 return false
207 end
208
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored
209 eql_attributes?(other)
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
210 end
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored
211
238682e Added Resource#<=>
Dan Kubb authored
212 ##
213 # Compares two Resources to allow them to be sorted
214 #
215 # @param [DataMapper::Resource] other
216 # The other Resource to compare with
217 #
218 # @return [Integer]
219 # Return 0 if Resources should be sorted as the same, -1 if the
220 # other Resource should be after self, and 1 if the other Resource
221 # should be before self
222 #
223 # @api public
224 def <=>(other)
225 unless other.kind_of?(model)
226 raise ArgumentError, "Cannot compare a #{other.model} instance with a #{model} instance"
227 end
228 cmp = 0
229 model.default_order(repository.name).map do |i|
230 cmp = i.property.get!(self) <=> i.property.get!(other)
231 cmp *= -1 if i.direction == :desc
232 break if cmp != 0
233 end
234 cmp
235 end
236
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
237 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
238 # Get a Human-readable representation of this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
239 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
240 # Foo.new #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>
6186835 Stripped whitespace
Dan Kubb authored
241 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
242 # @return [String]
243 # Human-readable representation of this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
244 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
245 # @api public
f7f784f @wycats Add #inspect and #pretty_print to DM::Resource
wycats authored
246 def inspect
4260cb7 Simplified Property and Resource
Dan Kubb authored
247 new_record = new_record?
248
852c46c Added Collection#reverse
Dan Kubb authored
249 attrs = []
c1b6c77 Updated Resource#inspect to not automatically lazy-load every property
Dan Kubb authored
250
3684f20 Minor Resource cleanup
Dan Kubb authored
251 properties.each do |property|
4260cb7 Simplified Property and Resource
Dan Kubb authored
252 value = if !property.loaded?(self) && !new_record
c1b6c77 Updated Resource#inspect to not automatically lazy-load every property
Dan Kubb authored
253 '<not loaded>'
254 else
255 send(property.getter).inspect
256 end
257
258 attrs << "#{property.name}=#{value}"
852c46c Added Collection#reverse
Dan Kubb authored
259 end
c1b6c77 Updated Resource#inspect to not automatically lazy-load every property
Dan Kubb authored
260
d294bac Updated Adapter API
Dan Kubb authored
261 "#<#{model.name} #{attrs * ' '}>"
f7f784f @wycats Add #inspect and #pretty_print to DM::Resource
wycats authored
262 end
456b9ca Updated automigration integrations specs to check every primitive
Dan Kubb authored
263
15f56b7 more resolution
Adam French authored
264 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
265 # Repository this resource belongs to in the context of this collection
266 # or of the resource's class.
15f56b7 more resolution
Adam French authored
267 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
268 # @return [Repository]
269 # the respository this resource belongs to, in the context of
270 # a collection OR in the instance's Model's context
78daf51 @advany add docs to DataMapper::Resource
advany authored
271 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
272 # @api semipublic
d409c1f @sam Added and specced LoadedSet.
sam authored
273 def repository
fbf3707 Minor code cleanup
Dan Kubb authored
274 # only set @repository explicitly when persisted
d294bac Updated Adapter API
Dan Kubb authored
275 @repository || model.repository
d409c1f @sam Added and specced LoadedSet.
sam authored
276 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
277
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
278 ##
30e48ec @dbussink Add a few more Resource specs
dbussink authored
279 # Retrieve the key(s) for this resource.
6186835 Stripped whitespace
Dan Kubb authored
280 #
30e48ec @dbussink Add a few more Resource specs
dbussink authored
281 # This always returns the persisted key value,
282 # even if the key is changed and not yet persisted.
283 # This is done so all relations still work.
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored
284 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
285 # @return [Array(Key)]
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
286 # the key(s) identifying this resource
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored
287 #
288 # @api public
a519fb4 @sam CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
sam authored
289 def key
fbf3707 Minor code cleanup
Dan Kubb authored
290 @key ||= model.key(repository.name).map do |property|
2f5d300 Defer typecasting of original values until Resource#dirty_attributes
Dan Kubb authored
291 original_values[property] || property.get!(self)
a519fb4 @sam CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
sam authored
292 end
293 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
294
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
295 ##
296 # Checks if an attribute has been loaded from the repository
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
297 #
78daf51 @advany add docs to DataMapper::Resource
advany authored
298 # class Foo
299 # include DataMapper::Resource
300 # property :name, String
301 # property :description, Text, :lazy => false
302 # end
303 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
304 # Foo.new.attribute_loaded?(:description) #=> false
78daf51 @advany add docs to DataMapper::Resource
advany authored
305 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
306 # @return [TrueClass, FalseClass]
307 # true if ivar +name+ has been loaded
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
308 #
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
309 # @return [TrueClass, FalseClass] true if ivar +name+ has been loaded
a373795 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
310 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
311 # @api private
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
312 def attribute_loaded?(name)
4260cb7 Simplified Property and Resource
Dan Kubb authored
313 properties[name].loaded?(self)
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
314 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored
315
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
316 ##
317 # Fetches all the names of the attributes that have been loaded,
78daf51 @advany add docs to DataMapper::Resource
advany authored
318 # even if they are lazy but have been called
319 #
320 # class Foo
321 # include DataMapper::Resource
322 # property :name, String
323 # property :description, Text, :lazy => false
324 # end
325 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
326 # Foo.new.loaded_attributes #=> [:name]
78daf51 @advany add docs to DataMapper::Resource
advany authored
327 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
328 # @return [Array(Symbol)]
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
329 # names of attributes that have been loaded
8665cc6 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
330 #
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
331 # @return [Array<Symbol>] names of attributes that have been loaded
a373795 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
332 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
333 # @api private
68a9fd7 @sam Fixed reload issue: http://wm.lighthouseapp.com/projects/4819/tickets…
sam authored
334 def loaded_attributes
fa131af Minor update to Resource#loaded_attributes to not create unecessary A…
Dan Kubb authored
335 loaded_attributes = properties.map { |p| p.name if p.loaded?(self) }
336 loaded_attributes.compact!
337 loaded_attributes
68a9fd7 @sam Fixed reload issue: http://wm.lighthouseapp.com/projects/4819/tickets…
sam authored
338 end
322419c Stripped whitespace with "sake strip"
Dan Kubb authored
339
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
340 ##
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
341 # Hash of original values of attributes that have unsaved changes
322419c Stripped whitespace with "sake strip"
Dan Kubb authored
342 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
343 # @return [Hash]
344 # original values of attributes that have unsaved changes
322419c Stripped whitespace with "sake strip"
Dan Kubb authored
345 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
346 # @api semipublic
e31b867 @bernerdschaefer Added Property#track :set to replace Resource#dirty_attributes
bernerdschaefer authored
347 def original_values
63ab157 @bernerdschaefer Adding track => :load support
bernerdschaefer authored
348 @original_values ||= {}
e31b867 @bernerdschaefer Added Property#track :set to replace Resource#dirty_attributes
bernerdschaefer authored
349 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
350
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
351 ##
352 # Hash of attributes that have unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
353 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
354 # @return [Hash]
355 # attributes that have unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
356 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
357 # @api semipublic
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
358 def dirty_attributes
b8926c2 Removed Property tracking temporarily
Dan Kubb authored
359 dirty_attributes = {}
6f26387 Stripped whitespace using "sake strip"
Dan Kubb authored
360
cd61049 Updated original value tracking to only track changed values
Dan Kubb authored
361 original_values.each_key do |property|
362 dirty_attributes[property] = property.value(property.get!(self))
d294bac Updated Adapter API
Dan Kubb authored
363 end
364
365 dirty_attributes
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
366 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
367
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
368 ##
369 # Checks if the resource has unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
370 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
371 # @return
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
372 # [TrueClass, FalseClass] true if resource is new or has any unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
373 #
26dceb4 @dbussink Move some of the Resource specs to semipublic
dbussink authored
374 # @api semipublic
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored
375 def dirty?
2f5d300 Defer typecasting of original values until Resource#dirty_attributes
Dan Kubb authored
376 if dirty_attributes.any?
377 true
378 elsif new_record?
4260cb7 Simplified Property and Resource
Dan Kubb authored
379 model.identity_field || properties.any? { |p| p.default? }
2f5d300 Defer typecasting of original values until Resource#dirty_attributes
Dan Kubb authored
380 else
381 false
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored
382 end
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored
383 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
384
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
385 ##
386 # Checks if an attribute has unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
387 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
388 # @param [Symbol] name
389 # name of attribute to check for unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
390 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
391 # @return [TrueClass, FalseClass]
392 # true if attribute has unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored
393 #
26dceb4 @dbussink Move some of the Resource specs to semipublic
dbussink authored
394 # @api semipublic
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
395 def attribute_dirty?(name)
c60d82b Minor refactoring of Hash#key? usage
Dan Kubb authored
396 dirty_attributes.key?(properties[name])
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored
397 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
398
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
399 # Gets a Collection with the current Resource instance as its only member
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
400 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
401 # @return [DataMapper::Collection, FalseClass]
402 # false if this is a new record,
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
403 # otherwise a Collection with self as its only member
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
404 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
405 # @api private
b578dd9 @somebee made resource.collection make a new collection if it is not set, and …
somebee authored
406 def collection
fbf3707 Minor code cleanup
Dan Kubb authored
407 @collection ||= unless new_record?
408 Collection.new(to_query, [ self ])
d294bac Updated Adapter API
Dan Kubb authored
409 end
b578dd9 @somebee made resource.collection make a new collection if it is not set, and …
somebee authored
410 end
411
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
412 ##
413 # Reloads association and all child association
78daf51 @advany add docs to DataMapper::Resource
advany authored
414 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
415 # @return [Resource]
416 # the receiver, the current Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
417 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
418 # @api public
4367249 Minor refactoring
Dan Kubb authored
419 def reload
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored
420 unless new_record?
421 reload_attributes(*loaded_attributes)
49e210f Removed Relationship#get_children and Relationship#get_parent
Dan Kubb authored
422 child_associations.each { |a| a.reload }
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored
423 end
424
8aa0525 @sam Added reloading for associations. Need to spec.
sam authored
425 self
3412634 @sam Custom Types are integrated (with some caveats) see spec/integration/…
sam authored
426 end
58c07c8 Whitespace cleanup with "sake strip" (from dm-dev sake tasks)
Dan Kubb authored
427
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
428 ##
429 # Reloads specified attributes
78daf51 @advany add docs to DataMapper::Resource
advany authored
430 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
431 # @param [Enumerable(Symbol)] attributes
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
432 # name(s) of attribute(s) to reload
78daf51 @advany add docs to DataMapper::Resource
advany authored
433 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
434 # @return [Resource]
435 # the receiver, the current Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
436 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
437 # @api private
ece0d18 @somebee some small convenient changes. added possibility for iterating throug…
somebee authored
438 def reload_attributes(*attributes)
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored
439 unless attributes.empty? || new_record?
440 collection.reload(:fields => attributes)
441 end
442
ece0d18 @somebee some small convenient changes. added possibility for iterating throug…
somebee authored
443 self
444 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
445
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
446 ##
447 # Checks if this Resource instance has been saved
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
448 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
449 # @return [TrueClass, FalseClass]
450 # true if the resource has been saved
805e402 @myabc YARD conversion for dm-core
myabc authored
451 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
452 # @api public
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made …
Bauerpauer authored
453 def new_record?
8201c42 Explicitly set @new_record on Resource initialization
Dan Kubb authored
454 @new_record == true
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made …
Bauerpauer authored
455 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
456
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
457 ##
458 # Gets all the attributes of the Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored
459 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
460 # @return [Hash]
461 # All the (non)-lazy attributes
6186835 Stripped whitespace
Dan Kubb authored
462 #
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
463 # @return [Hash]
464 # All the (non)-lazy attributes
40fcf73 Stripped whitespace
Dan Kubb authored
465 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
466 # @api public
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
467 def attributes
5e9a6d3 Simplified Resource#attributes
Dan Kubb authored
468 attributes = {}
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
469 properties.each do |property|
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
470 if public_method?(getter = property.getter)
471 attributes[property.name] = send(getter)
472 end
5e9a6d3 Simplified Resource#attributes
Dan Kubb authored
473 end
474 attributes
e8b4f3b @sam Importing from old repository.
sam authored
475 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
476
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
477 ##
478 # Assign values to multiple attributes in one call (mass assignment)
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored
479 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
480 # @param [Hash] attributes
481 # names and values of attributes to assign
6186835 Stripped whitespace
Dan Kubb authored
482 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored
483 # @return [Hash]
484 # names and values of attributes assigned
78daf51 @advany add docs to DataMapper::Resource
advany authored
485 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored
486 # @api public
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
487 def attributes=(attributes)
488 attributes.each do |name,value|
fbf3707 Minor code cleanup
Dan Kubb authored
489 # XXX: is it common to have an attribute with a trailing question mark?
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
490 name = name.to_s.sub(/\?\z/, '')
491 if public_method?(setter = "#{name}=")
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
492 send(setter, value)
c398725 Removed :public, :public, :protected PROPERTY_OPTIONS
David James authored
493 else
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
494 raise ArgumentError, "The property '#{name}' is not accessible in #{self.class}"
c398725 Removed :public, :public, :protected PROPERTY_OPTIONS
David James authored
495 end
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
496 end
497 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored
498
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
499 ##
500 # Deprecated API for updating attributes and saving Resource
6186835 Stripped whitespace
Dan Kubb authored
501 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
502 # @see #update
6186835 Stripped whitespace
Dan Kubb authored
503 #
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
504 # @api public
505 def update_attributes(*args)
506 warn "#{self.class}#update_attributes is deprecated, use #{self.class}#update instead"
507 update(*args)
508 end
509
510 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
511 # Updates attributes and saves this Resource instance
72db385 @advany update_attributes should save model
advany authored
512 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
513 # @param [Hash] attributes attributes to be updated
514 # @param [Array] allowed (optional) list of attributes to update
78daf51 @advany add docs to DataMapper::Resource
advany authored
515 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
516 # @return [TrueClass, FalseClass] true if resource and storage state match
72db385 @advany update_attributes should save model
advany authored
517 #
8708119 @myabc Convert docs to YARD-style
myabc authored
518 # @api public
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
519 def update(attributes = {}, *allowed)
520 assert_kind_of 'attributes', attributes, Hash
521
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
522 # filter out only allowed attributes
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored
523 self.attributes = allowed.any? ? attributes.only(*allowed) : attributes
524
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
525 _update
1019ac3 @sam Added key validation to Resources, and added Resource#update_attributes.
sam authored
526 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
527
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
528 ##
529 # Save the instance and associated children to the data-store.
6186835 Stripped whitespace
Dan Kubb authored
530 #
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
531 # This saves all children in a has n relationship (if they're dirty).
532 #
533 # @return [TrueClass, FalseClass]
534 # true if Resource instance and all associations were saved
535 #
536 # @see DataMapper::Repository#save
537 #
538 # @api public
539 def save(context = :default)
540 # Takes a context, but does nothing with it. This is to maintain the
541 # same API through out all of dm-more. dm-validations requires a
542 # context to be passed
543
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
544 unless saved = new_record? ? _create : _update
3748f80 Updated Resource#save logic to short-circuit if unable to create or u…
Dan Kubb authored
545 return false
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
546 end
547
3748f80 Updated Resource#save logic to short-circuit if unable to create or u…
Dan Kubb authored
548 original_values.clear
549
49e210f Removed Relationship#get_children and Relationship#get_parent
Dan Kubb authored
550 child_associations.all? { |a| a.save }
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
551 end
552
553 ##
554 # Destroy the instance, remove it from the repository
555 #
556 # @return [TrueClass, FalseClass]
557 # true if resource was destroyed
558 #
559 # @api public
560 def destroy
d0ce66c Updated Resource#destroy logic to be more clear
Dan Kubb authored
561 if !new_record? && repository.delete(to_query) == 1
562 reset
563 true
564 else
565 false
566 end
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored
567 end
568
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
569 # Gets a Query that will return this Resource instance
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
570 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
571 # @return [Query] Query that will retrieve this Resource instance
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
572 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
573 # @api private
fbf3707 Minor code cleanup
Dan Kubb authored
574 def to_query
575 model.to_query(repository, key)
e3ad4a2 Improved the transactional system greatly and introduced two-phase
Martin Kihlgren authored
576 end
577
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
578 ##
579 # Reset the Resource to a similar state as a new record
580 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
581 # @api private
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
582 def reset
583 @new_record = true
584 repository.identity_map(model).delete(key)
585 original_values.clear
3684f20 Minor Resource cleanup
Dan Kubb authored
586 end
587
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
588 protected
ec84ce4 @myabc Stripped whitespace with "sake strip"
myabc authored
589
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
590 ##
591 # Saves this Resource instance to the repository,
592 # setting default values for any unset properties
593 #
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
594 # Needs to be a protected method so that it is hookable
6186835 Stripped whitespace
Dan Kubb authored
595 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
596 # @return [TrueClass, FalseClass]
597 # true if the receiver was successfully created
6186835 Stripped whitespace
Dan Kubb authored
598 #
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
599 # @api semipublic
600 def _create
3558f1d @dbussink Fix that saving a non-dirty object returns true
dbussink authored
601 # Can't create a resource that is not dirty and doesn't have serial keys
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
602 if new_record? && !dirty?
603 return false
604 end
8d88b6f Updated Resource#save to return false if changing the key to nil
Dan Kubb authored
605
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
606 # set defaults for new resource
3684f20 Minor Resource cleanup
Dan Kubb authored
607 properties.each do |property|
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
608 if property.default? && !property.loaded?(self)
609 property.set(self, property.default_for(self))
610 end
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
611 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
612
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
613 if repository.create([ self ]) != 1
614 return false
615 end
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
616
617 @repository = repository
618 @new_record = false
619
19f15ea Simplified IdentityMap to be a subclass of Hash
Dan Kubb authored
620 repository.identity_map(model)[key] = self
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
621
622 true
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored
623 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored
624
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
625 # TODO: document
626 # @api semipublic
627 def _update
628 # retrieve the attributes that need to be persisted
629 dirty_attributes = self.dirty_attributes
630
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
631 if dirty_attributes.empty?
632 true
633 elsif dirty_attributes.only(*model.key).any? { |_,v| v.blank? }
634 false
635 elsif repository.update(dirty_attributes, to_query) != 1
636 false
637 else
638 repository.identity_map(model)[key] = self
377098b Saving a Resource with a new key updates the Identity Map
Dan Kubb authored
639
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
640 true
641 end
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored
642 end
643
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
644 # Gets this instance's Model's properties
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
645 #
646 # @return [Array(Property)]
647 # List of this Resource's Model's properties
648 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
649 # @api private
650 def properties
651 model.properties(repository.name)
652 end
653
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
654 # Gets this instance's Model's relationships
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
655 #
656 # @return [Array(Associations::Relationship)]
657 # List of this instance's Model's Relationships
658 #
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored
659 # @api private
660 def relationships
661 model.relationships(repository.name)
662 end
663
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
664 private
665
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
666 ##
667 # Initialize a new instance of this Resource using the provided values
6186835 Stripped whitespace
Dan Kubb authored
668 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
669 # @param [Hash] attributes
670 # attribute values to use for the new instance
6186835 Stripped whitespace
Dan Kubb authored
671 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored
672 # @return [Resource]
673 # the newly initialized resource instance
6186835 Stripped whitespace
Dan Kubb authored
674 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
675 # @api public
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
676 def initialize(attributes = {}) # :nodoc:
677 assert_valid_model
8201c42 Explicitly set @new_record on Resource initialization
Dan Kubb authored
678 @new_record = true
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
679 self.attributes = attributes
680 end
0ac6b42 Stripped whitespace with sake strip
Dan Kubb authored
681
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
682 # TODO: move to Model#assert_valid
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored
683 # @api private
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
684 def assert_valid_model # :nodoc:
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
685 if self.class._valid_model
686 return
687 end
688
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
689 properties = self.properties
690
691 if properties.empty? && relationships.empty?
692 raise IncompleteResourceError, "#{model.name} must have at least one property or relationship to be initialized."
693 end
694
695 if properties.key.empty?
696 raise IncompleteResourceError, "#{model.name} must have a key."
697 end
16f99f5 Only #assert_valid_model once per model, rather than once per #new. A…
Paul Sadauskas authored
698
7a8b498 @dbussink Performance refactoring
dbussink authored
699 self.class.instance_variable_set("@_valid_model", true)
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored
700 end
701
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
702 # TODO: document
703 # @api private
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored
704 def lazy_load(name)
a1f99a9 Updated lazy load to only load non-loaded attributes
Dan Kubb authored
705 reload_attributes(*properties.lazy_load_context(name) - loaded_attributes)
3684f20 Minor Resource cleanup
Dan Kubb authored
706 end
707
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored
708 # TODO: document
709 # @api private
3684f20 Minor Resource cleanup
Dan Kubb authored
710 def child_associations
711 @child_associations ||= []
712 end
713
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
714 ##
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
715 # Return true if the accesor or mutator +method+ is publicly accessible
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
716 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
717 # @param [String, Symbol] method
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
718 # The name of accessor or mutator to test
719 #
720 # @return [TrueClass, FalseClass]
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
721 # true if the accessor or mutator +method+ is public
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored
722 #
723 # @api private
724 def public_method?(method)
725 model.public_method_defined?(method)
726 end
727
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored
728 ##
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored
729 # Return true if +other+'s attribtues are equal to +self+'s
730 #
731 # @param [Resource] other
732 # The Resource whose attributes are to be compared with +self+'s
733 #
734 # @return [TrueClass, FalseClass]
735 # 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
736 #
737 def eql_attributes?(other)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored
738 if key != other.key
739 return false
740 end
741
742 if repository == other.repository && !dirty? && !other.dirty?
743 return true
744 end
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored
745
746 loaded, not_loaded = properties.partition do |property|
4260cb7 Simplified Property and Resource
Dan Kubb authored
747 property.loaded?(self) && property.loaded?(other)
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored
748 end
749
750 # check all loaded properties, and then all unloaded properties
751 (loaded + not_loaded).all? { |p| p.get(self) == p.get(other) }
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored
752 end
0ba594d @myabc Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataM…
myabc authored
753 end # module Resource
754 end # module DataMapper
Something went wrong with that request. Please try again.