Module: Mongoid::Inspectable

Included in:
Composable
Defined in:
lib/mongoid/inspectable.rb

Overview

Contains the behavior around inspecting documents via inspect.

Instance Method Summary collapse

Instance Method Details

#inspectString

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

Examples:

Inspect the document.

person.inspect

Returns:

  • (String)

    A nice pretty string to look at.



16
17
18
19
20
# File 'lib/mongoid/inspectable.rb', line 16

def inspect
  inspection = []
  inspection.concat(inspect_fields).concat(inspect_dynamic_fields)
  "#<#{self.class.name} _id: #{_id}, #{inspection * ', '}>"
end

#pretty_print(pretty_printer) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This pretty prints the same information as the inspect method. This is meant to be called by the standard ‘pp’ library.

Examples:

Pretty print the document.

person.pretty_inspect

Parameters:

  • pretty_printer (PP)

    The pretty printer.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongoid/inspectable.rb', line 31

def pretty_print(pretty_printer)
  keys = fields.keys | attributes.keys
  pretty_printer.group(1, "#<#{self.class.name}", '>') do
    sep = lambda { pretty_printer.text(',') }
    pretty_printer.seplist(keys, sep) do |key|
      pretty_printer.breakable
      field = fields[key]
      as = "(#{field.options[:as]})" if field && field.options[:as]
      pretty_printer.text("#{key}#{as}")
      pretty_printer.text(':')
      pretty_printer.group(1) do
        pretty_printer.breakable
        if key == "_id"
          pretty_printer.text(_id.to_s)
        else
          pretty_printer.pp(@attributes[key])
        end
      end
    end
  end
end