Permalink
Browse files

To infinity… and beyond!

Allow infinite values for validates_length_of. Particularly useful
for prettily defining an open ended range such as

validates_length_of :human_stupidity, :within => 0..Float::INFINITY
  • Loading branch information...
niels committed Feb 6, 2012
1 parent af7daff commit 60dad828aefa41703c0eee3863a7141c56caa7da
@@ -29,8 +29,8 @@ def check_validity!
keys.each do |key|
value = options[key]
- unless value.is_a?(Integer) && value >= 0
- raise ArgumentError, ":#{key} must be a nonnegative Integer"
+ unless value.is_a?(Integer) && value >= 0 or value == Float::INFINITY

This comment has been minimized.

Show comment Hide comment
@bquorning

bquorning Feb 22, 2012

Contributor

unless … && … or … – really? I think this is more readable:

unless (value.is_a?(Integer) && value >= 0) || value == Float::INFINITY
@bquorning

bquorning Feb 22, 2012

Contributor

unless … && … or … – really? I think this is more readable:

unless (value.is_a?(Integer) && value >= 0) || value == Float::INFINITY

This comment has been minimized.

Show comment Hide comment
@josevalim

josevalim Feb 22, 2012

Contributor

agreed. pull req pls?

@josevalim

josevalim Feb 22, 2012

Contributor

agreed. pull req pls?

This comment has been minimized.

Show comment Hide comment
@exviva

exviva Feb 22, 2012

Contributor

How about an :at_least and :at_most option? (or :minimum/:maximum)

@exviva

exviva Feb 22, 2012

Contributor

How about an :at_least and :at_most option? (or :minimum/:maximum)

This comment has been minimized.

Show comment Hide comment
@josevalim

josevalim Feb 22, 2012

Contributor

Sorry? We already have minimum and maximum.

@josevalim

josevalim Feb 22, 2012

Contributor

Sorry? We already have minimum and maximum.

This comment has been minimized.

Show comment Hide comment
@bquorning

bquorning Feb 22, 2012

Contributor

@josevalim Pull request is here: #5124

@bquorning

bquorning Feb 22, 2012

Contributor

@josevalim Pull request is here: #5124

This comment has been minimized.

Show comment Hide comment
@exviva

exviva Feb 22, 2012

Contributor

@josevalim doh I was looking at the wrong RDoc ;)

In that case, why use within: 5..Float::INFINITY when one could use minimum: 5 ?

@exviva

exviva Feb 22, 2012

Contributor

@josevalim doh I was looking at the wrong RDoc ;)

In that case, why use within: 5..Float::INFINITY when one could use minimum: 5 ?

This comment has been minimized.

Show comment Hide comment
@niels

niels Feb 24, 2012

Contributor

@exviva the use case for which I implemented this was a 3rd party library (devise) that allows the configuration of a length range but not min or max individually, let alone only one of the two.

IMO this is perfectly legit and I actually find X..Float::INFINITY more readable than only supplying a minimum and no maximum. It conveys intention more clearly.

@niels

niels Feb 24, 2012

Contributor

@exviva the use case for which I implemented this was a 3rd party library (devise) that allows the configuration of a length range but not min or max individually, let alone only one of the two.

IMO this is perfectly legit and I actually find X..Float::INFINITY more readable than only supplying a minimum and no maximum. It conveys intention more clearly.

This comment has been minimized.

Show comment Hide comment
@exviva

exviva Feb 24, 2012

Contributor

@nielsomat makes sense. I'm just obsessively allergic to feature bloat :).

@exviva

exviva Feb 24, 2012

Contributor

@nielsomat makes sense. I'm just obsessively allergic to feature bloat :).

+ raise ArgumentError, ":#{key} must be a nonnegative Integer or Infinity"
end
end
end
@@ -357,4 +357,22 @@ def test_validates_length_of_for_ruby_class
ensure
Person.reset_callbacks(:validate)
end
+
+ def test_validates_length_of_for_infinite_maxima
+ Topic.validates_length_of(:title, :within => 5..Float::INFINITY)
+
+ t = Topic.new("title" => "1234")
+ assert t.invalid?
+ assert t.errors[:title].any?
+
+ t.title = "12345"
+ assert t.valid?
+
+ Topic.validates_length_of(:author_name, :maximum => Float::INFINITY)
+
+ assert t.valid?
+
+ t.author_name = "A very long author name that should still be valid." * 100
+ assert t.valid?
+ end
end

0 comments on commit 60dad82

Please sign in to comment.