aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations/clusivity.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-03-28 17:52:52 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-03-28 18:57:56 -0300
commit170956cdae043733288c2dc1440b8940af309793 (patch)
treec5a8f25ca4bc4cda8ea27cd22a2f65d9045527f6 /activemodel/lib/active_model/validations/clusivity.rb
parentc82fd8fc2ac9f8273825fc272a0bf17e5c583d71 (diff)
downloadrails-170956cdae043733288c2dc1440b8940af309793.tar.gz
rails-170956cdae043733288c2dc1440b8940af309793.tar.bz2
rails-170956cdae043733288c2dc1440b8940af309793.zip
Remove code duplication in InclusionValidator and ExclusionValidator.
Orginal code by @jamescook Closes #1352 [Rafael Mendonça França + James Cook]
Diffstat (limited to 'activemodel/lib/active_model/validations/clusivity.rb')
-rw-r--r--activemodel/lib/active_model/validations/clusivity.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb
new file mode 100644
index 0000000000..b632a2bd6b
--- /dev/null
+++ b/activemodel/lib/active_model/validations/clusivity.rb
@@ -0,0 +1,31 @@
+require 'active_support/core_ext/range.rb'
+
+module ActiveModel
+ module Validations
+ module Clusivity
+ ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " <<
+ "and must be supplied as the :in option of the configuration hash"
+
+ def check_validity!
+ unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
+ raise ArgumentError, ERROR_MESSAGE
+ end
+ end
+
+ private
+
+ def include?(record, value)
+ delimiter = options[:in]
+ exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter
+ exclusions.send(inclusion_method(exclusions), value)
+ end
+
+ # 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?
+ end
+ end
+ end
+end