From a386d130dd45a02eb38fac712283d6a427fdf0c7 Mon Sep 17 00:00:00 2001 From: Diego Carrion Date: Sat, 19 Feb 2011 21:13:07 -0200 Subject: [PATCH 1/2] added Range#count? for Ruby 1.8 --- activesupport/lib/active_support/core_ext/range.rb | 1 + .../lib/active_support/core_ext/range/cover.rb | 3 +++ activesupport/test/core_ext/range_ext_test.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 0 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/range/cover.rb diff --git a/activesupport/lib/active_support/core_ext/range.rb b/activesupport/lib/active_support/core_ext/range.rb index c0736f3..2428a02 100644 --- a/activesupport/lib/active_support/core_ext/range.rb +++ b/activesupport/lib/active_support/core_ext/range.rb @@ -2,3 +2,4 @@ require 'active_support/core_ext/range/blockless_step' require 'active_support/core_ext/range/conversions' require 'active_support/core_ext/range/include_range' require 'active_support/core_ext/range/overlaps' +require 'active_support/core_ext/range/cover' diff --git a/activesupport/lib/active_support/core_ext/range/cover.rb b/activesupport/lib/active_support/core_ext/range/cover.rb new file mode 100644 index 0000000..3a182cd --- /dev/null +++ b/activesupport/lib/active_support/core_ext/range/cover.rb @@ -0,0 +1,3 @@ +class Range + alias_method(:cover?, :include?) unless instance_methods.include?(:cover?) +end diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb index 5701eee..1424fa4 100644 --- a/activesupport/test/core_ext/range_ext_test.rb +++ b/activesupport/test/core_ext/range_ext_test.rb @@ -62,4 +62,16 @@ class RangeTest < Test::Unit::TestCase (1..10).step(2) {|i| array << i } assert_equal [1,3,5,7,9], array end + + if RUBY_VERSION < '1.9' + def test_cover + assert((1..3).cover?(2)) + assert !(1..3).cover?(4) + end + else + def test_cover_is_not_override + range = (1..3) + assert range.method(:include?) != range.method(:cover?) + end + end end -- 1.6.5.2 From 6ec67bc8a8baea08734694bfc76577bb9da7fa1e Mon Sep 17 00:00:00 2001 From: Diego Carrion Date: Sat, 19 Feb 2011 21:13:33 -0200 Subject: [PATCH 2/2] refactored ActiveModel::Validations::InclusionValidator#validate_each --- .../lib/active_model/validations/inclusion.rb | 26 ++++++------------- 1 files changed, 8 insertions(+), 18 deletions(-) diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 108586b..b0903a6 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/range.rb' + module ActiveModel # == Active Model Inclusion Validator @@ -8,27 +10,15 @@ module ActiveModel ":in option of the configuration hash" unless options[:in].respond_to?(:include?) end + def validate_each(record, attribute, value) + record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) unless options[:in].send(include?, value) + end + # On Ruby 1.9 Range#include? 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. - if (1..2).respond_to?(:cover?) - def validate_each(record, attribute, value) - included = if options[:in].is_a?(Range) - options[:in].cover?(value) - else - options[:in].include?(value) - end - - unless included - record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) - end - end - else - def validate_each(record, attribute, value) - unless options[:in].include?(value) - record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) - end - end + def include? + options[:in].is_a?(Range) ? :cover? : :include? end end -- 1.6.5.2