Sha256: 58b2668bd8679d64627eb084c31ace876bb8e984e6c2265c3de2205bb3c972ac

Contents?: true

Size: 1.72 KB

Versions: 8

Compression:

Stored size: 1.72 KB

Contents

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

      # Defines the behaviour for array fields.
      class Array
        include Serializable

        # Get the default value for the field. If the default is a proc call
        # it, otherwise clone the array.
        #
        # @example Get the default.
        #   field.default
        #
        # @return [ Object ] The default value.
        #
        # @since 2.1.0
        def default
          return nil unless default_value
          default_value.respond_to?(:call) ? default_value.call : default_value.dup
        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 [ Array ] The converted object.
        #
        # @since 2.1.0
        def serialize(object)
          raise_or_return(object)
        end
        alias :set :serialize

        protected

        # If the value is not an array or nil we will raise an error,
        # otherwise return the value.
        #
        # @example Raise or return the value.
        #   field.raise_or_return([])
        #
        # @param [ Object ] value The value to check.a
        #
        # @raise [ InvalidType ] If not passed an array.
        #
        # @return [ Array ] The array.
        #
        # @since 2.1.0
        def raise_or_return(value)
          unless value.nil? || value.is_a?(::Array)
            raise Mongoid::Errors::InvalidType.new(::Array, value)
          end
          value
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-2.1.7 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.6 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.5 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.4 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.3 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.2 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.1 lib/mongoid/fields/serializable/array.rb
mongoid-2.1.0 lib/mongoid/fields/serializable/array.rb