Skip to content

Commit ff4c317

Browse files
stenlarssonjamis
andauthored
Add pretty print support (#5810)
* 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]>
1 parent a35d481 commit ff4c317

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

lib/mongoid/inspectable.rb

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ def inspect
1919
"#<#{self.class.name} _id: #{_id}, #{inspection * ', '}>"
2020
end
2121

22+
# This pretty prints the same information as the inspect method. This is
23+
# meant to be called by the standard 'pp' library.
24+
#
25+
# @param [ PP ] pretty_printer The pretty printer.
26+
#
27+
# @example Pretty print the document.
28+
# person.pretty_inspect
29+
#
30+
# @api private
31+
def pretty_print(pretty_printer)
32+
keys = fields.keys | attributes.keys
33+
pretty_printer.group(1, "#<#{self.class.name}", '>') do
34+
sep = lambda { pretty_printer.text(',') }
35+
pretty_printer.seplist(keys, sep) do |key|
36+
pretty_printer.breakable
37+
field = fields[key]
38+
as = "(#{field.options[:as]})" if field && field.options[:as]
39+
pretty_printer.text("#{key}#{as}")
40+
pretty_printer.text(':')
41+
pretty_printer.group(1) do
42+
pretty_printer.breakable
43+
if key == "_id"
44+
pretty_printer.text(_id.to_s)
45+
else
46+
pretty_printer.pp(@attributes[key])
47+
end
48+
end
49+
end
50+
end
51+
end
52+
2253
private
2354

2455
# Get an array of inspected fields for the document.

spec/mongoid/inspectable_spec.rb

+80
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,84 @@
8484
end
8585
end
8686
end
87+
88+
describe "#pretty_inspect" do
89+
90+
context "when not allowing dynamic fields" do
91+
92+
let(:person) do
93+
Person.new(title: "CEO")
94+
end
95+
96+
let(:pretty_inspected) do
97+
person.pretty_inspect
98+
end
99+
100+
it "includes the model type" do
101+
expect(pretty_inspected).to include("#<Person")
102+
end
103+
104+
it "displays the id" do
105+
expect(pretty_inspected).to include("_id: #{person.id}")
106+
end
107+
108+
it "displays defined fields" do
109+
expect(pretty_inspected).to include(%q,title: "CEO",)
110+
end
111+
112+
it "displays field aliases" do
113+
expect(pretty_inspected).to include("t(test):")
114+
end
115+
116+
it "displays the default discriminator key" do
117+
expect(pretty_inspected).to include(%q,_type: "Person",)
118+
end
119+
end
120+
121+
context "when using a custom discriminator key" do
122+
123+
before do
124+
Person.discriminator_key = "dkey"
125+
end
126+
127+
after do
128+
Person.discriminator_key = nil
129+
end
130+
131+
let(:person) do
132+
Person.new(title: "CEO")
133+
end
134+
135+
let(:pretty_inspected) do
136+
person.pretty_inspect
137+
end
138+
139+
it "displays the new discriminator key" do
140+
expect(pretty_inspected).to include(%q,dkey: "Person",)
141+
end
142+
end
143+
144+
context "when allowing dynamic fields" do
145+
146+
let(:person) do
147+
Person.new(title: "CEO", some_attribute: "foo")
148+
end
149+
150+
let(:pretty_inspected) do
151+
person.pretty_inspect
152+
end
153+
154+
it "includes dynamic attributes" do
155+
expect(pretty_inspected).to include(%q,some_attribute: "foo",)
156+
end
157+
end
158+
159+
context 'when id is unaliased' do
160+
let(:shirt) { Shirt.new(id: 1, _id: 2) }
161+
162+
it 'shows the correct _id and id values' do
163+
shirt.pretty_inspect.should == "#<Shirt _id: 2, color: nil, id: \"1\">\n"
164+
end
165+
end
166+
end
87167
end

0 commit comments

Comments
 (0)