Class: RubyUnitValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/ruby_unit_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'app/validators/ruby_unit_validator.rb', line 2

def validate_each(object, attribute, value)
  [value].flatten.map(&:presence).compact.each do |v|
    begin
      u = RubyUnits::Unit.new("1 #{v}")
    rescue ArgumentError => exc
      object.errors.add(attribute.to_sym, (options[:message] || "#{v} is not a supported unit"))
    end
  end
end