Sha256: 9e1b5efd1bbd334dd1acb454e54c3c51c8c4adce11325c0afde76679325a959e

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

# encoding: utf-8
module Mongoid
  module Extensions
    module BigDecimal

      # Convert the big decimal to an $inc-able value.
      #
      # @example Convert the big decimal.
      #   bd.__to_inc__
      #
      # @return [ Float ] The big decimal as a float.
      #
      # @since 3.0.3
      def __to_inc__
        to_f
      end

      # Turn the object from the ruby type we deal with to a Mongo friendly
      # type.
      #
      # @example Mongoize the object.
      #   object.mongoize
      #
      # @return [ Object ] The object.
      #
      # @since 3.0.0
      def mongoize
        to_s
      end

      module ClassMethods

        # Convert the object from it's mongo friendly ruby type to this type.
        #
        # @example Demongoize the object.
        #   Object.demongoize(object)
        #
        # @param [ Object ] object The object to demongoize.
        #
        # @return [ Object ] The object.
        #
        # @since 3.0.0
        def demongoize(object)
          if object
            object.numeric? ? ::BigDecimal.new(object.to_s) : object
          end
        end
      end
    end
  end
end

::BigDecimal.__send__(:include, Mongoid::Extensions::BigDecimal)
::BigDecimal.__send__(:extend, Mongoid::Extensions::BigDecimal::ClassMethods)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-3.0.6 lib/mongoid/extensions/big_decimal.rb
mongoid-3.0.5 lib/mongoid/extensions/big_decimal.rb
mongoid-3.0.4 lib/mongoid/extensions/big_decimal.rb
mongoid-3.0.3 lib/mongoid/extensions/big_decimal.rb