Sha256: 44efb431812aaa52b65f8db21b9fbd4e4bdcb61da74b5b74f6122f824b3eada9

Contents?: true

Size: 1.92 KB

Versions: 14

Compression:

Stored size: 1.92 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

      # Is the BigDecimal a number?
      #
      # @example Is the object a number?.
      #   object.numeric?
      #
      # @return [ true ] Always true.
      #
      # @since 6.0.0
      def numeric?
        true
      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 [ BigDecimal, nil ] A BigDecimal derived from the object or nil.
        #
        # @since 3.0.0
        def demongoize(object)
          object && object.numeric? ? BigDecimal(object.to_s) : nil
        end

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

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mongoid-7.0.13 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.12 lib/mongoid/extensions/big_decimal.rb
mongoid-6.4.8 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.11 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.10 lib/mongoid/extensions/big_decimal.rb
mongoid-6.4.7 lib/mongoid/extensions/big_decimal.rb
mongoid-6.4.5 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.8 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.7 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.6 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.5 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.4 lib/mongoid/extensions/big_decimal.rb
mongoid-6.4.4 lib/mongoid/extensions/big_decimal.rb
mongoid-7.0.3 lib/mongoid/extensions/big_decimal.rb