Class: Moon::Validator::Length

Inherits:
Object
  • Object
show all
Defined in:
lib/moon/validator/length.rb

Overview

Value length validator.

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ Length

Returns a new instance of Length.



5
6
7
# File 'lib/moon/validator/length.rb', line 5

def initialize(range)
  @range = range
end

Instance Method Details

#messages(value, context) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/moon/validator/length.rb', line 9

def messages(value, context)
  raise ArgumentError, "Value doesn't response to :size" unless value.respond_to?(:size)
  size, minimum, maximum = value.size, @range.min, @range.max
  @range.include?(size) ?
    [ ] :
    [ "Must have at #{size < minimum ? ("least " + minimum.to_s) : ("most " + maximum.to_s)} entries." ]
end