float.rb 722 字节
Newer Older
1
module ActiveModel
2 3
  module Type
    class Float < Value # :nodoc:
4
      include Helpers::Numeric
5 6 7 8 9

      def type
        :float
      end

10 11 12 13 14 15 16 17 18
      def type_cast_for_schema(value)
        return "::Float::NAN" if value.try(:nan?)
        case value
        when ::Float::INFINITY then "::Float::INFINITY"
        when -::Float::INFINITY then "-::Float::INFINITY"
        else super
        end
      end

S
Sean Griffin 已提交
19
      alias serialize cast
20 21 22 23

      private

      def cast_value(value)
24 25 26 27 28 29 30
        case value
        when ::Float then value
        when "Infinity" then ::Float::INFINITY
        when "-Infinity" then -::Float::INFINITY
        when "NaN" then ::Float::NAN
        else value.to_f
        end
31 32 33 34
      end
    end
  end
end