Class: RubyUnitValidator

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

Overview

Custom validator: ruby unit.

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



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

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