Permalink
Browse files

Prefer cover? over include? for validates_includes/validates_inclusio…

…n_of

On ruby 1.9, non-numeric ranges (such as date ranges) are treated
differently than 1.8 in regards to include?.  In 1.8 include?
just checked against the beginning and end of the range, now it
checks each object in the range to see if it matches.  The 1.8
include? behavior is now known as cover?.  This is a simple switch
to use cover? if available, and fallback to include? if not.
  • Loading branch information...
jeremyevans committed Feb 16, 2011
1 parent 7251baa commit 7dd4146e53685f07944e9c05da5d9049efe9e7f3
Showing with 10 additions and 7 deletions.
  1. +2 −0 CHANGELOG
  2. +6 −5 lib/sequel/plugins/validation_class_methods.rb
  3. +2 −2 lib/sequel/plugins/validation_helpers.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Prefer cover? over include? for validates_includes/validates_inclusion_of (jeremyevans)
+
* Make using NULL/NOT NULL, DEFAULT, and UNIQUE column options work correctly on H2 and possibly Oracle (jeremyevans)
* Make bin/sequel accept file arguments and work correctly when $stdin is not a tty (jeremyevans)
@@ -262,7 +262,7 @@ def validates_length_of(*atts)
o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && v.size == i
end
if w = opts[:within]
- o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && w.include?(v.size)
+ o.errors.add(a, opts[:message] || opts[:wrong_length]) unless v && w.send(w.respond_to?(:cover?) ? :cover? : :include?, v.size)
end
end
end
@@ -345,14 +345,15 @@ def validates_presence_of(*atts)
# * :message - The message to use (default: 'is not in range or set: <specified range>')
def validates_inclusion_of(*atts)
opts = extract_options!(atts)
- unless opts[:in] && opts[:in].respond_to?(:include?)
- raise ArgumentError, "The :in parameter is required, and respond to include?"
+ n = opts[:in]
+ unless n && (n.respond_to?(:cover?) || n.respond_to?(:include?))
+ raise ArgumentError, "The :in parameter is required, and must respond to cover? or include?"
end
- opts[:message] ||= "is not in range or set: #{opts[:in].inspect}"
+ opts[:message] ||= "is not in range or set: #{n.inspect}"
reflect_validation(:inclusion, opts, atts)
atts << opts
validates_each(*atts) do |o, a, v|
- o.errors.add(a, opts[:message]) unless opts[:in].include?(v)
+ o.errors.add(a, opts[:message]) unless n.send(n.respond_to?(:cover?) ? :cover? : :include?, v)
end
end
@@ -72,7 +72,7 @@ def validates_format(with, atts, opts={})
# Check attribute value(s) is included in the given set.
def validates_includes(set, atts, opts={})
- validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.include?(v)}
+ validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.send(set.respond_to?(:cover?) ? :cover? : :include?, v)}
end
# Check attribute value(s) string representation is a valid integer.
@@ -89,7 +89,7 @@ def validates_integer(atts, opts={})
# Check that the attribute values length is in the specified range.
def validates_length_range(range, atts, opts={})
- validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) unless v && range.include?(v.length)}
+ validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) unless v && range.send(range.respond_to?(:cover?) ? :cover? : :include?, v.length)}
end
# Check that the attribute values are not longer than the given max length.

0 comments on commit 7dd4146

Please sign in to comment.