Sha256: a9fa33ead92c59246b99317e34bcd3c2ea7bfffa0ba1e6764de02c1217c56022

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

require 'dm-core/property/typecast/numeric'

module DataMapper
  class Property
    class Numeric < Object
      include PassThroughLoadDump
      include Typecast::Numeric

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

      DEFAULT_PRECISION        = 10
      DEFAULT_NUMERIC_MIN      = 0
      DEFAULT_NUMERIC_MAX      = 2**31-1

      protected

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

        if @primitive == BigDecimal || @primitive == ::Float
          @precision = @options.fetch(:precision, DEFAULT_PRECISION)
          @scale     = @options.fetch(:scale,     self.class::DEFAULT_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, DEFAULT_NUMERIC_MIN)
          @max = @options.fetch(:max, 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 DataMapper

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dm-core-1.1.0 lib/dm-core/property/numeric.rb
dm-core-1.1.0.rc3 lib/dm-core/property/numeric.rb
dm-core-1.1.0.rc2 lib/dm-core/property/numeric.rb
dm-core-1.1.0.rc1 lib/dm-core/property/numeric.rb