1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

add open struct support

This commit is contained in:
Marshall Shen 2014-01-02 18:10:24 -06:00 committed by James Cox
parent 84bfda9d07
commit a71586ad99
3 changed files with 59 additions and 0 deletions

View file

@ -36,4 +36,5 @@ unless defined?(AwesomePrint::Inspector)
require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer) require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer)
require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple) require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple)
require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel) require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct)
end end

View file

@ -0,0 +1,27 @@
# Copyright (c) 2010-2013 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 OpenStruct
def self.included(base)
base.send :alias_method, :cast_without_ostruct, :cast
base.send :alias_method, :cast, :cast_with_ostruct
end
def cast_with_ostruct(object, type)
cast = cast_without_ostruct(object, type)
if (defined?(::OpenStruct)) && (object.is_a?(::OpenStruct))
cast = :open_struct_instance
end
cast
end
def awesome_open_struct_instance(object)
"#{object.class} #{awesome_hash(object.marshal_dump)}"
end
end
end
AwesomePrint::Formatter.send(:include, AwesomePrint::OpenStruct)

31
spec/ext/ostruct_spec.rb Normal file
View file

@ -0,0 +1,31 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
begin
require 'ostruct'
require 'awesome_print/ext/ostruct'
describe 'AwesomePrint Ostruct extension' do
before do
stub_dotfile!
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
end
it "empty hash" do
struct = OpenStruct.new
@ap.send(:awesome, struct).should == "OpenStruct {}"
end
it "plain multiline" do
struct = OpenStruct.new :name => "Marshall Shen", :address => "605 Madion St."
@ap.send(:awesome, struct).should == <<-EOS.strip
OpenStruct {
:address => "605 Madion St.",
:name => "Marshall Shen"
}
EOS
end
end
rescue LoadError => error
puts "Skipping OpenStruct specs: #{error}"
end