Skip to content

Commit

Permalink
changed inspect so that only non-nil keys are shown by default - rela…
Browse files Browse the repository at this point in the history
…ted to issue 194
  • Loading branch information
Jonathan Chambers authored and bkeepers committed Sep 2, 2011
1 parent 6c274a5 commit d5c52d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .project
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mongomapper</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.aptana.ide.core.unifiedBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.aptana.ruby.core.rubynature</nature>
</natures>
</projectDescription>
5 changes: 3 additions & 2 deletions lib/mongo_mapper/plugins/inspect.rb
Expand Up @@ -5,8 +5,9 @@ module Inspect
extend ActiveSupport::Concern

module InstanceMethods
def inspect
attributes_as_nice_string = key_names.sort.collect do |name|
def inspect(include_super=false)
key_array = include_super ? key_names : attributes.keys
attributes_as_nice_string = key_array.sort.collect do |name|
"#{name}: #{self[name].inspect}"
end.join(", ")
"#<#{self.class} #{attributes_as_nice_string}>"
Expand Down
7 changes: 6 additions & 1 deletion test/unit/test_inspect.rb
Expand Up @@ -6,14 +6,19 @@ class InspectTest < Test::Unit::TestCase
@document = Doc('User') do
key :name, String
key :age, Integer
key :email, String
end

@doc = @document.new(:name => 'John', :age => 29)
end

should "print out attributes in alpha sorted order" do
should "print out non-nil attributes in alpha sorted order" do
@doc.inspect.should =~ /_id:.*, age: 29, name: "John"/
end

should "print out all attributes when (optional) include_super argument is true" do
@doc.inspect(true).should =~ /_id:.*, age: 29, email: nil, name: "John"/
end

should "include class name" do
@doc.inspect.should =~ /^#<User/
Expand Down

0 comments on commit d5c52d8

Please sign in to comment.