Skip to content

Commit

Permalink
Add pretty print support (#5810)
Browse files Browse the repository at this point in the history
* Add pretty print support

This adds the `pretty_print` method, which pretty prints the same information as the `inspect` method. This is meant to be called by the default 'pp' gem, e.g. by calling `pp(person)` or `person.pretty_inspect`.

The specs are an almost identical to the specs for the `inspect` method to ensure the same information is included.

* Simplify code with if modifier

Co-authored-by: Jamis Buck <[email protected]>

* Avoid "default gem" terminology

Co-authored-by: Jamis Buck <[email protected]>

---------

Co-authored-by: Jamis Buck <[email protected]>
Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
3 people authored Apr 30, 2024
1 parent a35d481 commit ff4c317
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/mongoid/inspectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,37 @@ def inspect
"#<#{self.class.name} _id: #{_id}, #{inspection * ', '}>"
end

# This pretty prints the same information as the inspect method. This is
# meant to be called by the standard 'pp' library.
#
# @param [ PP ] pretty_printer The pretty printer.
#
# @example Pretty print the document.
# person.pretty_inspect
#
# @api private
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

private

# Get an array of inspected fields for the document.
Expand Down
80 changes: 80 additions & 0 deletions spec/mongoid/inspectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,84 @@
end
end
end

describe "#pretty_inspect" do

context "when not allowing dynamic fields" do

let(:person) do
Person.new(title: "CEO")
end

let(:pretty_inspected) do
person.pretty_inspect
end

it "includes the model type" do
expect(pretty_inspected).to include("#<Person")
end

it "displays the id" do
expect(pretty_inspected).to include("_id: #{person.id}")
end

it "displays defined fields" do
expect(pretty_inspected).to include(%q,title: "CEO",)
end

it "displays field aliases" do
expect(pretty_inspected).to include("t(test):")
end

it "displays the default discriminator key" do
expect(pretty_inspected).to include(%q,_type: "Person",)
end
end

context "when using a custom discriminator key" do

before do
Person.discriminator_key = "dkey"
end

after do
Person.discriminator_key = nil
end

let(:person) do
Person.new(title: "CEO")
end

let(:pretty_inspected) do
person.pretty_inspect
end

it "displays the new discriminator key" do
expect(pretty_inspected).to include(%q,dkey: "Person",)
end
end

context "when allowing dynamic fields" do

let(:person) do
Person.new(title: "CEO", some_attribute: "foo")
end

let(:pretty_inspected) do
person.pretty_inspect
end

it "includes dynamic attributes" do
expect(pretty_inspected).to include(%q,some_attribute: "foo",)
end
end

context 'when id is unaliased' do
let(:shirt) { Shirt.new(id: 1, _id: 2) }

it 'shows the correct _id and id values' do
shirt.pretty_inspect.should == "#<Shirt _id: 2, color: nil, id: \"1\">\n"
end
end
end
end

0 comments on commit ff4c317

Please sign in to comment.