dbussink (owner)

Revisions

gist: 187562 Download_button fork
public
Public Clone URL: git://gist.github.com/187562.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
diff --git a/lib/dm-core/property.rb b/lib/dm-core/property.rb
index dbb6de9..5d54323 100644
--- a/lib/dm-core/property.rb
+++ b/lib/dm-core/property.rb
@@ -766,6 +766,23 @@ module DataMapper
       "#<#{self.class.name} @model=#{model.inspect} @name=#{name.inspect}>"
     end
 
+ # Test a value to see if it matches the primitive type
+ #
+ # @param [Object] value
+ # value to test
+ #
+ # @return [Boolean]
+ # true if the value is the correct type
+ #
+ # @api semipublic
+ def primitive?(value)
+ if primitive == TrueClass
+ value == true || value == false
+ else
+ value.kind_of?(primitive)
+ end
+ end
+
     private
 
     # TODO: document
@@ -929,22 +946,6 @@ module DataMapper
       @writer_visibility = @options[:writer] || @options[:accessor] || :public
     end
 
- # Test a value to see if it matches the primitive type
- #
- # @param [Object] value
- # value to test
- #
- # @return [Boolean]
- # true if the value is the correct type
- #
- # @api private
- def primitive?(value)
- if primitive == TrueClass
- value == true || value == false
- else
- value.kind_of?(primitive)
- end
- end
 
     # Typecast a value to an Integer
     #
diff --git a/lib/dm-core/query/conditions/comparison.rb b/lib/dm-core/query/conditions/comparison.rb
index fb19556..797148b 100644
--- a/lib/dm-core/query/conditions/comparison.rb
+++ b/lib/dm-core/query/conditions/comparison.rb
@@ -580,8 +580,10 @@ module DataMapper
         # @api semipublic
         def valid?
           case value
- when Array, Range, Set
+ when Array, Set
               loaded_value.any? && loaded_value.all? { |val| subject.valid?(val) }
+ when Range
+ loaded_value.any? && subject.valid?(loaded_value.first)
             else
               false
           end
@@ -596,7 +598,11 @@ module DataMapper
         #
         # @api private
         def expected_value
- loaded_value.map { |val| super(val) }
+ if loaded_value.is_a?(Range)
+ Range.new(super(loaded_value.first), super(loaded_value.last), loaded_value.exclude_end?)
+ else
+ loaded_value.map { |val| super(val) }
+ end
         end
 
         # Typecasts each value in the inclusion set
@@ -607,7 +613,15 @@ module DataMapper
         #
         # @api private
         def typecast_value(val)
- if subject.respond_to?(:typecast) && val.respond_to?(:map)
+ if subject.respond_to?(:typecast) && val.is_a?(Range)
+ if subject.primitive?(val.first)
+ # If the range type matches, nothing to do
+ val
+ else
+ # Create a new range with the new type
+ Range.new(subject.typecast(val.first), subject.typecast(val.last), val.exclude_end?)
+ end
+ elsif subject.respond_to?(:typecast) && val.respond_to?(:map)
             val.map { |el| subject.typecast(el) }
           else
             val
@@ -622,7 +636,9 @@ module DataMapper
         #
         # @api private
         def dumped_value(val)
- if subject.respond_to?(:value) && val.respond_to?(:map)
+ if subject.respond_to?(:value) && val.is_a?(Range) && !subject.custom?
+ val
+ elsif subject.respond_to?(:value) && val.respond_to?(:map)
             val.map { |el| subject.value(el) }
           else
             val