class RangeValidator

Attributes

attrib[R]

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/simple_range_validator.rb, line 7
def validate_each(record, attribute, value)
  @attrib = attribute
  record.errors[attribute] << "must be within: #{show_range}" unless within? value
end

Protected Instance Methods

range() click to toggle source
# File lib/simple_range_validator.rb, line 20
def range
  options[:within]
end
show_range() click to toggle source
# File lib/simple_range_validator.rb, line 24
def show_range
  "#{range.min}-#{range.max}"
end
within?(value) click to toggle source
# File lib/simple_range_validator.rb, line 14
def within? value
  return true if value.nil?
  raise ArgumentError, "#{attrib} must be a Range, was: #{value}" unless value.kind_of?(Range)
  range.cover?(value.min) && range.cover?(value.max)
end