Skip to content

Commit

Permalink
added support for Struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mullis committed May 10, 2010
1 parent ba89f3d commit 34f268f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/ap/awesome_print.rb
Expand Up @@ -7,7 +7,7 @@

class AwesomePrint
AP = :__awesome_print__
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational ]
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct ]

def initialize(options = {})
@options = {
Expand All @@ -23,6 +23,7 @@ def initialize(options = {})
:fixnum => :blue,
:float => :blue,
:hash => :pale,
:struct => :pale,
:nilclass => :red,
:string => :yellowish,
:symbol => :cyanish,
Expand Down Expand Up @@ -92,6 +93,14 @@ def awesome_hash(h)
end
end

# Format a Struct. If @options[:indent] if negative left align hash keys.
#------------------------------------------------------------------------------
def awesome_struct(s)
h = {}
s.each_pair { |k,v| h[k] = v }
awesome_hash(h)
end

# Format Class object.
#------------------------------------------------------------------------------
def awesome_class(c)
Expand Down Expand Up @@ -154,6 +163,7 @@ def nested(object)
case printable(object)
when :array then colorize("[...]", :array)
when :hash then colorize("{...}", :hash)
when :struct then colorize("{...}", :struct)
else colorize("...#{object.class}...", :class)
end
end
Expand All @@ -167,12 +177,20 @@ def printable(object)
# Turn class name into symbol, ex: Hello::World => :hello_world.
#------------------------------------------------------------------------------
def declassify(object)
object.class.to_s.gsub(/:+/, "_").downcase.to_sym
if object.class.to_s.downcase =~ /^struct/
result = :struct
else
result = object.class.to_s.gsub(/:+/, "_").downcase.to_sym
end
result
end

# Pick the color and apply it to the given string as necessary.
#------------------------------------------------------------------------------
def colorize(s, type)
if type && !type.to_s.empty?
type = type.to_s.gsub(/^struct_.*/,'struct').to_sym
end
if @options[:plain] || @options[:color][type].nil?
s
else
Expand Down
47 changes: 47 additions & 0 deletions spec/awesome_print_spec.rb
Expand Up @@ -288,4 +288,51 @@
end
end


#------------------------------------------------------------------------------
describe "Struct" do
before(:each) do
struct = Struct.new("SimpleStruct", :name, :address)
@struct = struct.new
@struct.name = "Herman Munster"
@struct.address = "1313 Mockingbird Lane"
end

it "empty struct" do
Struct.new("EmptyStruct").ai.should == "\e[1;33mStruct::EmptyStruct < Struct\e[0m"
end

it "plain multiline" do
@struct.ai(:plain => true).should == <<-EOS.strip
{
:address => "1313 Mockingbird Lane",
:name => "Herman Munster"
}
EOS
end

it "plain multiline indented" do
@struct.ai(:plain => true, :indent => 1).should == <<-EOS.strip
{
:address => "1313 Mockingbird Lane",
:name => "Herman Munster"
}
EOS
end

it "plain single line" do
@struct.ai(:plain => true, :multiline => false).should == "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
end

it "colored multiline (default)" do
@struct.ai.should == <<-EOS.strip
{
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
}
EOS
end
end


end

0 comments on commit 34f268f

Please sign in to comment.