Sha256: c177aea198bf35bc60034faa7a3e15010e6af203c079a558a7e8122ed0d4bf0f

Contents?: true

Size: 826 Bytes

Versions: 6

Compression:

Stored size: 826 Bytes

Contents

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

      # Defines the behaviour for integer fields.
      class Integer
        include Serializable

        # 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 [ Integer ] The converted integer.
        #
        # @since 2.1.0
        def serialize(object)
          return nil if object.blank?
          begin
            object.to_s =~ /(^[-+]?[0-9]+$)|(\.0+)$/ ? Integer(object) : Float(object)
          rescue
            object
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongoid-2.2.6 lib/mongoid/fields/serializable/integer.rb
mongoid-2.2.5 lib/mongoid/fields/serializable/integer.rb
mongoid-2.2.4 lib/mongoid/fields/serializable/integer.rb
mongoid-2.2.3 lib/mongoid/fields/serializable/integer.rb
mongoid-2.2.2 lib/mongoid/fields/serializable/integer.rb
mongoid-2.2.1 lib/mongoid/fields/serializable/integer.rb