Skip to content
Newer
Older
100644 761 lines (678 sloc) 19.2 KB
e8b4f3b @sam Importing from old repository.
sam authored Mar 1, 2008
1 module DataMapper
2 module Resource
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 27, 2008
3 include Extlib::Assertions
182e590 Minor code cleanup
Dan Kubb authored Jul 8, 2008
4
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 25, 2008
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 Nov 21, 2008
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 Jun 26, 2008
14 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Oct 12, 2008
17 #
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
18 # @api semipublic
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 26, 2008
19 def self.append_inclusions(*inclusions)
20 extra_inclusions.concat inclusions
21 true
22 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored Jun 27, 2008
23
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
24 # The current registered extra inclusions
25 # @api private
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 26, 2008
26 def self.extra_inclusions
27 @extra_inclusions ||= []
28 end
7e45d1a Stripped whitespace with "sake strip"
Dan Kubb authored Jun 27, 2008
29
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Jun 1, 2008
31 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
32 # @api private
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
33 # TODO: move logic to Model#extended
74ef620 Modules now push themselves into Resource rather than it pulling them
Dan Kubb authored May 10, 2008
34 def self.included(model)
e41f6ab Renamed Resource::ClassMethods to Model
Dan Kubb authored Jun 18, 2008
35 model.extend Model
bface80 Updated DataMapper singleton methods to only mix-in on demand
Dan Kubb authored Jan 14, 2009
36
ffa83a4 @sam Fixed autotest support, corrected some issues with reloading models.
sam authored Jun 26, 2008
37 extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
38
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
39 descendants << model
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
40
16f99f5 Only #assert_valid_model once per model, rather than once per #new. A…
Paul Sadauskas authored Sep 8, 2008
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 Apr 18, 2008
45 end
46
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
47 ##
10b3266 Added better documentation for Resource.including_classes.
Martin Kihlgren authored Apr 18, 2008
48 # Return all classes that include the DataMapper::Resource module
49 #
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
50 # Class Foo
51 # include DataMapper::Resource
52 # end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 2, 2008
53 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
54 # DataMapper::Resource.descendants.to_a.first #=> Foo
55 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
56 # @return [Set]
57 # Set containing the including classes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
58 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
59 # @api semipublic
aa7c427 @wzph -1 Resource::descendents ; +1 Resource::descendants
wzph authored May 31, 2008
60 def self.descendants
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
61 @descendants ||= Set.new
bd0410f @sam resource_spec passes now.
sam authored Mar 2, 2008
62 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
63
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 18, 2008
64 # +---------------
65 # Instance methods
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
66
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
67 # TODO: document
68 # @api private
d294bac Updated Adapter API
Dan Kubb authored Jun 11, 2008
69 attr_writer :collection
70
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
71 # TODO: document
72 # @api public
d294bac Updated Adapter API
Dan Kubb authored Jun 11, 2008
73 alias model class
6db24c7 Small performance optimizations
Dan Kubb authored Apr 4, 2008
74
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
75 ##
6186835 Stripped whitespace
Dan Kubb authored Nov 23, 2008
76 # Returns the value of the attribute.
77 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
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 Jun 16, 2008
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 Nov 21, 2008
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 Nov 20, 2008
100 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Nov 20, 2008
104 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
105 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
106 def attribute_get(name)
84880f2 Simplified Resource#attribute_get
Dan Kubb authored Jan 13, 2009
107 properties[name].get(self)
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
108 end
109
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Jun 17, 2008
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 Nov 20, 2008
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 Jun 17, 2008
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 Nov 21, 2008
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 Nov 20, 2008
141 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
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 Nov 20, 2008
145 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
146 # @api public
fa3f914 Updated Resource#attribute_get and #attribute_set be public again
Dan Kubb authored Jun 17, 2008
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 Nov 21, 2008
151 ##
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
152 # Compares another Resource for equality
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
153 #
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
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 Jan 24, 2009
156 # are equivalent
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
157 #
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
158 # @param [DataMapper::Resource] other
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
159 # the other Resource to compare with
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
160 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
161 # @return [TrueClass, FalseClass]
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
162 # true if they are equal, false if not
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
163 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
164 # @api public
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 28, 2008
165 def eql?(other)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
166 if equal?(other)
167 return true
168 end
169
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
170 unless self.class.equal?(other.class)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
171 return false
172 end
173
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 24, 2009
174 cmp_attributes?(other, :eql?)
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 29, 2008
175 end
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 2, 2008
176
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
177 ##
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
178 # Compares another Resource for equivalency
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
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 Jan 24, 2009
182 # are equivalent
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
183 #
184 # @param [DataMapper::Resource] other
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
185 # the other Resource to compare with
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
186 #
187 # @return [TrueClass, FalseClass]
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
188 # true if they are equivalent, false if not
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
189 #
190 # @api public
191 def ==(other)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
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
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
200 cmp_attributes?(other, :==)
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
201 end
e5847b9 Updated Collection to implement an interface similar to Array
Dan Kubb authored Apr 29, 2008
202
238682e Added Resource#<=>
Dan Kubb authored Nov 20, 2008
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 Nov 21, 2008
228 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
229 # Get a Human-readable representation of this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
230 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
231 # Foo.new #=> #<Foo name=nil updated_at=nil created_at=nil id=nil>
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
232 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
233 # @return [String]
234 # Human-readable representation of this Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
235 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
236 # @api public
f7f784f @wycats Add #inspect and #pretty_print to DM::Resource
wycats authored May 2, 2008
237 def inspect
4260cb7 Simplified Property and Resource
Dan Kubb authored Jan 6, 2009
238 new_record = new_record?
239
852c46c Added Collection#reverse
Dan Kubb authored Jun 8, 2008
240 attrs = []
c1b6c77 Updated Resource#inspect to not automatically lazy-load every property
Dan Kubb authored Jun 8, 2008
241
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
242 properties.each do |property|
4260cb7 Simplified Property and Resource
Dan Kubb authored Jan 6, 2009
243 value = if !property.loaded?(self) && !new_record
c1b6c77 Updated Resource#inspect to not automatically lazy-load every property
Dan Kubb authored Jun 9, 2008
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 Jun 8, 2008
250 end
c1b6c77 Updated Resource#inspect to not automatically lazy-load every property
Dan Kubb authored Jun 9, 2008
251
d294bac Updated Adapter API
Dan Kubb authored Jun 11, 2008
252 "#<#{model.name} #{attrs * ' '}>"
f7f784f @wycats Add #inspect and #pretty_print to DM::Resource
wycats authored May 2, 2008
253 end
456b9ca Updated automigration integrations specs to check every primitive
Dan Kubb authored May 3, 2008
254
15f56b7 more resolution
Adam French authored May 18, 2008
255 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 May 18, 2008
258 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 May 30, 2008
262 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
263 # @api semipublic
d409c1f @sam Added and specced LoadedSet.
sam authored Mar 15, 2008
264 def repository
fbf3707 Minor code cleanup
Dan Kubb authored Jan 8, 2009
265 # only set @repository explicitly when persisted
d294bac Updated Adapter API
Dan Kubb authored Jun 11, 2008
266 @repository || model.repository
d409c1f @sam Added and specced LoadedSet.
sam authored Mar 16, 2008
267 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
268
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
269 ##
30e48ec @dbussink Add a few more Resource specs
dbussink authored Nov 17, 2008
270 # Retrieve the key(s) for this resource.
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
271 #
30e48ec @dbussink Add a few more Resource specs
dbussink authored Nov 17, 2008
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 Nov 12, 2008
275 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
276 # @return [Array(Key)]
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
277 # the key(s) identifying this resource
dfac1c5 @dbussink Add more shared Resource specs
dbussink authored Nov 12, 2008
278 #
279 # @api public
a519fb4 @sam CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
sam authored Mar 23, 2008
280 def key
fbf3707 Minor code cleanup
Dan Kubb authored Jan 8, 2009
281 @key ||= model.key(repository.name).map do |property|
2f5d300 Defer typecasting of original values until Resource#dirty_attributes
Dan Kubb authored Jan 6, 2009
282 original_values[property] || property.get!(self)
a519fb4 @sam CRUD in DataObjectAdapter is mostly done. There appears to be a bug i…
sam authored Mar 24, 2008
283 end
284 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
285
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Jun 2, 2008
288 #
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
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 Nov 21, 2008
295 # Foo.new.attribute_loaded?(:description) #=> false
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
296 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
297 # @return [TrueClass, FalseClass]
298 # true if ivar +name+ has been loaded
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
299 #
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
300 # @return [TrueClass, FalseClass] true if ivar +name+ has been loaded
a373795 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
301 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
302 # @api private
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 19, 2008
303 def attribute_loaded?(name)
4260cb7 Simplified Property and Resource
Dan Kubb authored Jan 6, 2009
304 properties[name].loaded?(self)
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 19, 2008
305 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored May 8, 2008
306
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
307 ##
308 # Fetches all the names of the attributes that have been loaded,
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
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 Nov 21, 2008
317 # Foo.new.loaded_attributes #=> [:name]
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
318 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
319 # @return [Array(Symbol)]
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
320 # names of attributes that have been loaded
8665cc6 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
321 #
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
322 # @return [Array<Symbol>] names of attributes that have been loaded
a373795 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
323 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
324 # @api private
68a9fd7 @sam Fixed reload issue: http://wm.lighthouseapp.com/projects/4819/tickets…
sam authored May 2, 2008
325 def loaded_attributes
fa131af Minor update to Resource#loaded_attributes to not create unecessary A…
Dan Kubb authored Jan 6, 2009
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 May 2, 2008
329 end
322419c Stripped whitespace with "sake strip"
Dan Kubb authored Jun 11, 2008
330
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
331 ##
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
332 # Hash of original values of attributes that have unsaved changes
322419c Stripped whitespace with "sake strip"
Dan Kubb authored Jun 12, 2008
333 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
334 # @return [Hash]
335 # original values of attributes that have unsaved changes
322419c Stripped whitespace with "sake strip"
Dan Kubb authored Jun 12, 2008
336 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
337 # @api semipublic
e31b867 @bernerdschaefer Added Property#track :set to replace Resource#dirty_attributes
bernerdschaefer authored Jun 11, 2008
338 def original_values
63ab157 @bernerdschaefer Adding track => :load support
bernerdschaefer authored Jun 12, 2008
339 @original_values ||= {}
e31b867 @bernerdschaefer Added Property#track :set to replace Resource#dirty_attributes
bernerdschaefer authored Jun 12, 2008
340 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
341
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
342 ##
343 # Hash of attributes that have unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
344 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
345 # @return [Hash]
346 # attributes that have unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
347 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
348 # @api semipublic
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 19, 2008
349 def dirty_attributes
b8926c2 Removed Property tracking temporarily
Dan Kubb authored Jan 6, 2009
350 dirty_attributes = {}
6f26387 Stripped whitespace using "sake strip"
Dan Kubb authored Jun 12, 2008
351
cd61049 Updated original value tracking to only track changed values
Dan Kubb authored Jan 6, 2009
352 original_values.each_key do |property|
353 dirty_attributes[property] = property.value(property.get!(self))
d294bac Updated Adapter API
Dan Kubb authored Jun 11, 2008
354 end
355
356 dirty_attributes
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 19, 2008
357 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
358
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
359 ##
360 # Checks if the resource has unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
361 #
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
362 # @return
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
363 # [TrueClass, FalseClass] true if resource is new or has any unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
364 #
26dceb4 @dbussink Move some of the Resource specs to semipublic
dbussink authored Nov 17, 2008
365 # @api semipublic
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored Mar 23, 2008
366 def dirty?
2f5d300 Defer typecasting of original values until Resource#dirty_attributes
Dan Kubb authored Jan 6, 2009
367 if dirty_attributes.any?
368 true
369 elsif new_record?
4260cb7 Simplified Property and Resource
Dan Kubb authored Jan 6, 2009
370 model.identity_field || properties.any? { |p| p.default? }
2f5d300 Defer typecasting of original values until Resource#dirty_attributes
Dan Kubb authored Jan 6, 2009
371 else
372 false
0cdf41a Updated Resource#eql? to be strict match
Dan Kubb authored Nov 24, 2008
373 end
041d9f1 @sam DataObjectAdapter sqlite3 integration spec passes for #create.
sam authored Mar 23, 2008
374 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
375
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
376 ##
377 # Checks if an attribute has unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
378 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
379 # @param [Symbol] name
380 # name of attribute to check for unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
381 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
382 # @return [TrueClass, FalseClass]
383 # true if attribute has unsaved changes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
384 #
26dceb4 @dbussink Move some of the Resource specs to semipublic
dbussink authored Nov 17, 2008
385 # @api semipublic
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 19, 2008
386 def attribute_dirty?(name)
c60d82b Minor refactoring of Hash#key? usage
Dan Kubb authored Jan 6, 2009
387 dirty_attributes.key?(properties[name])
0074905 @sam attribute_set/attribute_get are mostly implemented minus lazy-loading.
sam authored Mar 19, 2008
388 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
389
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
390 # Gets a Collection with the current Resource instance as its only member
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
391 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
392 # @return [DataMapper::Collection, FalseClass]
393 # false if this is a new record,
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
394 # otherwise a Collection with self as its only member
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
395 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
396 # @api private
b578dd9 @somebee made resource.collection make a new collection if it is not set, and …
somebee authored Jun 9, 2008
397 def collection
fbf3707 Minor code cleanup
Dan Kubb authored Jan 8, 2009
398 @collection ||= unless new_record?
399 Collection.new(to_query, [ self ])
d294bac Updated Adapter API
Dan Kubb authored Jun 11, 2008
400 end
b578dd9 @somebee made resource.collection make a new collection if it is not set, and …
somebee authored Jun 9, 2008
401 end
402
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
403 ##
404 # Reloads association and all child association
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
405 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
406 # @return [Resource]
407 # the receiver, the current Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
408 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
409 # @api public
4367249 Minor refactoring
Dan Kubb authored Apr 23, 2008
410 def reload
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored Jun 18, 2008
411 unless new_record?
412 reload_attributes(*loaded_attributes)
49e210f Removed Relationship#get_children and Relationship#get_parent
Dan Kubb authored Dec 12, 2008
413 child_associations.each { |a| a.reload }
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored Jun 18, 2008
414 end
415
8aa0525 @sam Added reloading for associations. Need to spec.
sam authored May 21, 2008
416 self
3412634 @sam Custom Types are integrated (with some caveats) see spec/integration/…
sam authored Apr 1, 2008
417 end
58c07c8 Whitespace cleanup with "sake strip" (from dm-dev sake tasks)
Dan Kubb authored May 25, 2008
418
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
419 ##
420 # Reloads specified attributes
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
421 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
422 # @param [Enumerable(Symbol)] attributes
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
423 # name(s) of attribute(s) to reload
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
424 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
425 # @return [Resource]
426 # the receiver, the current Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
427 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
428 # @api private
ece0d18 @somebee some small convenient changes. added possibility for iterating throug…
somebee authored May 23, 2008
429 def reload_attributes(*attributes)
deffabb @antw Fix so that Resource#reload and #reload_attribute only run when the r…
antw authored Jun 18, 2008
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 May 23, 2008
434 self
435 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
436
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
437 ##
438 # Checks if this Resource instance has been saved
c53a629 Updated property type to use Integer instead of Fixnum internally
Dan Kubb authored Jun 2, 2008
439 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
440 # @return [TrueClass, FalseClass]
441 # true if the resource has been saved
805e402 @myabc YARD conversion for dm-core
myabc authored May 18, 2008
442 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
443 # @api public
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made t…
Bauerpauer authored Mar 23, 2008
444 def new_record?
8201c42 Explicitly set @new_record on Resource initialization
Dan Kubb authored Nov 23, 2008
445 @new_record == true
320fb84 @Bauerpauer Added a couple api-check specs. Restored Resource#new_record?. Made t…
Bauerpauer authored Mar 23, 2008
446 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
447
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
448 ##
449 # Gets all the attributes of the Resource instance
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
450 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
451 # @return [Hash]
452 # All the (non)-lazy attributes
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
453 #
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
454 # @return [Hash]
455 # All the (non)-lazy attributes
40fcf73 Stripped whitespace
Dan Kubb authored Nov 26, 2008
456 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
457 # @api public
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored Mar 3, 2008
458 def attributes
5e9a6d3 Simplified Resource#attributes
Dan Kubb authored Nov 18, 2008
459 attributes = {}
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
460 properties.each do |property|
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
461 if public_method?(getter = property.getter)
462 attributes[property.name] = send(getter)
463 end
5e9a6d3 Simplified Resource#attributes
Dan Kubb authored Nov 19, 2008
464 end
465 attributes
e8b4f3b @sam Importing from old repository.
sam authored Mar 2, 2008
466 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
467
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Jun 2, 2008
470 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
471 # @param [Hash] attributes
472 # names and values of attributes to assign
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
473 #
2e14f81 Small cleanup of YARD doc formatting in Resource.
Emmanuel Gomez authored Nov 21, 2008
474 # @return [Hash]
475 # names and values of attributes assigned
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
476 #
c5ecb84 moving/rewriting docs in newest yard style
Adam French authored Jun 19, 2008
477 # @api public
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
478 def attributes=(attributes)
479 attributes.each do |name,value|
fbf3707 Minor code cleanup
Dan Kubb authored Jan 8, 2009
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 Nov 24, 2008
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 Nov 19, 2008
483 send(setter, value)
c398725 Removed :public, :public, :protected PROPERTY_OPTIONS
David James authored Jun 26, 2008
484 else
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
485 raise ArgumentError, "The property '#{name}' is not accessible in #{self.class}"
c398725 Removed :public, :public, :protected PROPERTY_OPTIONS
David James authored Jun 27, 2008
486 end
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored Mar 3, 2008
487 end
488 end
1260c12 Updated Resource#initialize to always validate the resource
Dan Kubb authored May 9, 2008
489
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
490 ##
491 # Deprecated API for updating attributes and saving Resource
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
492 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
493 # @see #update
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
494 #
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
495 # @api public
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored Jan 15, 2009
496 def update_attributes(attributes = {}, *allowed)
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
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 Jan 15, 2009
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 Nov 19, 2008
506 end
507
508 ##
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
509 # Updates attributes and saves this Resource instance
72db385 @advany update_attributes should save model
advany authored May 19, 2008
510 #
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored Jan 15, 2009
511 # @param [Hash] attributes
512 # attributes to be updated
78daf51 @advany add docs to DataMapper::Resource
advany authored May 30, 2008
513 #
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored Jan 15, 2009
514 # @return [TrueClass, FalseClass]
515 # true if resource and storage state match
72db385 @advany update_attributes should save model
advany authored May 19, 2008
516 #
8708119 @myabc Convert docs to YARD-style
myabc authored May 20, 2008
517 # @api public
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored Jan 15, 2009
518 def update(attributes = {})
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
519 assert_kind_of 'attributes', attributes, Hash
520
a19fc30 Removed optional allowed Array to Resource#update and Collection#update
Dan Kubb authored Jan 15, 2009
521 self.attributes = attributes
d3183e6 Deprecated Resource#update_attributes and renamed as Resource#update
Dan Kubb authored Nov 19, 2008
522
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 28, 2008
523 _update
1019ac3 @sam Added key validation to Resources, and added Resource#update_attributes.
sam authored May 8, 2008
524 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
525
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 23, 2008
526 ##
527 # Save the instance and associated children to the data-store.
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
528 #
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
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 Dec 28, 2008
542 unless saved = new_record? ? _create : _update
3748f80 Updated Resource#save logic to short-circuit if unable to create or u…
Dan Kubb authored Nov 23, 2008
543 return false
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
544 end
545
49e210f Removed Relationship#get_children and Relationship#get_parent
Dan Kubb authored Dec 12, 2008
546 child_associations.all? { |a| a.save }
45bbaf1 Group Resource#save and Resource#destroy near other persistence methods
Dan Kubb authored Nov 24, 2008
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 Jan 6, 2009
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 Nov 24, 2008
563 end
564
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
565 # Gets a Query that will return this Resource instance
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
566 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
567 # @return [Query] Query that will retrieve this Resource instance
f132afb @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
568 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
569 # @api private
fbf3707 Minor code cleanup
Dan Kubb authored Jan 8, 2009
570 def to_query
571 model.to_query(repository, key)
e3ad4a2 Improved the transactional system greatly and introduced two-phase
Martin Kihlgren authored Apr 25, 2008
572 end
573
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 20, 2008
574 ##
575 # Reset the Resource to a similar state as a new record
576 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
577 # @api private
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
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 Jun 16, 2008
582 end
583
06e66e6 Refactored Resource resetting code after destruction
Dan Kubb authored Nov 21, 2008
584 protected
ec84ce4 @myabc Stripped whitespace with "sake strip"
myabc authored Jun 20, 2008
585
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
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 Jun 19, 2008
590 # Needs to be a protected method so that it is hookable
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
591 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
592 # @return [TrueClass, FalseClass]
593 # true if the receiver was successfully created
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
594 #
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 28, 2008
595 # @api semipublic
596 def _create
3558f1d @dbussink Fix that saving a non-dirty object returns true
dbussink authored Oct 9, 2008
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 Jan 15, 2009
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 Nov 20, 2008
601
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored Jun 11, 2008
602 # set defaults for new resource
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
603 properties.each do |property|
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
604 unless property.serial? || property.loaded?(self)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
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 Mar 3, 2008
607 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
608
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
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 Jun 12, 2008
612
613 @repository = repository
614 @new_record = false
615
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
616 original_values.clear
617
19f15ea Simplified IdentityMap to be a subclass of Hash
Dan Kubb authored Nov 21, 2008
618 repository.identity_map(model)[key] = self
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored Jun 12, 2008
619
620 true
e58ed2e @sam Lots and lots of changes to move things over to the new Resource modu…
sam authored Mar 3, 2008
621 end
15987f0 @david Merge branch 'master' of git@github.com:sam/dm-core
david authored Mar 28, 2008
622
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 28, 2008
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 Jan 15, 2009
629 if dirty_attributes.empty?
630 true
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
631 elsif dirty_attributes.any? { |p,v| !p.nullable? && v.nil? }
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
632 false
633 elsif repository.update(dirty_attributes, to_query) != 1
634 false
635 else
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
636 original_values.clear
637
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
638 repository.identity_map(model)[key] = self
377098b Saving a Resource with a new key updates the Identity Map
Dan Kubb authored Jan 11, 2009
639
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
640 true
641 end
bc3dd47 Initial commit of new Many To Many Relationship
Dan Kubb authored Dec 28, 2008
642 end
643
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
644 # Gets this instance's Model's properties
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
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 Nov 21, 2008
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 Nov 21, 2008
654 # Gets this instance's Model's relationships
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
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 Nov 21, 2008
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 Jun 19, 2008
664 private
665
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
666 ##
667 # Initialize a new instance of this Resource using the provided values
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
668 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
669 # @param [Hash] attributes
670 # attribute values to use for the new instance
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
671 #
43ce436 @emmanuel Updated Resource API docs to YARD format.
emmanuel authored Nov 21, 2008
672 # @return [Resource]
673 # the newly initialized resource instance
6186835 Stripped whitespace
Dan Kubb authored Nov 24, 2008
674 #
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
675 # @api public
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
676 def initialize(attributes = {}) # :nodoc:
677 assert_valid_model
8201c42 Explicitly set @new_record on Resource initialization
Dan Kubb authored Nov 24, 2008
678 @new_record = true
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
679 self.attributes = attributes
680 end
0ac6b42 Stripped whitespace with sake strip
Dan Kubb authored Sep 11, 2008
681
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
682 # TODO: move to Model#assert_valid
25e6fe5 @emmanuel Began updating Resource API docs to YARD format (still in progress).
emmanuel authored Nov 20, 2008
683 # @api private
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
684 def assert_valid_model # :nodoc:
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
685 if self.class._valid_model
686 return
687 end
688
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
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 Sep 8, 2008
698
7a8b498 @dbussink Performance refactoring
dbussink authored Sep 14, 2008
699 self.class.instance_variable_set("@_valid_model", true)
887befd Removed Hook - It has been moved to extlib
Carl Lerche authored Jun 19, 2008
700 end
701
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
702 # TODO: document
703 # @api private
c073732 Updated Repository to be a thinner wrapper around the adapter
Dan Kubb authored Jun 12, 2008
704 def lazy_load(name)
a1f99a9 Updated lazy load to only load non-loaded attributes
Dan Kubb authored Jul 11, 2008
705 reload_attributes(*properties.lazy_load_context(name) - loaded_attributes)
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
706 end
707
f0c4d00 Updated @api tags for Resource methods
Dan Kubb authored Nov 17, 2008
708 # TODO: document
709 # @api private
3684f20 Minor Resource cleanup
Dan Kubb authored Jun 16, 2008
710 def child_associations
711 @child_associations ||= []
712 end
713
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
714 ##
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
715 # Return true if the accesor or mutator +method+ is publicly accessible
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
716 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
717 # @param [String, Symbol] method
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
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 Nov 28, 2008
721 # true if the accessor or mutator +method+ is public
7faa940 Updated Resource#attributes= to only use public mutators
Dan Kubb authored Nov 24, 2008
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 Nov 25, 2008
728 ##
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
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 Nov 28, 2008
730 #
731 # @param [Resource] other
732 # The Resource whose attributes are to be compared with +self+'s
733 #
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
734 # @param [Symbol] operator
735 # The comparison operator to use to compare the attributes
736 #
77c0e33 Replaced "Array<Member, Classes>" type notation with "Array(Member, C…
Emmanuel Gomez authored Nov 28, 2008
737 # @return [TrueClass, FalseClass]
738 # The result of the comparison of +other+'s attributes with +self+'s
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored Nov 25, 2008
739 #
d478c13 Minor documentation update in Resource
Dan Kubb authored Jan 24, 2009
740 # @api private
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
741 def cmp_attributes?(other, operator)
742 unless key.send(operator, other.key)
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
743 return false
744 end
745
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
746 if repository.send(operator, other.repository) && !dirty? && !other.dirty?
b050d8f Unrolled if/unless conditions to allow easier coverage testing
Dan Kubb authored Jan 15, 2009
747 return true
748 end
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored Dec 12, 2008
749
05bae27 Refactoring to improve spec coverage
Dan Kubb authored Jan 18, 2009
750 # get all the loaded and non-loaded properties that are not keys,
751 # since the key comparison was performed earlier
752 loaded, not_loaded = properties.select { |p| !p.key? }.partition do |property|
4260cb7 Simplified Property and Resource
Dan Kubb authored Jan 6, 2009
753 property.loaded?(self) && property.loaded?(other)
0380616 Removed ManyToMany::Proxy (to be rebuilt)
Dan Kubb authored Dec 12, 2008
754 end
755
756 # check all loaded properties, and then all unloaded properties
b88cab2 Added Query semipublic specs
Dan Kubb authored Jan 25, 2009
757 (loaded + not_loaded).all? { |p| p.get(self).send(operator, p.get(other)) }
7d1befe Refactored equivalent property checks from Resource#eql? and Resource#==
Dan Kubb authored Nov 25, 2008
758 end
0ba594d @myabc Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataM…
myabc authored Apr 3, 2008
759 end # module Resource
760 end # module DataMapper
Something went wrong with that request. Please try again.