Sha256: 02324bead806c7e41d5c176462bf29562d449f0936d2e3a58cbf540a58be40ca

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Fields #:nodoc:
    module Internal #:nodoc:

      # Defines the behaviour for big decimal fields.
      class BigDecimal
        include Serializable

        # Deserialize this field from the type stored in MongoDB to the type
        # defined on the model.
        #
        # @example Deserialize the field.
        #   field.deserialize(object)
        #
        # @param [ Object ] object The object to cast.
        #
        # @return [ BigDecimal ] The converted big decimal.
        #
        # @since 2.1.0
        def deserialize(object)
          return object unless object
          begin
            Float(object) unless object == 'NaN'
            ::BigDecimal.new(object)
          rescue ArgumentError, TypeError
            object
          end
        end

        # Special case to serialize the object.
        #
        # @example Convert to a selection.
        #   field.selection(object)
        #
        # @param [ Object ] The object to convert.
        #
        # @return [ Object ] The converted object.
        #
        # @since 2.4.0
        def selection(object)
          return object if object.is_a?(::Hash)
          serialize(object)
        end

        # Serialize the object from the type defined in the model to a MongoDB
        # compatible object to store.
        #
        # @example Serialize the field.
        #   field.serialize(object)
        #
        # @param [ Object ] object The object to cast.
        #
        # @return [ String ] The converted string.
        #
        # @since 2.1.0
        def serialize(object)
          object ? object.to_s : object
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-2.8.1 lib/mongoid/fields/internal/big_decimal.rb
mongoid-2.8.0 lib/mongoid/fields/internal/big_decimal.rb
mongoid-2.7.1 lib/mongoid/fields/internal/big_decimal.rb
mongoid-2.7.0 lib/mongoid/fields/internal/big_decimal.rb