Class: Anodator::Validator::LengthValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/anodator/validator/length_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#options, #target

Instance Method Summary collapse

Methods inherited from Base

#allow_blank?, default_options, #description, #initialize, #target_value, #to_s, #valid?, valid_option_keys, #validate_configuration, values, values=

Constructor Details

This class inherits a constructor from Anodator::Validator::Base

Instance Method Details

#validateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/anodator/validator/length_validator.rb', line 9

def validate
  length = target_value.split(//).size

  if allow_blank?
    return true if length.zero?
  end

  @options.each do |option, configuration|
    case option
    when :in
      if configuration.is_a? Range
        return false unless configuration.include?(length)
      else
        raise ConfigurationError.new(":in option value must be Range object")
      end
    when :maximum
      return false if length > configuration.to_i
    when :minimum
      return false if length < configuration.to_i
    when :is
      return false if length != configuration.to_i
    end
  end

  return true
end