class ArrayTypesValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/array_types_validator.rb, line 2
def validate_each(record, attribute, value)
  record.errors[attribute] << "must contain instances of only: #{show_allowed_types}, was: #{types_for(value)}" unless allowed_type? value
end

Protected Instance Methods

allowed_type?(value) click to toggle source
# File lib/array_types_validator.rb, line 8
def allowed_type? value
  raise ArgumentError, "Must be an Array, was: #{value}" unless value.kind_of?(Array)
  return true if !allowed_types || allowed_types.empty?
  (types_for(value) - allowed_types).empty?
end
allowed_types() click to toggle source
# File lib/array_types_validator.rb, line 22
def allowed_types
  [only].flatten
end
only() click to toggle source
# File lib/array_types_validator.rb, line 26
def only
  options[:only] || options[:in]
end
show_allowed_types() click to toggle source
# File lib/array_types_validator.rb, line 18
def show_allowed_types
  allowed_types.join(", ")
end
types_for(value) click to toggle source
# File lib/array_types_validator.rb, line 14
def types_for value
  value.map(&:class).uniq
end