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
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
# TODO: update Model#respond_to? to return true if method_method missing
|
|
2
2
|
# would handle the message
|
|
3
3
|
|
|
4
|
-
require 'dm-core/relationship_set'
|
|
5
|
-
|
|
6
4
|
module DataMapper
|
|
7
5
|
module Model
|
|
8
6
|
module Relationship
|
|
9
7
|
Model.append_extensions self
|
|
10
8
|
|
|
11
9
|
include DataMapper::Assertions
|
|
12
|
-
extend Chainable
|
|
13
10
|
|
|
14
11
|
# Initializes relationships hash for extended model
|
|
15
12
|
# class.
|
|
@@ -23,21 +20,19 @@ module DataMapper
|
|
|
23
20
|
model.instance_variable_set(:@relationships, {})
|
|
24
21
|
end
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
model.instance_variable_set(:@relationships, {})
|
|
33
|
-
|
|
34
|
-
@relationships.each do |repository_name, relationships|
|
|
35
|
-
model_relationships = model.relationships(repository_name)
|
|
36
|
-
relationships.each { |relationship| model_relationships << relationship }
|
|
37
|
-
end
|
|
23
|
+
# When DataMapper model is inherited, relationships
|
|
24
|
+
# of parent are duplicated and copied to subclass model
|
|
25
|
+
#
|
|
26
|
+
# @api private
|
|
27
|
+
def inherited(model)
|
|
28
|
+
model.instance_variable_set(:@relationships, {})
|
|
38
29
|
|
|
39
|
-
|
|
30
|
+
@relationships.each do |repository_name, relationships|
|
|
31
|
+
model_relationships = model.relationships(repository_name)
|
|
32
|
+
relationships.each { |relationship| model_relationships << relationship }
|
|
40
33
|
end
|
|
34
|
+
|
|
35
|
+
super
|
|
41
36
|
end
|
|
42
37
|
|
|
43
38
|
# Returns copy of relationships set in given repository.
|
|
@@ -194,7 +189,7 @@ module DataMapper
|
|
|
194
189
|
relationship
|
|
195
190
|
end
|
|
196
191
|
|
|
197
|
-
|
|
192
|
+
private
|
|
198
193
|
|
|
199
194
|
# Extract the model from an Array of arguments
|
|
200
195
|
#
|
|
@@ -342,7 +337,7 @@ module DataMapper
|
|
|
342
337
|
# of 1:1, the underlying collection is hidden in a
|
|
343
338
|
# private ivar, and the resource is in a known ivar
|
|
344
339
|
|
|
345
|
-
|
|
340
|
+
persistence_state.get(relationships[#{name.inspect}], query)
|
|
346
341
|
end
|
|
347
342
|
RUBY
|
|
348
343
|
end
|
|
@@ -362,22 +357,21 @@ module DataMapper
|
|
|
362
357
|
#{writer_visibility}
|
|
363
358
|
def #{writer_name}(target)
|
|
364
359
|
relationship = relationships[#{name.inspect}]
|
|
365
|
-
self.
|
|
366
|
-
|
|
360
|
+
self.persistence_state = persistence_state.set(relationship, target)
|
|
361
|
+
persistence_state.get(relationship)
|
|
367
362
|
end
|
|
368
363
|
RUBY
|
|
369
364
|
end
|
|
370
365
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
return Query::Path.new([ relationship ])
|
|
376
|
-
end
|
|
377
|
-
|
|
378
|
-
super
|
|
366
|
+
# @api public
|
|
367
|
+
def method_missing(method, *args, &block)
|
|
368
|
+
if relationship = relationships(repository_name)[method]
|
|
369
|
+
return Query::Path.new([ relationship ])
|
|
379
370
|
end
|
|
371
|
+
|
|
372
|
+
super
|
|
380
373
|
end
|
|
374
|
+
|
|
381
375
|
end # module Relationship
|
|
382
376
|
end # module Model
|
|
383
377
|
end # module DataMapper
|
data/lib/dm-core/model.rb
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
# TODO: add Model#create!, Model#update, Model#update!, Model#destroy and Model#destroy!
|
|
2
|
-
|
|
3
|
-
# TODO: DRY up raise_on_save_failure with attr_accessor_with_default
|
|
4
|
-
# once AS branch is merged in
|
|
5
|
-
|
|
6
1
|
module DataMapper
|
|
7
2
|
module Model
|
|
8
|
-
extend Chainable
|
|
9
|
-
|
|
10
3
|
include Enumerable
|
|
11
4
|
|
|
5
|
+
WRITER_METHOD_REGEXP = /=\z/.freeze
|
|
6
|
+
INVALID_WRITER_METHODS = %w[ == != === []= taguri= attributes= collection= persistence_state= raise_on_save_failure= ].to_set.freeze
|
|
7
|
+
|
|
12
8
|
# Creates a new Model class with its constant already set
|
|
13
9
|
#
|
|
14
10
|
# If a block is passed, it will be eval'd in the context of the new Model
|
|
@@ -132,6 +128,19 @@ module DataMapper
|
|
|
132
128
|
@raise_on_save_failure = raise_on_save_failure
|
|
133
129
|
end
|
|
134
130
|
|
|
131
|
+
# Finish model setup and verify it is valid
|
|
132
|
+
#
|
|
133
|
+
# @return [undefined]
|
|
134
|
+
#
|
|
135
|
+
# @api public
|
|
136
|
+
def finalize
|
|
137
|
+
finalize_relationships
|
|
138
|
+
finalize_allowed_writer_methods
|
|
139
|
+
assert_valid_name
|
|
140
|
+
assert_valid_properties
|
|
141
|
+
assert_valid_key
|
|
142
|
+
end
|
|
143
|
+
|
|
135
144
|
# Appends a module for inclusion into the model class after Resource.
|
|
136
145
|
#
|
|
137
146
|
# This is a useful way to extend Resource while still retaining a
|
|
@@ -211,15 +220,13 @@ module DataMapper
|
|
|
211
220
|
end
|
|
212
221
|
|
|
213
222
|
# @api private
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
descendant.instance_variable_set(:@default_order, @default_order.dup)
|
|
222
|
-
end
|
|
223
|
+
def inherited(descendant)
|
|
224
|
+
descendants << descendant
|
|
225
|
+
|
|
226
|
+
descendant.instance_variable_set(:@valid, false)
|
|
227
|
+
descendant.instance_variable_set(:@base_model, base_model)
|
|
228
|
+
descendant.instance_variable_set(:@storage_names, @storage_names.dup)
|
|
229
|
+
descendant.instance_variable_set(:@default_order, @default_order.dup)
|
|
223
230
|
end
|
|
224
231
|
|
|
225
232
|
# Gets the name of the storage receptacle for this resource in the given
|
|
@@ -564,8 +571,7 @@ module DataMapper
|
|
|
564
571
|
discriminator = properties(repository_name).discriminator
|
|
565
572
|
no_reload = !query.reload?
|
|
566
573
|
|
|
567
|
-
field_map = fields.map { |property| [ property, property.field ] }
|
|
568
|
-
field_map = DataMapper::Ext::Array.to_hash(field_map)
|
|
574
|
+
field_map = Hash[ fields.map { |property| [ property, property.field ] } ]
|
|
569
575
|
|
|
570
576
|
records.map do |record|
|
|
571
577
|
identity_map = nil
|
|
@@ -621,13 +627,13 @@ module DataMapper
|
|
|
621
627
|
resource.instance_variable_set(:@_repository, repository)
|
|
622
628
|
|
|
623
629
|
if identity_map
|
|
624
|
-
resource.
|
|
630
|
+
resource.persistence_state = Resource::PersistenceState::Clean.new(resource) unless resource.persistence_state?
|
|
625
631
|
|
|
626
632
|
# defer setting the IdentityMap so second level caches can
|
|
627
633
|
# record the state of the resource after loaded
|
|
628
634
|
identity_map[key_values] = resource
|
|
629
635
|
else
|
|
630
|
-
resource.
|
|
636
|
+
resource.persistence_state = Resource::PersistenceState::Immutable.new(resource)
|
|
631
637
|
end
|
|
632
638
|
|
|
633
639
|
resource
|
|
@@ -637,6 +643,13 @@ module DataMapper
|
|
|
637
643
|
# @api semipublic
|
|
638
644
|
attr_reader :base_model
|
|
639
645
|
|
|
646
|
+
# The list of writer methods that can be mass-assigned to in #attributes=
|
|
647
|
+
#
|
|
648
|
+
# @return [Set]
|
|
649
|
+
#
|
|
650
|
+
# @api private
|
|
651
|
+
attr_reader :allowed_writer_methods
|
|
652
|
+
|
|
640
653
|
# @api semipublic
|
|
641
654
|
def default_repository_name
|
|
642
655
|
Repository.default_name
|
|
@@ -760,32 +773,32 @@ module DataMapper
|
|
|
760
773
|
end
|
|
761
774
|
end
|
|
762
775
|
|
|
776
|
+
# Initialize all foreign key properties established by relationships
|
|
777
|
+
#
|
|
778
|
+
# @return [undefined]
|
|
779
|
+
#
|
|
780
|
+
# @api private
|
|
781
|
+
def finalize_relationships
|
|
782
|
+
relationships(repository_name).each { |relationship| relationship.finalize }
|
|
783
|
+
end
|
|
784
|
+
|
|
785
|
+
# Initialize the list of allowed writer methods
|
|
786
|
+
#
|
|
787
|
+
# @return [undefined]
|
|
788
|
+
#
|
|
789
|
+
# @api private
|
|
790
|
+
def finalize_allowed_writer_methods
|
|
791
|
+
@allowed_writer_methods = public_instance_methods.map { |method| method.to_s }.grep(WRITER_METHOD_REGEXP).to_set
|
|
792
|
+
@allowed_writer_methods -= INVALID_WRITER_METHODS
|
|
793
|
+
@allowed_writer_methods.freeze
|
|
794
|
+
end
|
|
795
|
+
|
|
763
796
|
# @api private
|
|
764
797
|
# TODO: Remove this once appropriate warnings can be added.
|
|
765
798
|
def assert_valid(force = false) # :nodoc:
|
|
766
799
|
return if @valid && !force
|
|
767
800
|
@valid = true
|
|
768
|
-
|
|
769
|
-
name = self.name
|
|
770
|
-
repository_name = self.repository_name
|
|
771
|
-
|
|
772
|
-
if properties(repository_name).empty? &&
|
|
773
|
-
!relationships(repository_name).any? { |(relationship_name, relationship)| relationship.kind_of?(Associations::ManyToOne::Relationship) }
|
|
774
|
-
raise IncompleteModelError, "#{name} must have at least one property or many to one relationship to be valid"
|
|
775
|
-
end
|
|
776
|
-
|
|
777
|
-
if key(repository_name).empty?
|
|
778
|
-
raise IncompleteModelError, "#{name} must have a key to be valid"
|
|
779
|
-
end
|
|
780
|
-
|
|
781
|
-
# initialize join models and target keys
|
|
782
|
-
@relationships.values.each do |relationships|
|
|
783
|
-
relationships.each do |relationship|
|
|
784
|
-
relationship.child_key
|
|
785
|
-
relationship.through if relationship.respond_to?(:through)
|
|
786
|
-
relationship.via if relationship.respond_to?(:via)
|
|
787
|
-
end
|
|
788
|
-
end
|
|
801
|
+
finalize
|
|
789
802
|
end
|
|
790
803
|
|
|
791
804
|
# Raises an exception if #get receives the wrong number of arguments
|
|
@@ -807,5 +820,50 @@ module DataMapper
|
|
|
807
820
|
raise ArgumentError, "The number of arguments for the key is invalid, expected #{expected_key_size} but was #{actual_key_size}"
|
|
808
821
|
end
|
|
809
822
|
end
|
|
823
|
+
|
|
824
|
+
# Test if the model name is valid
|
|
825
|
+
#
|
|
826
|
+
# @return [undefined]
|
|
827
|
+
#
|
|
828
|
+
# @api private
|
|
829
|
+
def assert_valid_name
|
|
830
|
+
if name.to_s.strip.empty?
|
|
831
|
+
raise IncompleteModelError, "#{inspect} must have a name"
|
|
832
|
+
end
|
|
833
|
+
end
|
|
834
|
+
|
|
835
|
+
# Test if the model has properties
|
|
836
|
+
#
|
|
837
|
+
# A model may also be valid if it has at least one m:1 relationships which
|
|
838
|
+
# will add inferred foreign key properties.
|
|
839
|
+
#
|
|
840
|
+
# @return [undefined]
|
|
841
|
+
#
|
|
842
|
+
# @raise [IncompleteModelError]
|
|
843
|
+
# raised if the model has no properties
|
|
844
|
+
#
|
|
845
|
+
# @api private
|
|
846
|
+
def assert_valid_properties
|
|
847
|
+
repository_name = self.repository_name
|
|
848
|
+
if properties(repository_name).empty? &&
|
|
849
|
+
!relationships(repository_name).any? { |relationship| relationship.kind_of?(Associations::ManyToOne::Relationship) }
|
|
850
|
+
raise IncompleteModelError, "#{name} must have at least one property or many to one relationship to be valid"
|
|
851
|
+
end
|
|
852
|
+
end
|
|
853
|
+
|
|
854
|
+
# Test if the model has a valid key
|
|
855
|
+
#
|
|
856
|
+
# @return [undefined]
|
|
857
|
+
#
|
|
858
|
+
# @raise [IncompleteModelError]
|
|
859
|
+
# raised if the model does not have a valid key
|
|
860
|
+
#
|
|
861
|
+
# @api private
|
|
862
|
+
def assert_valid_key
|
|
863
|
+
if key(repository_name).empty?
|
|
864
|
+
raise IncompleteModelError, "#{name} must have a key to be valid"
|
|
865
|
+
end
|
|
866
|
+
end
|
|
867
|
+
|
|
810
868
|
end # module Model
|
|
811
869
|
end # module DataMapper
|
|
@@ -2,6 +2,21 @@ module DataMapper
|
|
|
2
2
|
class Property
|
|
3
3
|
class Binary < String
|
|
4
4
|
include PassThroughLoadDump
|
|
5
|
+
|
|
6
|
+
if RUBY_VERSION >= "1.9"
|
|
7
|
+
|
|
8
|
+
def load(value)
|
|
9
|
+
super.dup.force_encoding("BINARY") unless value.nil?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def dump(value)
|
|
13
|
+
value.dup.force_encoding("BINARY") unless value.nil?
|
|
14
|
+
rescue
|
|
15
|
+
value
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
5
20
|
end # class Binary
|
|
6
21
|
end # class Property
|
|
7
22
|
end # module DataMapper
|
|
@@ -5,6 +5,11 @@ module DataMapper
|
|
|
5
5
|
|
|
6
6
|
primitive ::TrueClass
|
|
7
7
|
|
|
8
|
+
TRUE_VALUES = [ 1, '1', 't', 'T', 'true', 'TRUE' ].freeze
|
|
9
|
+
FALSE_VALUES = [ 0, '0', 'f', 'F', 'false', 'FALSE' ].freeze
|
|
10
|
+
BOOLEAN_MAP = Hash[
|
|
11
|
+
TRUE_VALUES.product([ true ]) + FALSE_VALUES.product([ false ]) ].freeze
|
|
12
|
+
|
|
8
13
|
def primitive?(value)
|
|
9
14
|
value == true || value == false
|
|
10
15
|
end
|
|
@@ -19,16 +24,7 @@ module DataMapper
|
|
|
19
24
|
#
|
|
20
25
|
# @api private
|
|
21
26
|
def typecast_to_primitive(value)
|
|
22
|
-
|
|
23
|
-
return true if value == 1
|
|
24
|
-
return false if value == 0
|
|
25
|
-
elsif value.respond_to?(:to_str)
|
|
26
|
-
string_value = value.to_str.downcase
|
|
27
|
-
return true if %w[ true 1 t ].include?(string_value)
|
|
28
|
-
return false if %w[ false 0 f ].include?(string_value)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
value
|
|
27
|
+
BOOLEAN_MAP.fetch(value, value)
|
|
32
28
|
end
|
|
33
29
|
end # class Boolean
|
|
34
30
|
end # class Property
|
|
@@ -3,7 +3,11 @@ module DataMapper
|
|
|
3
3
|
class Decimal < Numeric
|
|
4
4
|
primitive BigDecimal
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
DEFAULT_PRECISION = 10
|
|
7
|
+
DEFAULT_SCALE = 0
|
|
8
|
+
|
|
9
|
+
precision(DEFAULT_PRECISION)
|
|
10
|
+
scale(DEFAULT_SCALE)
|
|
7
11
|
|
|
8
12
|
protected
|
|
9
13
|
|
|
@@ -11,8 +15,8 @@ module DataMapper
|
|
|
11
15
|
super
|
|
12
16
|
|
|
13
17
|
[ :scale, :precision ].each do |key|
|
|
14
|
-
unless options.key?(key)
|
|
15
|
-
warn "options[#{key.inspect}] should be set for #{self.class}, defaulting to #{send(key).inspect}"
|
|
18
|
+
unless @options.key?(key)
|
|
19
|
+
warn "options[#{key.inspect}] should be set for #{self.class}, defaulting to #{send(key).inspect} (#{caller.first})"
|
|
16
20
|
end
|
|
17
21
|
end
|
|
18
22
|
|
|
@@ -8,40 +8,38 @@ module DataMapper
|
|
|
8
8
|
|
|
9
9
|
# @api private
|
|
10
10
|
def bind
|
|
11
|
-
model.
|
|
12
|
-
|
|
13
|
-
model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
14
|
-
extend Chainable
|
|
11
|
+
model.extend Model unless model < Model
|
|
12
|
+
end
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
module Model
|
|
15
|
+
def inherited(model)
|
|
16
|
+
super # setup self.descendants
|
|
17
|
+
set_discriminator_scope_for(model)
|
|
18
|
+
end
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
def new(*args, &block)
|
|
21
|
+
if args.size == 1 && args.first.kind_of?(Hash)
|
|
22
|
+
discriminator = properties(repository_name).discriminator
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
if discriminator_value = args.first[discriminator.name]
|
|
25
|
+
model = discriminator.typecast_to_primitive(discriminator_value)
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
32
|
-
end
|
|
27
|
+
if model.kind_of?(Model) && !model.equal?(self)
|
|
28
|
+
return model.new(*args, &block)
|
|
33
29
|
end
|
|
34
|
-
|
|
35
|
-
super
|
|
36
30
|
end
|
|
31
|
+
end
|
|
37
32
|
|
|
38
|
-
|
|
33
|
+
super
|
|
34
|
+
end
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def set_discriminator_scope_for(model)
|
|
39
|
+
discriminator = self.properties.discriminator
|
|
40
|
+
default_scope = model.default_scope(discriminator.repository_name)
|
|
41
|
+
default_scope.update(discriminator.name => model.descendants.dup << model)
|
|
42
|
+
end
|
|
45
43
|
end
|
|
46
44
|
end # class Discriminator
|
|
47
45
|
end # module Property
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'dm-core/property/typecast/numeric'
|
|
2
|
-
|
|
3
1
|
module DataMapper
|
|
4
2
|
class Property
|
|
5
3
|
class Numeric < Object
|
|
@@ -9,9 +7,8 @@ module DataMapper
|
|
|
9
7
|
accept_options :precision, :scale, :min, :max
|
|
10
8
|
attr_reader :precision, :scale, :min, :max
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
DEFAULT_NUMERIC_MAX = 2**31-1
|
|
10
|
+
DEFAULT_NUMERIC_MIN = 0
|
|
11
|
+
DEFAULT_NUMERIC_MAX = 2**31-1
|
|
15
12
|
|
|
16
13
|
protected
|
|
17
14
|
|
|
@@ -19,8 +16,8 @@ module DataMapper
|
|
|
19
16
|
super
|
|
20
17
|
|
|
21
18
|
if @primitive == BigDecimal || @primitive == ::Float
|
|
22
|
-
@precision = @options.fetch(:precision
|
|
23
|
-
@scale = @options.fetch(:scale
|
|
19
|
+
@precision = @options.fetch(:precision)
|
|
20
|
+
@scale = @options.fetch(:scale)
|
|
24
21
|
|
|
25
22
|
unless @precision > 0
|
|
26
23
|
raise ArgumentError, "precision must be greater than 0, but was #{@precision.inspect}"
|
|
@@ -28,8 +25,8 @@ module DataMapper
|
|
|
28
25
|
end
|
|
29
26
|
|
|
30
27
|
if @options.key?(:min) || @options.key?(:max)
|
|
31
|
-
@min = @options.fetch(:min, DEFAULT_NUMERIC_MIN)
|
|
32
|
-
@max = @options.fetch(:max, DEFAULT_NUMERIC_MAX)
|
|
28
|
+
@min = @options.fetch(:min, self.class::DEFAULT_NUMERIC_MIN)
|
|
29
|
+
@max = @options.fetch(:max, self.class::DEFAULT_NUMERIC_MAX)
|
|
33
30
|
|
|
34
31
|
if @max < DEFAULT_NUMERIC_MIN && !@options.key?(:min)
|
|
35
32
|
raise ArgumentError, "min should be specified when the max is less than #{DEFAULT_NUMERIC_MIN}"
|
|
@@ -8,6 +8,7 @@ module DataMapper
|
|
|
8
8
|
accept_options :length
|
|
9
9
|
|
|
10
10
|
DEFAULT_LENGTH = 50
|
|
11
|
+
length(DEFAULT_LENGTH)
|
|
11
12
|
|
|
12
13
|
# Returns maximum property length (if applicable).
|
|
13
14
|
# This usually only makes sense when property is of
|
|
@@ -29,7 +30,7 @@ module DataMapper
|
|
|
29
30
|
|
|
30
31
|
def initialize(model, name, options = {})
|
|
31
32
|
super
|
|
32
|
-
@length = @options.fetch(:length
|
|
33
|
+
@length = @options.fetch(:length)
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
# Typecast a value to a String
|
|
@@ -15,14 +15,19 @@ module DataMapper
|
|
|
15
15
|
#
|
|
16
16
|
# @api private
|
|
17
17
|
def extract_time(value)
|
|
18
|
-
mash = value.respond_to?(:to_mash)
|
|
18
|
+
mash = if value.respond_to?(:to_mash)
|
|
19
|
+
value.to_mash
|
|
20
|
+
else
|
|
21
|
+
DataMapper::Ext::Hash.to_mash(value)
|
|
22
|
+
end
|
|
23
|
+
|
|
19
24
|
now = ::Time.now
|
|
20
25
|
|
|
21
26
|
[ :year, :month, :day, :hour, :min, :sec ].map do |segment|
|
|
22
27
|
typecast_to_numeric(mash.fetch(segment, now.send(segment)), :to_i)
|
|
23
28
|
end
|
|
24
29
|
end
|
|
25
|
-
end #
|
|
30
|
+
end # Time
|
|
26
31
|
end # Typecast
|
|
27
32
|
end # Property
|
|
28
33
|
end # DataMapper
|