dm-core 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +22 -13
- data/README.rdoc +1 -1
- data/Rakefile +1 -2
- data/VERSION +1 -1
- data/dm-core.gemspec +29 -175
- data/lib/dm-core/adapters/abstract_adapter.rb +1 -2
- data/lib/dm-core/associations/many_to_many.rb +11 -5
- data/lib/dm-core/associations/many_to_one.rb +17 -2
- data/lib/dm-core/associations/one_to_many.rb +16 -0
- data/lib/dm-core/backwards.rb +13 -0
- data/lib/dm-core/collection.rb +1 -1
- data/lib/dm-core/model/property.rb +24 -27
- data/lib/dm-core/model/relationship.rb +22 -28
- data/lib/dm-core/model.rb +99 -41
- data/lib/dm-core/property/binary.rb +15 -0
- data/lib/dm-core/property/boolean.rb +6 -10
- data/lib/dm-core/property/date.rb +0 -2
- data/lib/dm-core/property/date_time.rb +0 -2
- data/lib/dm-core/property/decimal.rb +7 -3
- data/lib/dm-core/property/discriminator.rb +24 -26
- data/lib/dm-core/property/float.rb +5 -1
- data/lib/dm-core/property/numeric.rb +6 -9
- data/lib/dm-core/property/string.rb +2 -1
- data/lib/dm-core/property/time.rb +0 -2
- data/lib/dm-core/property/typecast/time.rb +7 -2
- data/lib/dm-core/property.rb +37 -50
- data/lib/dm-core/property_set.rb +1 -3
- data/lib/dm-core/query/conditions/comparison.rb +5 -1
- data/lib/dm-core/query/conditions/operation.rb +1 -1
- data/lib/dm-core/query.rb +3 -10
- data/lib/dm-core/relationship_set.rb +0 -2
- data/lib/dm-core/resource/{state → persistence_state}/clean.rb +4 -4
- data/lib/dm-core/resource/{state → persistence_state}/deleted.rb +2 -2
- data/lib/dm-core/resource/{state → persistence_state}/dirty.rb +2 -2
- data/lib/dm-core/resource/{state → persistence_state}/immutable.rb +3 -3
- data/lib/dm-core/resource/{state → persistence_state}/persisted.rb +3 -3
- data/lib/dm-core/resource/{state → persistence_state}/transient.rb +3 -3
- data/lib/dm-core/resource/{state.rb → persistence_state.rb} +2 -2
- data/lib/dm-core/resource.rb +27 -28
- data/lib/dm-core/spec/lib/adapter_helpers.rb +2 -5
- data/lib/dm-core/spec/setup.rb +3 -2
- data/lib/dm-core/spec/shared/public/property_spec.rb +8 -0
- data/lib/dm-core/spec/shared/resource_spec.rb +14 -0
- data/lib/dm-core/spec/shared/semipublic/property_spec.rb +5 -11
- data/lib/dm-core/support/descendant_set.rb +0 -2
- data/lib/dm-core/support/ext/array.rb +0 -19
- data/lib/dm-core/support/ext/blank.rb +1 -0
- data/lib/dm-core/support/hook.rb +0 -3
- data/lib/dm-core/support/naming_conventions.rb +6 -0
- data/lib/dm-core/support/ordered_set.rb +0 -2
- data/lib/dm-core/support/subject_set.rb +0 -2
- data/lib/dm-core/version.rb +1 -1
- data/lib/dm-core.rb +32 -67
- data/spec/public/associations/many_to_many_spec.rb +0 -1
- data/spec/public/model/property_spec.rb +55 -9
- data/spec/public/model/relationship_spec.rb +24 -2
- data/spec/public/model_spec.rb +32 -0
- data/spec/public/property/binary_spec.rb +33 -6
- data/spec/public/property/boolean_spec.rb +14 -6
- data/spec/public/property/class_spec.rb +14 -6
- data/spec/public/property/date_spec.rb +14 -6
- data/spec/public/property/date_time_spec.rb +14 -6
- data/spec/public/property/decimal_spec.rb +10 -2
- data/spec/public/property/discriminator_spec.rb +15 -1
- data/spec/public/property/float_spec.rb +14 -6
- data/spec/public/property/integer_spec.rb +14 -6
- data/spec/public/property/object_spec.rb +8 -0
- data/spec/public/property/serial_spec.rb +14 -6
- data/spec/public/property/string_spec.rb +14 -6
- data/spec/public/property/text_spec.rb +14 -6
- data/spec/public/property/time_spec.rb +14 -6
- data/spec/public/resource_spec.rb +58 -0
- data/spec/public/shared/finder_shared_spec.rb +8 -4
- data/spec/semipublic/associations/many_to_many_spec.rb +2 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +2 -0
- data/spec/semipublic/associations/one_to_many_spec.rb +2 -0
- data/spec/semipublic/associations/one_to_one_spec.rb +2 -0
- data/spec/semipublic/property/binary_spec.rb +5 -5
- data/spec/semipublic/property/boolean_spec.rb +5 -23
- data/spec/semipublic/property/class_spec.rb +5 -5
- data/spec/semipublic/property/date_spec.rb +5 -5
- data/spec/semipublic/property/date_time_spec.rb +5 -5
- data/spec/semipublic/property/decimal_spec.rb +2 -2
- data/spec/semipublic/property/discriminator_spec.rb +5 -5
- data/spec/semipublic/property/float_spec.rb +5 -5
- data/spec/semipublic/property/integer_spec.rb +5 -5
- data/spec/semipublic/property/lookup_spec.rb +3 -3
- data/spec/semipublic/property/serial_spec.rb +5 -5
- data/spec/semipublic/property/string_spec.rb +5 -5
- data/spec/semipublic/property/text_spec.rb +5 -5
- data/spec/semipublic/property/time_spec.rb +5 -5
- data/spec/semipublic/query/conditions/comparison_spec.rb +44 -4
- data/spec/semipublic/query_spec.rb +2 -11
- data/spec/semipublic/resource/state/clean_spec.rb +6 -6
- data/spec/semipublic/resource/state/deleted_spec.rb +4 -4
- data/spec/semipublic/resource/state/dirty_spec.rb +8 -8
- data/spec/semipublic/resource/state/immutable_spec.rb +6 -6
- data/spec/semipublic/resource/state/transient_spec.rb +5 -5
- data/spec/semipublic/resource/state_spec.rb +15 -15
- data/spec/semipublic/shared/resource_shared_spec.rb +7 -1
- data/spec/semipublic/shared/resource_state_shared_spec.rb +8 -8
- data/spec/unit/array_spec.rb +0 -14
- data/spec/unit/blank_spec.rb +11 -0
- metadata +52 -174
- data/lib/dm-core/support/inflector.rb +0 -3
data/lib/dm-core/property.rb
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
require 'dm-core/resource'
|
|
2
|
-
require 'dm-core/query'
|
|
3
|
-
|
|
4
1
|
module DataMapper
|
|
5
2
|
# = Properties
|
|
6
3
|
# Properties for a model are not derived from a database structure, but
|
|
@@ -83,11 +80,13 @@ module DataMapper
|
|
|
83
80
|
# property :title, String
|
|
84
81
|
#
|
|
85
82
|
# def title=(new_title)
|
|
86
|
-
# raise ArgumentError if new_title != '
|
|
87
|
-
#
|
|
83
|
+
# raise ArgumentError if new_title != 'Lee is l337'
|
|
84
|
+
# super(new_title)
|
|
88
85
|
# end
|
|
89
86
|
# end
|
|
90
87
|
#
|
|
88
|
+
# Calling super ensures that any validators defined for the property are kept active.
|
|
89
|
+
#
|
|
91
90
|
# == Lazy Loading
|
|
92
91
|
# By default, some properties are not loaded when an object is fetched in
|
|
93
92
|
# DataMapper. These lazily loaded properties are fetched on demand when their
|
|
@@ -107,7 +106,7 @@ module DataMapper
|
|
|
107
106
|
#
|
|
108
107
|
# If you want to over-ride the lazy loading on any field you can set it to a
|
|
109
108
|
# context or false to disable it with the :lazy option. Contexts allow
|
|
110
|
-
#
|
|
109
|
+
# multiple lazy properties to be loaded at one time. If you set :lazy to
|
|
111
110
|
# true, it is placed in the :default context
|
|
112
111
|
#
|
|
113
112
|
# class Post
|
|
@@ -179,44 +178,40 @@ module DataMapper
|
|
|
179
178
|
#
|
|
180
179
|
# == Inferred Validations
|
|
181
180
|
# If you require the dm-validations plugin, auto-validations will
|
|
182
|
-
# automatically be mixed-in in to your model classes:
|
|
183
|
-
#
|
|
184
|
-
# specific column restrictions.
|
|
181
|
+
# automatically be mixed-in in to your model classes: validation rules that
|
|
182
|
+
# are inferred when properties are declared with specific column restrictions.
|
|
185
183
|
#
|
|
186
|
-
#
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
# property :title, String, :length => 250
|
|
190
|
-
# # => infers 'validates_length :title,
|
|
191
|
-
# :minimum => 0, :maximum => 250'
|
|
184
|
+
# class Post
|
|
185
|
+
# include DataMapper::Resource
|
|
192
186
|
#
|
|
193
|
-
#
|
|
194
|
-
#
|
|
187
|
+
# property :title, String, :length => 250, :min => 0, :max => 250
|
|
188
|
+
# # => infers 'validates_length :title'
|
|
195
189
|
#
|
|
196
|
-
#
|
|
197
|
-
#
|
|
190
|
+
# property :title, String, :required => true
|
|
191
|
+
# # => infers 'validates_present :title'
|
|
198
192
|
#
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
# # 'validates_present'
|
|
202
|
-
# # better: property :title, String, :length => 1..255
|
|
193
|
+
# property :email, String, :format => :email_address
|
|
194
|
+
# # => infers 'validates_format :email, :with => :email_address'
|
|
203
195
|
#
|
|
204
|
-
#
|
|
196
|
+
# property :title, String, :length => 255, :required => true
|
|
197
|
+
# # => infers both 'validates_length' as well as 'validates_present'
|
|
198
|
+
# # better: property :title, String, :length => 1..255
|
|
199
|
+
# end
|
|
205
200
|
#
|
|
206
201
|
# This functionality is available with the dm-validations gem. For more information
|
|
207
202
|
# about validations, check the documentation for dm-validations.
|
|
208
203
|
#
|
|
209
204
|
# == Default Values
|
|
210
|
-
# To set a default for a property, use the <tt>:default</tt> key.
|
|
205
|
+
# To set a default for a property, use the <tt>:default</tt> key. The
|
|
211
206
|
# property will be set to the value associated with that key the first time
|
|
212
207
|
# it is accessed, or when the resource is saved if it hasn't been set with
|
|
213
|
-
# another value already.
|
|
208
|
+
# another value already. This value can be a static value, such as 'hello'
|
|
214
209
|
# but it can also be a proc that will be evaluated when the property is read
|
|
215
|
-
# before its value has been set.
|
|
216
|
-
# proc.
|
|
210
|
+
# before its value has been set. The property is set to the return of the
|
|
211
|
+
# proc. The proc is passed two values, the resource the property is being set
|
|
217
212
|
# for and the property itself.
|
|
218
213
|
#
|
|
219
|
-
# property :display_name, String, :default => { |resource, property| resource.login }
|
|
214
|
+
# property :display_name, String, :default => lambda { |resource, property| resource.login }
|
|
220
215
|
#
|
|
221
216
|
# Word of warning. Don't try to read the value of the property you're setting
|
|
222
217
|
# the default for in the proc. An infinite loop will ensue.
|
|
@@ -270,9 +265,6 @@ module DataMapper
|
|
|
270
265
|
# Only makes sense for float type properties. Must be > 0.
|
|
271
266
|
# Default is nil for Float type and 10 for BigDecimal
|
|
272
267
|
#
|
|
273
|
-
# All other keys you pass to +property+ method are stored and available
|
|
274
|
-
# as options[:extra_keys].
|
|
275
|
-
#
|
|
276
268
|
# == Overriding default Property options
|
|
277
269
|
#
|
|
278
270
|
# There is the ability to reconfigure a Property and it's subclasses by explicitly
|
|
@@ -291,7 +283,7 @@ module DataMapper
|
|
|
291
283
|
# DataMapper::Property.auto_validation(false)
|
|
292
284
|
#
|
|
293
285
|
# # set all mutator methods to be private by default
|
|
294
|
-
# DataMapper::Property.writer(
|
|
286
|
+
# DataMapper::Property.writer(:private)
|
|
295
287
|
#
|
|
296
288
|
# Please note that this has no effect when a subclass has explicitly
|
|
297
289
|
# defined it's own option. For example, setting the String length to
|
|
@@ -311,8 +303,7 @@ module DataMapper
|
|
|
311
303
|
module PassThroughLoadDump
|
|
312
304
|
# @api semipublic
|
|
313
305
|
def load(value)
|
|
314
|
-
|
|
315
|
-
typecast(value)
|
|
306
|
+
typecast(value) unless value.nil?
|
|
316
307
|
end
|
|
317
308
|
|
|
318
309
|
# Stub instance method for dumping
|
|
@@ -329,10 +320,9 @@ module DataMapper
|
|
|
329
320
|
|
|
330
321
|
include DataMapper::Assertions
|
|
331
322
|
include Subject
|
|
332
|
-
extend Chainable
|
|
333
323
|
extend Equalizer
|
|
334
324
|
|
|
335
|
-
equalize :model, :name
|
|
325
|
+
equalize :model, :name, :options
|
|
336
326
|
|
|
337
327
|
PRIMITIVES = [
|
|
338
328
|
TrueClass,
|
|
@@ -357,7 +347,10 @@ module DataMapper
|
|
|
357
347
|
VISIBILITY_OPTIONS = [ :public, :protected, :private ].to_set.freeze
|
|
358
348
|
|
|
359
349
|
# Invalid property names
|
|
360
|
-
INVALID_NAMES = (Resource.instance_methods +
|
|
350
|
+
INVALID_NAMES = (Resource.instance_methods +
|
|
351
|
+
Resource.private_instance_methods +
|
|
352
|
+
Query::OPTIONS.to_a
|
|
353
|
+
).map { |name| name.to_s }
|
|
361
354
|
|
|
362
355
|
attr_reader :primitive, :model, :name, :instance_variable_name,
|
|
363
356
|
:reader_visibility, :writer_visibility, :options,
|
|
@@ -462,9 +455,8 @@ module DataMapper
|
|
|
462
455
|
# @api public
|
|
463
456
|
def options
|
|
464
457
|
options = {}
|
|
465
|
-
accepted_options.each do |
|
|
466
|
-
|
|
467
|
-
options[method] = value unless value.nil?
|
|
458
|
+
accepted_options.each do |name|
|
|
459
|
+
options[name] = send(name) if instance_variable_defined?("@#{name}")
|
|
468
460
|
end
|
|
469
461
|
options
|
|
470
462
|
end
|
|
@@ -737,20 +729,15 @@ module DataMapper
|
|
|
737
729
|
value.kind_of?(primitive)
|
|
738
730
|
end
|
|
739
731
|
|
|
740
|
-
|
|
741
|
-
def self.new(model, name, options = {})
|
|
742
|
-
super
|
|
743
|
-
end
|
|
744
|
-
end
|
|
745
|
-
|
|
746
|
-
protected
|
|
732
|
+
protected
|
|
747
733
|
|
|
748
734
|
# @api semipublic
|
|
749
735
|
def initialize(model, name, options = {})
|
|
750
736
|
options = options.to_hash.dup
|
|
751
737
|
|
|
752
|
-
if INVALID_NAMES.include?(name.to_s)
|
|
753
|
-
raise ArgumentError,
|
|
738
|
+
if INVALID_NAMES.include?(name.to_s) || (kind_of?(Boolean) && INVALID_NAMES.include?("#{name}?"))
|
|
739
|
+
raise ArgumentError,
|
|
740
|
+
"+name+ was #{name.inspect}, which cannot be used as a property name since it collides with an existing method or a query option"
|
|
754
741
|
end
|
|
755
742
|
|
|
756
743
|
assert_valid_options(options)
|
data/lib/dm-core/property_set.rb
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'dm-core/support/subject_set'
|
|
2
|
-
|
|
3
1
|
module DataMapper
|
|
4
2
|
# Set of Property objects, used to associate
|
|
5
3
|
# queries with set of fields it performed over,
|
|
@@ -144,7 +142,7 @@ module DataMapper
|
|
|
144
142
|
|
|
145
143
|
# @api private
|
|
146
144
|
def field_map
|
|
147
|
-
|
|
145
|
+
Hash[ map { |property| [ property.field, property ] } ]
|
|
148
146
|
end
|
|
149
147
|
|
|
150
148
|
def inspect
|
|
@@ -102,7 +102,7 @@ module DataMapper
|
|
|
102
102
|
class AbstractComparison
|
|
103
103
|
extend Equalizer
|
|
104
104
|
|
|
105
|
-
equalize :
|
|
105
|
+
equalize :subject, :value
|
|
106
106
|
|
|
107
107
|
# @api semipublic
|
|
108
108
|
attr_accessor :parent
|
|
@@ -797,6 +797,7 @@ module DataMapper
|
|
|
797
797
|
#
|
|
798
798
|
# @api semipublic
|
|
799
799
|
def matches?(record)
|
|
800
|
+
return false if expected.nil?
|
|
800
801
|
record_value = record_value(record)
|
|
801
802
|
!record_value.nil? && record_value > expected
|
|
802
803
|
end
|
|
@@ -827,6 +828,7 @@ module DataMapper
|
|
|
827
828
|
#
|
|
828
829
|
# @api semipublic
|
|
829
830
|
def matches?(record)
|
|
831
|
+
return false if expected.nil?
|
|
830
832
|
record_value = record_value(record)
|
|
831
833
|
!record_value.nil? && record_value < expected
|
|
832
834
|
end
|
|
@@ -857,6 +859,7 @@ module DataMapper
|
|
|
857
859
|
#
|
|
858
860
|
# @api semipublic
|
|
859
861
|
def matches?(record)
|
|
862
|
+
return false if expected.nil?
|
|
860
863
|
record_value = record_value(record)
|
|
861
864
|
!record_value.nil? && record_value >= expected
|
|
862
865
|
end
|
|
@@ -885,6 +888,7 @@ module DataMapper
|
|
|
885
888
|
#
|
|
886
889
|
# @api semipublic
|
|
887
890
|
def matches?(record)
|
|
891
|
+
return false if expected.nil?
|
|
888
892
|
record_value = record_value(record)
|
|
889
893
|
!record_value.nil? && record_value <= expected
|
|
890
894
|
end
|
data/lib/dm-core/query.rb
CHANGED
|
@@ -1120,19 +1120,12 @@ module DataMapper
|
|
|
1120
1120
|
|
|
1121
1121
|
# Normalize the unique attribute
|
|
1122
1122
|
#
|
|
1123
|
-
# If any links are present, and the unique attribute was not
|
|
1124
|
-
# specified, then make sure the query is marked as unique
|
|
1125
|
-
# to this rule is links where there is known to be 0..1 records on the
|
|
1126
|
-
# the other side, such as belongs_to or a has 1. In this case, there
|
|
1127
|
-
# is no need to explicitly mark the query unique. This in turn allows
|
|
1128
|
-
# sane ordering on fields of the links, since no redundant GROUP BY
|
|
1129
|
-
# clause will be generated.
|
|
1123
|
+
# If any links are present, and the unique attribute was not
|
|
1124
|
+
# explicitly specified, then make sure the query is marked as unique
|
|
1130
1125
|
#
|
|
1131
1126
|
# @api private
|
|
1132
1127
|
def normalize_unique
|
|
1133
|
-
unless @options.key?(:unique)
|
|
1134
|
-
@unique = @links.reject {|x| x.min.between?(0, 1) && x.max == 1 }.any?
|
|
1135
|
-
end
|
|
1128
|
+
@unique = links.any? unless @options.key?(:unique)
|
|
1136
1129
|
end
|
|
1137
1130
|
|
|
1138
1131
|
# Append conditions to this Query
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Resource
|
|
3
|
-
class
|
|
3
|
+
class PersistenceState
|
|
4
4
|
|
|
5
5
|
# a persisted/unmodified resource
|
|
6
6
|
class Clean < Persisted
|
|
@@ -8,10 +8,10 @@ module DataMapper
|
|
|
8
8
|
if not_modified?(subject, value)
|
|
9
9
|
self
|
|
10
10
|
else
|
|
11
|
-
# assign to
|
|
11
|
+
# assign to persistence_state so that if Dirty#set calls
|
|
12
12
|
# a Relationship#set, which modifies a Property, the same
|
|
13
13
|
# Dirty state instance will be reused.
|
|
14
|
-
state = resource.
|
|
14
|
+
state = resource.persistence_state = Dirty.new(resource)
|
|
15
15
|
state.set(subject, value)
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -35,6 +35,6 @@ module DataMapper
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
end # class Clean
|
|
38
|
-
end # class
|
|
38
|
+
end # class PersistenceState
|
|
39
39
|
end # module Resource
|
|
40
40
|
end # module DataMapper
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Resource
|
|
3
|
-
class
|
|
3
|
+
class PersistenceState
|
|
4
4
|
|
|
5
5
|
# a persisted/deleted resource
|
|
6
6
|
class Deleted < Persisted
|
|
@@ -25,6 +25,6 @@ module DataMapper
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
end # class Deleted
|
|
28
|
-
end # class
|
|
28
|
+
end # class PersistenceState
|
|
29
29
|
end # module Resource
|
|
30
30
|
end # module DataMapper
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Resource
|
|
3
|
-
class
|
|
3
|
+
class PersistenceState
|
|
4
4
|
|
|
5
5
|
# a persisted/dirty resource
|
|
6
6
|
class Dirty < Persisted
|
|
@@ -91,6 +91,6 @@ module DataMapper
|
|
|
91
91
|
end
|
|
92
92
|
|
|
93
93
|
end # class Dirty
|
|
94
|
-
end # class
|
|
94
|
+
end # class PersistenceState
|
|
95
95
|
end # module Resource
|
|
96
96
|
end # module DataMapper
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Resource
|
|
3
|
-
class
|
|
3
|
+
class PersistenceState
|
|
4
4
|
|
|
5
5
|
# a not-persisted/unmodifiable resource
|
|
6
|
-
class Immutable <
|
|
6
|
+
class Immutable < PersistenceState
|
|
7
7
|
def get(subject, *args)
|
|
8
8
|
unless subject.loaded?(resource) || subject.kind_of?(Associations::Relationship)
|
|
9
9
|
raise ImmutableError, 'Immutable resource cannot be lazy loaded'
|
|
@@ -29,6 +29,6 @@ module DataMapper
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
end # class Immutable
|
|
32
|
-
end # class
|
|
32
|
+
end # class PersistenceState
|
|
33
33
|
end # module Resource
|
|
34
34
|
end # module DataMapper
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Resource
|
|
3
|
-
class
|
|
3
|
+
class PersistenceState
|
|
4
4
|
|
|
5
5
|
# a persisted resource (abstract)
|
|
6
|
-
class Persisted <
|
|
6
|
+
class Persisted < PersistenceState
|
|
7
7
|
def get(subject, *args)
|
|
8
8
|
lazy_load(subject)
|
|
9
9
|
super
|
|
@@ -24,6 +24,6 @@ module DataMapper
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
end # class Persisted
|
|
27
|
-
end # class
|
|
27
|
+
end # class PersistenceState
|
|
28
28
|
end # module Resource
|
|
29
29
|
end # module DataMapper
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
module DataMapper
|
|
2
2
|
module Resource
|
|
3
|
-
class
|
|
3
|
+
class PersistenceState
|
|
4
4
|
|
|
5
5
|
# a not-persisted/modifiable resource
|
|
6
|
-
class Transient <
|
|
6
|
+
class Transient < PersistenceState
|
|
7
7
|
def get(subject, *args)
|
|
8
8
|
set_default_value(subject)
|
|
9
9
|
super
|
|
@@ -73,6 +73,6 @@ module DataMapper
|
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
end # class Transient
|
|
76
|
-
end # class
|
|
76
|
+
end # class PersistenceState
|
|
77
77
|
end # module Resource
|
|
78
78
|
end # module DataMapper
|
|
@@ -2,7 +2,7 @@ module DataMapper
|
|
|
2
2
|
module Resource
|
|
3
3
|
|
|
4
4
|
# the state of the resource (abstract)
|
|
5
|
-
class
|
|
5
|
+
class PersistenceState
|
|
6
6
|
extend Equalizer
|
|
7
7
|
|
|
8
8
|
equalize :resource
|
|
@@ -70,6 +70,6 @@ module DataMapper
|
|
|
70
70
|
set(relationship, get(relationship))
|
|
71
71
|
end
|
|
72
72
|
|
|
73
|
-
end # class
|
|
73
|
+
end # class PersistenceState
|
|
74
74
|
end # module Resource
|
|
75
75
|
end # module DataMapper
|
data/lib/dm-core/resource.rb
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
# TODO: DRY up raise_on_save_failure with attr_accessor_with_default
|
|
2
|
-
# once AS branch is merged in
|
|
3
|
-
|
|
4
1
|
module DataMapper
|
|
5
2
|
module Resource
|
|
6
3
|
include DataMapper::Assertions
|
|
7
|
-
extend Chainable
|
|
8
4
|
|
|
9
5
|
# @deprecated
|
|
10
6
|
def self.append_inclusions(*inclusions)
|
|
@@ -80,24 +76,24 @@ module DataMapper
|
|
|
80
76
|
|
|
81
77
|
# Get the persisted state for the resource
|
|
82
78
|
#
|
|
83
|
-
# @return [Resource::
|
|
79
|
+
# @return [Resource::PersistenceState]
|
|
84
80
|
# the current persisted state for the resource
|
|
85
81
|
#
|
|
86
82
|
# @api private
|
|
87
|
-
def
|
|
88
|
-
@
|
|
83
|
+
def persistence_state
|
|
84
|
+
@_persistence_state ||= Resource::PersistenceState::Transient.new(self)
|
|
89
85
|
end
|
|
90
86
|
|
|
91
87
|
# Set the persisted state for the resource
|
|
92
88
|
#
|
|
93
|
-
# @param [Resource::
|
|
89
|
+
# @param [Resource::PersistenceState]
|
|
94
90
|
# the new persisted state for the resource
|
|
95
91
|
#
|
|
96
92
|
# @return [undefined]
|
|
97
93
|
#
|
|
98
94
|
# @api private
|
|
99
|
-
def
|
|
100
|
-
@
|
|
95
|
+
def persistence_state=(state)
|
|
96
|
+
@_persistence_state = state
|
|
101
97
|
end
|
|
102
98
|
|
|
103
99
|
# Test if the persisted state is set
|
|
@@ -106,8 +102,8 @@ module DataMapper
|
|
|
106
102
|
# true if the persisted state is set
|
|
107
103
|
#
|
|
108
104
|
# @api private
|
|
109
|
-
def
|
|
110
|
-
defined?(@
|
|
105
|
+
def persistence_state?
|
|
106
|
+
defined?(@_persistence_state) ? true : false
|
|
111
107
|
end
|
|
112
108
|
|
|
113
109
|
# Repository this resource belongs to in the context of this collection
|
|
@@ -153,7 +149,7 @@ module DataMapper
|
|
|
153
149
|
#
|
|
154
150
|
# @api public
|
|
155
151
|
def new?
|
|
156
|
-
|
|
152
|
+
persistence_state.kind_of?(PersistenceState::Transient)
|
|
157
153
|
end
|
|
158
154
|
|
|
159
155
|
# Checks if this Resource instance is saved
|
|
@@ -163,7 +159,7 @@ module DataMapper
|
|
|
163
159
|
#
|
|
164
160
|
# @api public
|
|
165
161
|
def saved?
|
|
166
|
-
|
|
162
|
+
persistence_state.kind_of?(PersistenceState::Persisted)
|
|
167
163
|
end
|
|
168
164
|
|
|
169
165
|
# Checks if this Resource instance is destroyed
|
|
@@ -183,7 +179,8 @@ module DataMapper
|
|
|
183
179
|
#
|
|
184
180
|
# @api public
|
|
185
181
|
def clean?
|
|
186
|
-
|
|
182
|
+
persistence_state.kind_of?(PersistenceState::Clean) ||
|
|
183
|
+
persistence_state.kind_of?(PersistenceState::Immutable)
|
|
187
184
|
end
|
|
188
185
|
|
|
189
186
|
# Checks if the resource has unsaved changes
|
|
@@ -205,7 +202,7 @@ module DataMapper
|
|
|
205
202
|
#
|
|
206
203
|
# @api public
|
|
207
204
|
def readonly?
|
|
208
|
-
|
|
205
|
+
persistence_state.kind_of?(PersistenceState::Immutable)
|
|
209
206
|
end
|
|
210
207
|
|
|
211
208
|
# Returns the value of the attribute.
|
|
@@ -240,7 +237,8 @@ module DataMapper
|
|
|
240
237
|
#
|
|
241
238
|
# @api public
|
|
242
239
|
def attribute_get(name)
|
|
243
|
-
|
|
240
|
+
property = properties[name]
|
|
241
|
+
persistence_state.get(property) if property
|
|
244
242
|
end
|
|
245
243
|
|
|
246
244
|
alias_method :[], :attribute_get
|
|
@@ -281,7 +279,8 @@ module DataMapper
|
|
|
281
279
|
#
|
|
282
280
|
# @api public
|
|
283
281
|
def attribute_set(name, value)
|
|
284
|
-
|
|
282
|
+
property = properties[name]
|
|
283
|
+
self.persistence_state = persistence_state.set(property, value) if property
|
|
285
284
|
end
|
|
286
285
|
|
|
287
286
|
alias_method :[]=, :attribute_set
|
|
@@ -330,13 +329,13 @@ module DataMapper
|
|
|
330
329
|
attributes.each do |name, value|
|
|
331
330
|
case name
|
|
332
331
|
when String, Symbol
|
|
333
|
-
if model.
|
|
332
|
+
if model.allowed_writer_methods.include?(setter = "#{name}=")
|
|
334
333
|
__send__(setter, value)
|
|
335
334
|
else
|
|
336
335
|
raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}"
|
|
337
336
|
end
|
|
338
337
|
when Associations::Relationship, Property
|
|
339
|
-
self.
|
|
338
|
+
self.persistence_state = persistence_state.set(name, value)
|
|
340
339
|
end
|
|
341
340
|
end
|
|
342
341
|
end
|
|
@@ -360,7 +359,7 @@ module DataMapper
|
|
|
360
359
|
clear_subjects
|
|
361
360
|
end
|
|
362
361
|
|
|
363
|
-
self.
|
|
362
|
+
self.persistence_state = persistence_state.rollback
|
|
364
363
|
|
|
365
364
|
self
|
|
366
365
|
end
|
|
@@ -552,8 +551,8 @@ module DataMapper
|
|
|
552
551
|
#
|
|
553
552
|
# @api semipublic
|
|
554
553
|
def original_attributes
|
|
555
|
-
if
|
|
556
|
-
|
|
554
|
+
if persistence_state.respond_to?(:original_attributes)
|
|
555
|
+
persistence_state.original_attributes.dup.freeze
|
|
557
556
|
else
|
|
558
557
|
{}.freeze
|
|
559
558
|
end
|
|
@@ -755,7 +754,7 @@ module DataMapper
|
|
|
755
754
|
instance_variable_set(ivar, DataMapper::Ext.try_dup(instance_variable_get(ivar)))
|
|
756
755
|
end
|
|
757
756
|
|
|
758
|
-
self.
|
|
757
|
+
self.persistence_state = persistence_state.class.new(self)
|
|
759
758
|
end
|
|
760
759
|
|
|
761
760
|
# Returns name of the repository this object
|
|
@@ -954,7 +953,7 @@ module DataMapper
|
|
|
954
953
|
#
|
|
955
954
|
# @api private
|
|
956
955
|
def _persist
|
|
957
|
-
self.
|
|
956
|
+
self.persistence_state = persistence_state.commit
|
|
958
957
|
end
|
|
959
958
|
|
|
960
959
|
# This method executes the hooks before and after resource creation
|
|
@@ -997,7 +996,7 @@ module DataMapper
|
|
|
997
996
|
#
|
|
998
997
|
# @api private
|
|
999
998
|
def _destroy(execute_hooks = true)
|
|
1000
|
-
self.
|
|
999
|
+
self.persistence_state = persistence_state.delete
|
|
1001
1000
|
_persist
|
|
1002
1001
|
end
|
|
1003
1002
|
|
|
@@ -1127,8 +1126,8 @@ module DataMapper
|
|
|
1127
1126
|
|
|
1128
1127
|
# @api private
|
|
1129
1128
|
def set_default_value(subject)
|
|
1130
|
-
return unless
|
|
1131
|
-
|
|
1129
|
+
return unless persistence_state.respond_to?(:set_default_value, true)
|
|
1130
|
+
persistence_state.__send__(:set_default_value, subject)
|
|
1132
1131
|
end
|
|
1133
1132
|
|
|
1134
1133
|
# Execute all the queued up hooks for a given type and name
|
|
@@ -24,13 +24,10 @@ module DataMapper
|
|
|
24
24
|
@adapter = DataMapper::Spec.adapter(kind)
|
|
25
25
|
@repository = DataMapper.repository(@adapter.name)
|
|
26
26
|
|
|
27
|
+
@repository.scope { DataMapper.finalize }
|
|
28
|
+
|
|
27
29
|
# create all tables and constraints before each spec
|
|
28
30
|
if @repository.respond_to?(:auto_migrate!)
|
|
29
|
-
# If we are going to auto-migrate, we must also finalize
|
|
30
|
-
@repository.scope do
|
|
31
|
-
DataMapper.finalize
|
|
32
|
-
end
|
|
33
|
-
|
|
34
31
|
@repository.auto_migrate!
|
|
35
32
|
end
|
|
36
33
|
end
|
data/lib/dm-core/spec/setup.rb
CHANGED
|
@@ -42,11 +42,12 @@ module DataMapper
|
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def require_spec_adapter
|
|
45
|
-
|
|
45
|
+
desired_adapter = ENV['ADAPTER']
|
|
46
|
+
if desired_adapter.nil? || desired_adapter == 'in_memory'
|
|
46
47
|
ENV['ADAPTER_SUPPORTS'] = 'all'
|
|
47
48
|
Adapters.use(Adapters::InMemoryAdapter)
|
|
48
49
|
else
|
|
49
|
-
require "dm-#{
|
|
50
|
+
require "dm-#{desired_adapter}-adapter/spec/setup"
|
|
50
51
|
end
|
|
51
52
|
end
|
|
52
53
|
|
|
@@ -22,6 +22,14 @@ share_examples_for 'A public Property' do
|
|
|
22
22
|
@type.accept_options :foo, :bar
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
before :all do
|
|
26
|
+
@original = @type.accepted_options.dup
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
after :all do
|
|
30
|
+
@type.accepted_options.replace(@original - [ :foo, :bar ])
|
|
31
|
+
end
|
|
32
|
+
|
|
25
33
|
describe "predefined options" do
|
|
26
34
|
before :all do
|
|
27
35
|
class ::ChildSubType < @subtype
|
|
@@ -101,6 +101,13 @@ share_examples_for 'A public Resource' do
|
|
|
101
101
|
end
|
|
102
102
|
|
|
103
103
|
with_alternate_adapter do
|
|
104
|
+
before :all do
|
|
105
|
+
if @user_model.respond_to?(:auto_migrate!)
|
|
106
|
+
# force the user model to be available in the alternate repository
|
|
107
|
+
@user_model.auto_migrate!(@adapter.name)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
104
111
|
describe 'when comparing to a resource with a different repository, but the same properties' do
|
|
105
112
|
before :all do
|
|
106
113
|
rescue_if @skip do
|
|
@@ -489,6 +496,13 @@ share_examples_for 'A public Resource' do
|
|
|
489
496
|
end
|
|
490
497
|
|
|
491
498
|
with_alternate_adapter do
|
|
499
|
+
before :all do
|
|
500
|
+
if @user_model.respond_to?(:auto_migrate!)
|
|
501
|
+
# force the user model to be available in the alternate repository
|
|
502
|
+
@user_model.auto_migrate!(@adapter.name)
|
|
503
|
+
end
|
|
504
|
+
end
|
|
505
|
+
|
|
492
506
|
describe 'when comparing to a resource with a different repository, but the same properties' do
|
|
493
507
|
before :all do
|
|
494
508
|
rescue_if @skip do
|