Sha256: dbb55bb405f3a61d86b82012d72fd7d884594c4be48545dc072acf69bd032b2d

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module DataMapper
  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 DataMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ghost_dm-core-1.3.0.beta lib/dm-core/property/numeric.rb