class ArrayValidator

Attributes

attrib[R]

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/array_validator.rb, line 6
def validate_each(record, attribute, value)
  @attrib = attribute
  record.errors[attribute] << "must be in: #{show_list}" unless contains? value
end

Protected Instance Methods

contains?(value) click to toggle source
# File lib/array_validator.rb, line 13
def contains? value
  return true if value.nil?
  raise ArgumentError, "#{attrib} must be an Array, was: #{value}" unless value.kind_of?(Array)
  (value - list).empty?
end
list() click to toggle source

also works with range

# File lib/array_validator.rb, line 20
def list
  only.to_a
end
only() click to toggle source
# File lib/array_validator.rb, line 24
def only
  options[:in] || options[:only]
end
show_list() click to toggle source
# File lib/array_validator.rb, line 28
def show_list
  list.join(", ")
end