Commits

Sean Cribbs committed 7919e4a

Add prettier inspection output.

Comments (0)

Files changed (5)

ripple/lib/ripple.rb

   autoload :PropertyTypeMismatch
 
   # Utilities
+  autoload :Inspection
   autoload :Translation
 
   class << self

ripple/lib/ripple/document.rb

       include Ripple::Callbacks
       include Ripple::Conversion
       include Ripple::Document::Finders
+      include Ripple::Inspection
     end
 
     module ClassMethods

ripple/lib/ripple/embedded_document.rb

       include Ripple::Callbacks
       include Ripple::Conversion
       include Finders
+      include Ripple::Inspection
     end
 
     module ClassMethods

ripple/lib/ripple/inspection.rb

+# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+
+require 'ripple'
+
+module Ripple
+  # Makes IRB and other inspect output a bit friendlier
+  module Inspection
+    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}>"
+    end
+  end
+end

ripple/spec/ripple/inspection_spec.rb

+# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+require File.expand_path("../../spec_helper", __FILE__)
+
+describe Ripple::Inspection do
+  require 'support/models/box'
+  require 'support/models/address'
+
+  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=")
+  end
+  
+  it "should not display a key for embedded documents" do
+    @address.inspect.should_not include("[new]")
+  end
+end