module Mongoid::Serializable

def field_names(options)

Other tags:
    Since: - 3.0.0

Returns:
  • (Array) - The names of the fields.

Other tags:
    Example: Get all the field names. -

Other tags:
    Api: - private
def field_names(options)
  names = (as_attributes.keys + attribute_names).uniq.sort
  only = Array.wrap(options[:only]).map(&:to_s)
  except = Array.wrap(options[:except]).map(&:to_s)
  except |= ['_type'] unless Mongoid.include_type_for_serialization
  if !only.empty?
    names &= only
  elsif !except.empty?
    names -= except
  end
  names
end

def relation_names(inclusions)

Other tags:
    Since: - 2.0.0.rc.6

Returns:
  • (Array) - The names of the included associations.

Parameters:
  • inclusions (Hash, Symbol, Array) -- The inclusions.

Other tags:
    Example: Get the association names. -
def relation_names(inclusions)
  inclusions.is_a?(Hash) ? inclusions.keys : Array.wrap(inclusions)
end

def relation_options(inclusions, options, name)

Other tags:
    Since: - 2.0.0.rc.6

Returns:
  • (Hash) - The options for the association.

Parameters:
  • name (Symbol) -- The name of the association.
  • options (Hash) -- The options.
  • inclusions (Hash, Symbol, Array) -- The inclusions.

Other tags:
    Example: Get the association options. -
def relation_options(inclusions, options, name)
  if inclusions.is_a?(Hash)
    inclusions[name]
  else
    { except: options[:except], only: options[:only] }
  end
end

def serializable_hash(options = nil)

Other tags:
    Since: - 2.0.0.rc.6

Returns:
  • (Hash) - The document, ready to be serialized.

Options Hash: (**options)
  • :methods (Symbol) -- What methods to include.
  • :except (Symbol) -- Dont include these fields.
  • :only (Symbol) -- Limit the fields to only these.
  • :include (Symbol) -- What associations to include.

Parameters:
  • options (Hash) -- The options to pass.

Other tags:
    Example: Get the serializable hash with options. -
    Example: Get the serializable hash. -
def serializable_hash(options = nil)
  options ||= {}
  attrs = {}
  names = field_names(options)
  method_names = Array.wrap(options[:methods]).map do |name|
    name.to_s if respond_to?(name)
  end.compact
  (names + method_names).each do |name|
    without_autobuild do
      serialize_attribute(attrs, name, names, options)
    end
  end
  serialize_relations(attrs, options) if options[:include]
  attrs
end

def serialize_attribute(attrs, name, names, options)

Other tags:
    Since: - 3.0.0

Returns:
  • (Object) - The attribute.

Parameters:
  • options (Hash) -- The options.
  • names (Array) -- The names of all attributes.
  • name (String) -- The attribute name.
  • attrs (Hash) -- The attributes.

Other tags:
    Example: Serialize the attribute. -

Other tags:
    Api: - private
def serialize_attribute(attrs, name, names, options)
  if relations.key?(name)
    value = send(name)
    attrs[name] = value ? value.serializable_hash(options) : nil
  elsif names.include?(name) && !fields.key?(name)
    attrs[name] = read_raw_attribute(name)
  elsif !attribute_missing?(name)
    attrs[name] = send(name)
  end
end

def serialize_relations(attributes = {}, options = {})

Other tags:
    Since: - 2.0.0.rc.6

Options Hash: (**options)
  • :except (Symbol) -- Dont include these fields.
  • :only (Symbol) -- Limit the fields to only these.
  • :include (Symbol) -- What associations to include

Parameters:
  • options (Hash) -- The serialization options.
  • attributes (Hash) -- The attributes to serialize.

Other tags:
    Example: Serialize the included associations. -
def serialize_relations(attributes = {}, options = {})
  inclusions = options[:include]
  relation_names(inclusions).each do |name|
    association = relations[name.to_s]
    if association && relation = send(association.name)
      attributes[association.name.to_s] =
        relation.serializable_hash(relation_options(inclusions, options, name))
    end
  end
end