Class: Anodator::Validator::LengthValidator
- Defined in:
- lib/anodator/validator/length_validator.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(target_expression, options = { }) ⇒ LengthValidator
constructor
A new instance of LengthValidator.
- #validate ⇒ Object
Methods inherited from Base
#allow_blank?, #argument_value_at, default_options, #description, #target_value, #to_s, #valid?, valid_option_keys, #validate_configuration, values, values=
Constructor Details
#initialize(target_expression, options = { }) ⇒ LengthValidator
9 10 11 12 13 14 15 |
# File 'lib/anodator/validator/length_validator.rb', line 9 def initialize(target_expression, = { }) super(target_expression, ) [:maximum, :minimum, :is].each do |key| [key] = proxy_value([key]) unless [key].nil? end end |
Instance Method Details
#validate ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/anodator/validator/length_validator.rb', line 17 def validate length = target_value.split(//).size if allow_blank? return true if length.zero? end .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.value.to_i when :minimum return false if length < configuration.value.to_i when :is return false if length != configuration.value.to_i end end return true end |