Sha256: aef517fc0628492e3a1bc0c6f5dccdc37d4f1575ceef8e214e1b889c79679b20

Contents?: true

Size: 1.67 KB

Versions: 30

Compression:

Stored size: 1.67 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 its 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

        # Mongoize an object of any type to how it's stored in the db as a big
        # decimal.
        #
        # @example Mongoize the object.
        #   BigDecimal.mongoize(123)
        #
        # @param [ Object ] object The object to Mongoize
        #
        # @return [ String ] The mongoized object.
        #
        # @since 3.0.7
        def mongoize(object)
          object ? object.to_s : object
        end
      end
    end
  end
end

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

Version data entries

30 entries across 27 versions & 3 rubygems

Version Path
mongoid-5.4.1 lib/mongoid/extensions/big_decimal.rb
mongoid-5.4.0 lib/mongoid/extensions/big_decimal.rb
mongoid-5.2.1 lib/mongoid/extensions/big_decimal.rb
mongoid-5.2.0 lib/mongoid/extensions/big_decimal.rb
mongoid-5.2.0.rc0 lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.6 lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.5 lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.4 lib/mongoid/extensions/big_decimal.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-4.0.2/lib/mongoid/extensions/big_decimal.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mongoid-5.1.3/lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.3 lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.2 lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.1 lib/mongoid/extensions/big_decimal.rb
mongoid-5.1.0 lib/mongoid/extensions/big_decimal.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.1/lib/mongoid/extensions/big_decimal.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mongoid-5.0.2/lib/mongoid/extensions/big_decimal.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mongoid-5.0.2/lib/mongoid/extensions/big_decimal.rb
mongoid-5.0.2 lib/mongoid/extensions/big_decimal.rb
mongoid-5.0.1 lib/mongoid/extensions/big_decimal.rb
mongoid-5.0.0 lib/mongoid/extensions/big_decimal.rb