Class: UnitValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/unit_validation.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/unit_validation.rb', line 3

def validate_each(record, attribute, value)
  allowed=Array(options[:allowed_types]) if options[:allowed_types]
  begin
  v=Unit.new(value)
  if allowed
      unless allowed.include?(v.kind)
          record.errors[attribute] << (options[:message] || "is not a valid unit of #{allowed.collect(&:to_s).join(',')}")
      end
  end
  rescue 
    record.errors[attribute] << (options[:message] || "is not a valid measurement")
  end
end