Sha256: 83d14edc23b3401a1577df1253147e715eb70146258dfa679dd148286cbd46e2

Contents?: true

Size: 1.22 KB

Versions: 15

Compression:

Stored size: 1.22 KB

Contents

require 'ardm/property/object'

module Ardm
  class Property
    class Numeric < Object
      accept_options :precision, :scale, :min, :max

      attr_reader :precision, :scale, :min, :max

      DEFAULT_NUMERIC_MIN = 0
      DEFAULT_NUMERIC_MAX = 2**31-1

    protected

      def initialize(model, name, options = {})
        super

        if kind_of?(Decimal) || kind_of?(Float)
          @precision = @options.fetch(:precision)
          @scale     = @options.fetch(:scale)

          unless @precision > 0
            raise ArgumentError, "precision must be greater than 0, but was #{@precision.inspect}"
          end
        end

        if @options.key?(:min) || @options.key?(:max)
          @min = @options.fetch(:min, self.class::DEFAULT_NUMERIC_MIN)
          @max = @options.fetch(:max, self.class::DEFAULT_NUMERIC_MAX)

          if @max < DEFAULT_NUMERIC_MIN && !@options.key?(:min)
            raise ArgumentError, "min should be specified when the max is less than #{DEFAULT_NUMERIC_MIN}"
          elsif @max < @min
            raise ArgumentError, "max must be less than the min, but was #{@max} while the min was #{@min}"
          end
        end
      end
    end # class Numeric
  end # class Property
end # module Ardm

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ardm-0.4.0.ar427 lib/ardm/property/numeric.rb
ardm-0.4.0 lib/ardm/property/numeric.rb
ardm-0.3.2 lib/ardm/property/numeric.rb
ardm-0.3.1 lib/ardm/property/numeric.rb
ardm-0.3.0 lib/ardm/property/numeric.rb
ardm-0.2.7 lib/ardm/property/numeric.rb
ardm-0.2.6 lib/ardm/property/numeric.rb
ardm-0.2.5 lib/ardm/property/numeric.rb
ardm-0.2.4 lib/ardm/property/numeric.rb
ardm-0.2.3 lib/ardm/property/numeric.rb
ardm-0.2.2 lib/ardm/property/numeric.rb
ardm-0.2.1 lib/ardm/property/numeric.rb
ardm-0.2.0 lib/ardm/property/numeric.rb
ardm-0.1.0 lib/ardm/property/numeric.rb
ardm-0.0.1 lib/ardm/property/numeric.rb