Sha256: 88750cd55e53cec613bef09af280fdfe7d5e310052413e0e5701002f21222038

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 KB

Contents

module Volt
  class NumericalityValidator
    def self.validate(model, field_name, args)
      # Construct the class and return the errors
      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.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?
        @value = Kernel.Float(@value) rescue nil
        @value = nil if RUBY_PLATFORM == 'opal' && @value.nan?
      end

      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

20 entries across 20 versions & 1 rubygems

Version Path
volt-0.9.1 lib/volt/models/validators/numericality_validator.rb
volt-0.9.1.pre5 lib/volt/models/validators/numericality_validator.rb
volt-0.9.1.pre4 lib/volt/models/validators/numericality_validator.rb
volt-0.9.1.pre3 lib/volt/models/validators/numericality_validator.rb
volt-0.9.1.pre2 lib/volt/models/validators/numericality_validator.rb
volt-0.9.1.pre1 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre7 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre6 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre5 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre4 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre3 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre2 lib/volt/models/validators/numericality_validator.rb
volt-0.9.0.pre1 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta9 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta8 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta7 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta6 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta5 lib/volt/models/validators/numericality_validator.rb
volt-0.8.27.beta4 lib/volt/models/validators/numericality_validator.rb