Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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