Skip to content

Instantly share code, notes, and snippets.

@dbussink
Created September 15, 2009 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbussink/187562 to your computer and use it in GitHub Desktop.
Save dbussink/187562 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment