Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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