Sha256: e668e71fcf17397e14806aa1b2b81a12a7ba236e13a1e74b553beb6285f89c94

Contents?: true

Size: 746 Bytes

Versions: 8

Compression:

Stored size: 746 Bytes

Contents

module ActiveRecord
  module Type
    class Decimal < Value # :nodoc:
      include Numeric

      def type
        :decimal
      end

      def type_cast_for_schema(value)
        value.to_s
      end

      private

      def cast_value(value)
        case value
        when ::Float
          BigDecimal(value, float_precision)
        when ::Numeric, ::String
          BigDecimal(value, precision.to_i)
        else
          if value.respond_to?(:to_d)
            value.to_d
          else
            cast_value(value.to_s)
          end
        end
      end

      def float_precision
        if precision.to_i > ::Float::DIG + 1
          ::Float::DIG + 1
        else
          precision.to_i
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
activerecord-4.2.0 lib/active_record/type/decimal.rb
activerecord-4.2.0.rc3 lib/active_record/type/decimal.rb
activerecord-4.2.0.rc2 lib/active_record/type/decimal.rb
activerecord-4.2.0.rc1 lib/active_record/type/decimal.rb
activerecord-4.2.0.beta4 lib/active_record/type/decimal.rb
activerecord-4.2.0.beta3 lib/active_record/type/decimal.rb
activerecord-4.2.0.beta2 lib/active_record/type/decimal.rb
activerecord-4.2.0.beta1 lib/active_record/type/decimal.rb