Class: Rulix::Validators::LengthValidator
- Inherits:
-
Object
- Object
- Rulix::Validators::LengthValidator
- Defined in:
- lib/rulix/validators/length_validator.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
Instance Method Summary collapse
- #call(string) ⇒ Object
- #error_message(string) ⇒ Object
-
#initialize(options = nil) ⇒ LengthValidator
constructor
A new instance of LengthValidator.
- #to_proc ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ LengthValidator
Returns a new instance of LengthValidator.
6 7 8 9 10 11 12 13 |
# File 'lib/rulix/validators/length_validator.rb', line 6 def initialize = nil ||= {} min = [:min] || [:exactly] || 0 max = [:max] || [:exactly] || min + 1 self.min = min self.max = max end |
Instance Attribute Details
#max ⇒ Object
Returns the value of attribute max.
4 5 6 |
# File 'lib/rulix/validators/length_validator.rb', line 4 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
4 5 6 |
# File 'lib/rulix/validators/length_validator.rb', line 4 def min @min end |
Instance Method Details
#call(string) ⇒ Object
15 16 17 |
# File 'lib/rulix/validators/length_validator.rb', line 15 def call string (min..max).cover?(string.length) || [false, (string)] end |
#error_message(string) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rulix/validators/length_validator.rb', line 19 def string if string.length < min "is too short" elsif string.length > max "is too long" end end |
#to_proc ⇒ Object
27 28 29 |
# File 'lib/rulix/validators/length_validator.rb', line 27 def to_proc method(:call) end |