From f6540211b5b9133c9f93c11655a04d613c237e67 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sun, 10 Apr 2011 15:39:01 +0800 Subject: Add :use_include option to allow user to explicitly use `Range#include?` 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. --- activemodel/lib/active_model/validations/exclusion.rb | 16 +++++++++------- activemodel/lib/active_model/validations/inclusion.rb | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index abc1bfae78..e8d7bb162a 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -16,8 +16,8 @@ module ActiveModel 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 @@ module ActiveModel # In Ruby 1.9 Range#include? on non-numeric ranges checks all possible values in the # range for equality, so it may be slow for large ranges. The new Range#cover? # 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 ActiveModel # # Configuration options: # * :in - 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 Range#cover? - # (backported in Active Support for 1.8), otherwise with include?. + # This can be supplied as a proc or lambda which returns an enumerable. + # * :use_include - If set to true and the enumerable in :in option is a range, + # it will explicitly use Range#include? to perform the test. Otherwise Range#cover? + # will be used to perform the test for performance reason. + # (Range#cover? was backported in Active Support for 1.8.x) # * :message - Specifies a custom error message (default is: "is reserved"). # * :allow_nil - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * :allow_blank - If set to true, skips this validation if the attribute is blank (default is +false+). diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index cb46547e92..2b14f84bb5 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -16,8 +16,8 @@ module ActiveModel 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 @@ module ActiveModel # In Ruby 1.9 Range#include? on non-numeric ranges checks all possible values in the # range for equality, so it may be slow for large ranges. The new Range#cover? # 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 ActiveModel # # Configuration options: # * :in - 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 Range#cover? - # (backported in Active Support for 1.8), otherwise with include?. + # supplied as a proc or lambda which returns an enumerable. + # * :use_include - If set to true and the enumerable in :in option is a range, + # it will explicitly use Range#include? to perform the test. Otherwise Range#cover? + # will be used to perform the test for performance reason. + # (Range#cover? was backported in Active Support for 1.8.x) # * :message - Specifies a custom error message (default is: "is not included in the list"). # * :allow_nil - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * :allow_blank - If set to true, skips this validation if the attribute is blank (default is +false+). -- cgit v1.2.3