module Mongoid::Inspection

Contains the bahviour around inspecting documents via Object#inspect.

Public Instance Methods

inspect() click to toggle source

Returns the class name plus its attributes. If using dynamic fields will include those as well.

@example Inspect the document.

person.inspect

@return [ String ] A nice pretty string to look at.

# File lib/mongoid/inspection.rb, line 14
def inspect
  inspection = []
  inspection.concat(inspect_fields).concat(inspect_dynamic_fields)
  "#<#{self.class.name} _id: #{id}, #{inspection * ', '}>"
end

Private Instance Methods

inspect_dynamic_fields() click to toggle source

Get an array of inspected dynamic fields for the document.

@example Inspect the dynamic fields.

document.inspect_dynamic_fields

@return [ String ] An array of pretty printed dynamic field values.

# File lib/mongoid/inspection.rb, line 43
def inspect_dynamic_fields
  if Mongoid.allow_dynamic_fields
    keys = @attributes.keys - fields.keys - relations.keys - ["_id", "_type"]
    return keys.map do |name|
      "#{name}: #{@attributes[name].inspect}"
    end
  else
    []
  end
end
inspect_fields() click to toggle source

Get an array of inspected fields for the document.

@example Inspect the defined fields.

document.inspect_fields

@return [ String ] An array of pretty printed field values.

# File lib/mongoid/inspection.rb, line 28
def inspect_fields
  fields.map do |name, field|
    unless name == "_id"
      as = field.options[:as]
      "#{name}#{as ? "(#{as})" : nil}: #{@attributes[name].inspect}"
    end
  end.compact
end