Class: Rulix::Validators::LengthValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rulix/validators/length_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 options = nil
  options ||= {}
  min = options[:min] || options[:exactly] || 0
  max = options[:max] || options[:exactly] || min + 1

  self.min = min
  self.max = max
end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



4
5
6
# File 'lib/rulix/validators/length_validator.rb', line 4

def max
  @max
end

#minObject

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, error_message(string)]
end

#error_message(string) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rulix/validators/length_validator.rb', line 19

def error_message string
  if string.length < min
    "is too short"
  elsif string.length > max
    "is too long"
  end
end

#to_procObject



27
28
29
# File 'lib/rulix/validators/length_validator.rb', line 27

def to_proc
  method(:call)
end