class SimpleValidate::ValidatesLengthOf
Constants
- VALIDATORS
Public Class Methods
new(attribute, options)
click to toggle source
Calls superclass method
SimpleValidate::ValidatesBase::new
# File lib/simple_validate/validates_length_of.rb, line 7 def initialize(attribute, options) @validator = options.keys.first raise ArgumentError, "Invalid length option" unless VALIDATORS.include?(@validator) @valid_length = options[@validator] @allow_nil = options[:allow_nil] super(attribute, options[:message], options[:if] || proc { true }) end
Public Instance Methods
message()
click to toggle source
# File lib/simple_validate/validates_length_of.rb, line 18 def message @message = case @validator when :minimum "is too short" when :maximum "is too long" else "is not valid length" end end
valid?(instance)
click to toggle source
# File lib/simple_validate/validates_length_of.rb, line 42 def valid?(instance) val = instance.send(attribute) return true if val.nil? && @allow_nil == true actual_length = val&.length valid_length?(actual_length) end
valid_length?(actual_length)
click to toggle source
# File lib/simple_validate/validates_length_of.rb, line 29 def valid_length?(actual_length) case @validator when :minimum actual_length >= @valid_length when :maximum actual_length <= @valid_length when :in @valid_length.member?(actual_length) when :is actual_length == @valid_length end end