Sha256: 3e450b7409bcd386633778498c3cd197b472b0d7dbb76d7a5ced4af362a54cc7

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

module DataMapper
  class Property
    class Decimal < Numeric
      primitive BigDecimal

      DEFAULT_SCALE = 0

      protected

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

        unless @scale.nil?
          unless @scale >= 0
            raise ArgumentError, "scale must be equal to or greater than 0, but was #{@scale.inspect}"
          end

          unless @precision >= @scale
            raise ArgumentError, "precision must be equal to or greater than scale, but was #{@precision.inspect} and scale was #{scale_inspect}"
          end
        end
      end

      # Typecast a value to a BigDecimal
      #
      # @param [#to_str, #to_d, Integer] value
      #   value to typecast
      #
      # @return [BigDecimal]
      #   BigDecimal constructed from value
      #
      # @api private
      def typecast_to_primitive(value)
        if value.kind_of?(::Integer)
          # TODO: remove this case when Integer#to_d added by extlib
          value.to_s.to_d
        else
          typecast_to_numeric(value, :to_d)
        end
      end
    end # class Decimal
  end # class Property
end # module DataMapper

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dm-core-1.0.2 lib/dm-core/property/decimal.rb
dm-core-1.0.1 lib/dm-core/property/decimal.rb
dm-core-1.0.0 lib/dm-core/property/decimal.rb
dm-core-1.0.0.rc3 lib/dm-core/property/decimal.rb
dm-core-1.0.0.rc2 lib/dm-core/property/decimal.rb
dm-core-1.0.0.rc1 lib/dm-core/property/decimal.rb