Sha256: aa949a0c0493787f61d84c23abde6c8b48403802f662c912994b14925dfbf74e

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

module Volt
  class NumericalityValidator
    def self.validate(model, old_model, field_name, args)
      # Construct the class and return the errors
      self.new(model, field_name, args).errors
    end

    attr_reader :errors

    def initialize(model, field_name, args)
      @field_name = field_name
      @args = args
      @errors = {}

      @value = model.read_attribute(field_name)

      # Convert to float if it is a string for a float
      @value = Kernel.Float(@value) rescue nil

      check_errors
    end

    def add_error(error)
      field_errors = (@errors[@field_name] ||= [])
      field_errors << error
    end

    # Looks at the value
    def check_errors
      if @value && @value.is_a?(Numeric)
        if @args.is_a?(Hash)

          @args.each do |arg, val|
            case arg
            when :min
              if @value < val
               add_error("number must be greater than #{val}")
              end
            when :max
              if @value > val
                add_error("number must be less than #{val}")
              end
            end
          end

        end
      else
        message = (@args.is_a?(Hash) && @args[:message]) || 'must be a number'
        add_error(message)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
volt-0.8.27.beta2 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta1 lib/volt/models/validators/numericality_validator.rb
volt-0.8.26.beta1 lib/volt/models/validators/numericality_validator.rb
volt-0.8.26 lib/volt/models/validators/numericality_validator.rb
volt-0.8.24 lib/volt/models/validators/numericality_validator.rb
volt-0.8.23 lib/volt/models/validators/numericality_validator.rb