Sha256: 17702e93a4306e63d07122d1e329b4317bf880f77b0692d1fc0eddf372ec8756

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

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

      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 is_a?(Decimal) || is_a?(Float)
          @precision = @options.fetch(:precision)
          @scale     = @options.fetch(:scale)

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

        return unless @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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sbf-dm-core-1.5.0 lib/dm-core/property/numeric.rb
sbf-dm-core-1.4.0 lib/dm-core/property/numeric.rb
sbf-dm-core-1.3.0 lib/dm-core/property/numeric.rb
sbf-dm-core-1.3.0.beta lib/dm-core/property/numeric.rb