Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add :use_include option to allow user to explicitly use `Range#includ…

…e?` method in Ruby 1.9

In Ruby 1.9 we're currently use `Range#cover?` to fix the performance problem. However, there might be the case that you want to use `Range#include?` instead. This patch will give you that option.
  • Loading branch information...
commit f6540211b5b9133c9f93c11655a04d613c237e67 1 parent 58594be
@sikachu sikachu authored josevalim committed
View
16 activemodel/lib/active_model/validations/exclusion.rb
@@ -16,8 +16,8 @@ def check_validity!
def validate_each(record, attribute, value)
exclusions = options[:in].respond_to?(:call) ? options[:in].call(record) : options[:in]
- if exclusions.send(inclusion_method(exclusions), value)
- record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value))
+ if exclusions.send(inclusion_method(exclusions, options[:use_include]), value)
+ record.errors.add(attribute, :exclusion, options.except(:in, :use_include).merge!(:value => value))
end
rescue NoMethodError
raise ArgumentError, "Exclusion validation for :#{attribute} in #{record.class.name}: #{ERROR_MESSAGE}"
@@ -28,8 +28,8 @@ def validate_each(record, attribute, value)
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
# range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
# uses the previous logic of comparing a value with the range endpoints.
- def inclusion_method(enumerable)
- enumerable.is_a?(Range) ? :cover? : :include?
+ def inclusion_method(enumerable, use_include = nil)
+ !use_include && enumerable.is_a?(Range) ? :cover? : :include?
end
end
@@ -45,9 +45,11 @@ module HelperMethods
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of items that the value shouldn't be part of.
- # This can be supplied as a proc or lambda which returns an enumerable. If the enumerable
- # is a range the test is performed with <tt>Range#cover?</tt>
- # (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
+ # This can be supplied as a proc or lambda which returns an enumerable.
+ # * <tt>:use_include</tt> - If set to true and the enumerable in <tt>:in</tt> option is a range,
+ # it will explicitly use <tt>Range#include?</tt> to perform the test. Otherwise <tt>Range#cover?</tt>
+ # will be used to perform the test for performance reason.
+ # (Range#cover? was backported in Active Support for 1.8.x)
# * <tt>:message</tt> - Specifies a custom error message (default is: "is reserved").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
View
16 activemodel/lib/active_model/validations/inclusion.rb
@@ -16,8 +16,8 @@ def check_validity!
def validate_each(record, attribute, value)
exclusions = options[:in].respond_to?(:call) ? options[:in].call(record) : options[:in]
- unless exclusions.send(inclusion_method(exclusions), value)
- record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value))
+ unless exclusions.send(inclusion_method(exclusions, options[:use_include]), value)
+ record.errors.add(attribute, :inclusion, options.except(:in, :use_include).merge!(:value => value))
end
rescue NoMethodError
raise ArgumentError, "Exclusion validation for :#{attribute} in #{record.class.name}: #{ERROR_MESSAGE}"
@@ -28,8 +28,8 @@ def validate_each(record, attribute, value)
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
# range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
# uses the previous logic of comparing a value with the range endpoints.
- def inclusion_method(enumerable)
- enumerable.is_a?(Range) ? :cover? : :include?
+ def inclusion_method(enumerable, use_include = nil)
+ !use_include && enumerable.is_a?(Range) ? :cover? : :include?
end
end
@@ -45,9 +45,11 @@ module HelperMethods
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of available items. This can be
- # supplied as a proc or lambda which returns an enumerable. If the enumerable
- # is a range the test is performed with <tt>Range#cover?</tt>
- # (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
+ # supplied as a proc or lambda which returns an enumerable.
+ # * <tt>:use_include</tt> - If set to true and the enumerable in <tt>:in</tt> option is a range,
+ # it will explicitly use <tt>Range#include?</tt> to perform the test. Otherwise <tt>Range#cover?</tt>
+ # will be used to perform the test for performance reason.
+ # (Range#cover? was backported in Active Support for 1.8.x)
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not included in the list").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
View
10 activemodel/test/cases/validations/exclusion_validation_test.rb
@@ -63,4 +63,14 @@ def test_validates_exclustion_with_invalid_lambda_return
p.author_name = "sikachu"
assert_raise(ArgumentError){ p.valid? }
end
+
+ def test_validates_inclusion_with_explicit_include
+ range = (1..100)
+ Topic.validates_exclusion_of :title, :in => range, :use_include => true
+ range.expects(:include?).returns(false)
+
+ t = Topic.new
+ t.title = 102
+ assert t.valid?
+ end
end
View
9 activemodel/test/cases/validations/inclusion_validation_test.rb
@@ -95,4 +95,13 @@ def test_validates_inclustion_with_invalid_lambda_return
p.author_name = "sikachu"
assert_raise(ArgumentError){ p.valid? }
end
+
+ def test_validates_inclusion_with_explicit_include
+ range = (1..100)
+ Topic.validates_inclusion_of :title, :in => range, :use_include => true
+
+ t = Topic.new
+ t.title = 42
+ assert t.valid?
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.