Skip to content

Commit

Permalink
Merge pull request #198 from seomoz/to_s_method
Browse files Browse the repository at this point in the history
Add a #to_s method inside Ripple::Inspection that prints the document key
  • Loading branch information
seancribbs committed Jul 28, 2011
2 parents 6b62eee + a9898f2 commit 3595941
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
23 changes: 21 additions & 2 deletions ripple/lib/ripple/inspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
module Ripple
# Makes IRB and other inspect output a bit friendlier
module Inspection

# A human-readable version of the {Ripple::Document} or {Ripple::EmbeddedDocument}
# plus attributes
def inspect
attribute_list = attributes_for_persistence.except("_type").map {|k,v| "#{k}=#{v.inspect}" }.join(' ')
identifier = self.class.embeddable? ? "" : ":#{key || '[new]'}"
"<#{self.class.name}#{identifier} #{attribute_list}>"
inspection_string(attribute_list)
end

# A string representation of the {Ripple::Document} or {Ripple::EmbeddedDocument}
def to_s
inspection_string
end

private

def inspection_string(extra = nil)
body = self.class.name + persistance_identifier
body += " #{extra}" if extra
"<#{body}>"
end

def persistance_identifier
self.class.embeddable? ? "" : ":#{key || '[new]'}"
end

end
end
58 changes: 37 additions & 21 deletions ripple/spec/ripple/inspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,48 @@
require 'support/models/box'
require 'support/models/address'

shared_examples_for 'an inspected document' do |method|

it "should include the class name in the inspect string" do
@box.send(method).should be_starts_with("<Box")
end

it "should include the key in the #{method} string for documents" do
@box.key = "square"
@box.send(method).should be_starts_with("<Box:square")
end

it "should indicate a new document when no key is specified" do
@box.send(method).should be_starts_with("<Box:[new]")
end

it "should not display a key for embedded documents" do
@address.send(method).should_not include("[new]")
end

end

before :each do
@box = Box.new
@address = Address.new
end

it "should include the class name in the inspect string" do
@box.inspect.should be_starts_with("<Box")
end

it "should include the key in the inspect string for documents" do
@box.key = "square"
@box.inspect.should be_starts_with("<Box:square")
end

it "should indicate a new document when no key is specified" do
@box.inspect.should be_starts_with("<Box:[new]")
end

it "should enumerate the document's properties and their values" do
@box.shape = "square"
@box.inspect.should include("shape=\"square\"")
@box.inspect.should include("created_at=")
@box.inspect.should include("updated_at=")
describe '#inspect' do

it_should_behave_like 'an inspected document', :inspect

it "should enumerate the document's properties and their values" do
@box.shape = "square"
@box.inspect.should include("shape=\"square\"")
@box.inspect.should include("created_at=")
@box.inspect.should include("updated_at=")
end

end

it "should not display a key for embedded documents" do
@address.inspect.should_not include("[new]")

describe '#to_s' do

it_should_behave_like 'an inspected document', :to_s

end
end

0 comments on commit 3595941

Please sign in to comment.