class Volt::NumericalityValidator
Attributes
errors[R]
Public Class Methods
new(model, field_name, args)
click to toggle source
# File lib/volt/models/validators/numericality_validator.rb, line 10 def initialize(model, field_name, args) @field_name = field_name @args = args @errors = {} @value = model.get(field_name) # Convert to float if it is a string for a float # The nil check and the nan? check are only require for opal 0.6 unless @value.nil? begin @value = Kernel.Float(@value) rescue ArgumentError => e @value = nil end # @value = nil if RUBY_PLATFORM == 'opal' && @value.nan? end check_errors end
validate(model, field_name, args)
click to toggle source
# File lib/volt/models/validators/numericality_validator.rb, line 3 def self.validate(model, field_name, args) # Construct the class and return the errors new(model, field_name, args).errors end
Public Instance Methods
add_error(error)
click to toggle source
# File lib/volt/models/validators/numericality_validator.rb, line 31 def add_error(error) field_errors = (@errors[@field_name] ||= []) field_errors << error end
check_errors()
click to toggle source
Looks at the value
# File lib/volt/models/validators/numericality_validator.rb, line 37 def check_errors if @value && @value.is_a?(Numeric) if @args.is_a?(Hash) @args.each do |arg, val| case arg when :min add_error("number must be greater than #{val}") if @value < val when :max add_error("number must be less than #{val}") if @value > val end end end else message = (@args.is_a?(Hash) && @args[:message]) || 'must be a number' add_error(message) end end