Class: Validation::Rule::Length

Inherits:
Object
  • Object
show all
Defined in:
lib/validation/rule/length.rb

Overview

Length rule

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Length

params can be any of the following:

- :minimum - at least this many chars
- :maximum - at most this many chars
- :exact - exactly this many chars

Example:

{:minimum => 3, :maximum => 10}
{:exact => 10}


15
16
17
# File 'lib/validation/rule/length.rb', line 15

def initialize(params)
  @params = params
end

Instance Method Details

#error_keyObject



37
38
39
# File 'lib/validation/rule/length.rb', line 37

def error_key
  :length
end

#paramsObject

returns the params given in the constructor



20
21
22
# File 'lib/validation/rule/length.rb', line 20

def params
  @params
end

#valid_value?(value) ⇒ Boolean

determines if value is valid according to the constructor params

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/validation/rule/length.rb', line 25

def valid_value?(value)
  valid = true

  @params.each_pair do |key, param|
    valid = false if key == :minimum && value.length < param
    valid = false if key == :maximum && value.length > param
    valid = false if key == :exact && value.length != param
  end

  valid
end