From 553cd447be9ccdd61fd168dc5a79f573a75659bd Mon Sep 17 00:00:00 2001 From: shyndman Date: Thu, 7 Feb 2013 15:23:52 -0500 Subject: [PATCH 1/6] Added a basic implementation of a Ripple formatter --- lib/awesome_print.rb | 1 + lib/awesome_print/ext/ripple.rb | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lib/awesome_print/ext/ripple.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 0d04e3b..eeddfef 100755 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -29,4 +29,5 @@ unless defined?(AwesomePrint::Inspector) require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper) require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid) require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri) + require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) end diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb new file mode 100644 index 0000000..efb280b --- /dev/null +++ b/lib/awesome_print/ext/ripple.rb @@ -0,0 +1,64 @@ +# Copyright (c) 2010-2012 Michael Dvorkin +# +# Awesome Print is freely distributable under the terms of MIT license. +# See LICENSE file or http://www.opensource.org/licenses/mit-license.php +#------------------------------------------------------------------------------ +module AwesomePrint + module Ripple + + def self.included(base) + base.send :alias_method, :cast_without_ripple, :cast + base.send :alias_method, :cast, :cast_with_ripple + end + + # Add Ripple class names to the dispatcher pipeline. + #------------------------------------------------------------------------------ + def cast_with_ripple(object, type) + cast = cast_without_ripple(object, type) + return cast if !defined?(::Ripple) + + if object.is_a?(::Ripple::AttributeMethods) # Module used to access attributes across documents and embedded documents + cast = :ripple_document_instance + elsif object.is_a?(::Ripple::Properties) # Used to access property metadata on Ripple classes + cast = :ripple_document_class + end + cast + end + + private + + # Format Ripple instance object. + # + # NOTE: by default only instance attributes (i.e. columns) are shown. To format + # Ripple document instance as regular object showing its instance variables and + # accessors use :raw => true option: + # + # ap document, :raw => true + # + #------------------------------------------------------------------------------ + def awesome_ripple_document_instance(object) + return object.inspect if !defined?(::ActiveSupport::OrderedHash) + return awesome_object(object) if @options[:raw] + + data = object.attributes.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, value)| + hash[name.to_sym] = object.send(name) + hash + end + "##{object} " << awesome_hash(data) + end + + # Format Ripple class object. + #------------------------------------------------------------------------------ + def awesome_ripple_document_class(object) + return object.inspect if !defined?(::ActiveSupport::OrderedHash) || !object.respond_to?(:properties) + + data = object.properties.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, defn)| + hash[name.to_sym] = defn.type.to_s.downcase.to_sym + hash + end + "class #{object} < #{object.superclass} " << awesome_hash(data) + end + end +end + +AwesomePrint::Formatter.send(:include, AwesomePrint::Ripple) From bada37f595fb3328ca6042d24458de732f4a2401 Mon Sep 17 00:00:00 2001 From: shyndman Date: Thu, 7 Feb 2013 15:26:34 -0500 Subject: [PATCH 2/6] Made a comment more applicable to Ripple --- lib/awesome_print/ext/ripple.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index efb280b..825b24d 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -29,9 +29,8 @@ module AwesomePrint # Format Ripple instance object. # - # NOTE: by default only instance attributes (i.e. columns) are shown. To format - # Ripple document instance as regular object showing its instance variables and - # accessors use :raw => true option: + # NOTE: by default only instance attributes are shown. To format Ripple document instance + # as regular object showing its instance variables and accessors use :raw => true option: # # ap document, :raw => true # From 7eef826a63a15848e133c1722a9eea3d909a6cce Mon Sep 17 00:00:00 2001 From: shyndman Date: Thu, 7 Feb 2013 15:53:44 -0500 Subject: [PATCH 3/6] Added specs for Ripple (but still can't get them to run) --- spec/ext/ripple_spec.rb | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 spec/ext/ripple_spec.rb diff --git a/spec/ext/ripple_spec.rb b/spec/ext/ripple_spec.rb new file mode 100644 index 0000000..ec1c8cb --- /dev/null +++ b/spec/ext/ripple_spec.rb @@ -0,0 +1,55 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +begin + require 'ripple' + require 'awesome_print/ext/ripple' + + describe 'AwesomePrint/Ripple' do + before :all do + class RippleUser + include Ripple::Document + + key_on :_id + property :_id, String + property :first_name, String + property :last_name, String + end + end + + after :all do + Object.instance_eval { remove_const :RippleUser } + end + + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new :plain => true, :sort_keys => true + end + + it "should print class instance" do + user = RippleUser.new :_id => '12345' :first_name => "Al", :last_name => "Capone" + out = @ap.send :awesome, user + + out.should == <<-EOS.strip +# { + :_id => "12345", + :first_name => "Al", + :last_name => "Capone" +} +EOS + end + + it "should print the class" do + @ap.send(:awesome, RippleUser).should == <<-EOS.strip +class RippleUser < Object { + :_id => :string, + :_type => :string, + :first_name => :string, + :last_name => :string +} +EOS + end + end + +rescue LoadError => error + puts "Skipping Ripple specs: #{error}" +end From 66d59020c2be25560051be1dc6feaa9ded4707b7 Mon Sep 17 00:00:00 2001 From: shyndman Date: Thu, 7 Feb 2013 16:03:12 -0500 Subject: [PATCH 4/6] Comment improvement --- lib/awesome_print/ext/ripple.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index 825b24d..75cd2cb 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -29,8 +29,8 @@ module AwesomePrint # Format Ripple instance object. # - # NOTE: by default only instance attributes are shown. To format Ripple document instance - # as regular object showing its instance variables and accessors use :raw => true option: + # NOTE: by default only instance attributes are shown. To format a Ripple document instance + # as a regular object showing its instance variables and accessors use :raw => true option: # # ap document, :raw => true # From 581de0cbebe569c29cc99f27b309638d979c30cd Mon Sep 17 00:00:00 2001 From: shyndman Date: Thu, 7 Feb 2013 16:56:14 -0500 Subject: [PATCH 5/6] Added support for awesome printing embedded assocations --- lib/awesome_print/ext/ripple.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index 75cd2cb..c8a5f4b 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -38,11 +38,20 @@ module AwesomePrint def awesome_ripple_document_instance(object) return object.inspect if !defined?(::ActiveSupport::OrderedHash) return awesome_object(object) if @options[:raw] + exclude_assoc = @options[:exclude_assoc] or @options[:exclude_associations] data = object.attributes.inject(::ActiveSupport::OrderedHash.new) do |hash, (name, value)| hash[name.to_sym] = object.send(name) hash end + + unless exclude_assoc + data = object.embedded_associations.inject(data) do |hash, assoc| + hash[assoc.name] = assoc.load_target + hash + end + end + "##{object} " << awesome_hash(data) end From 54b06ccac74a5c5925305e7ceb85ce88338b9058 Mon Sep 17 00:00:00 2001 From: shyndman Date: Thu, 7 Feb 2013 17:00:17 -0500 Subject: [PATCH 6/6] Bug fixes --- lib/awesome_print/ext/ripple.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/awesome_print/ext/ripple.rb b/lib/awesome_print/ext/ripple.rb index c8a5f4b..e1fc03c 100644 --- a/lib/awesome_print/ext/ripple.rb +++ b/lib/awesome_print/ext/ripple.rb @@ -46,8 +46,8 @@ module AwesomePrint end unless exclude_assoc - data = object.embedded_associations.inject(data) do |hash, assoc| - hash[assoc.name] = assoc.load_target + data = object.class.embedded_associations.inject(data) do |hash, assoc| + hash[assoc.name] = object.get_proxy(assoc) # Should always be array or Ripple::EmbeddedDocument for embedded associations hash end end