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

Added method formatting

This commit is contained in:
Mike Dvorkin 2010-10-30 13:50:48 -07:00
parent 4dde5baab4
commit 14e4fd5e7d
2 changed files with 74 additions and 2 deletions

View file

@ -7,7 +7,7 @@ require "shellwords"
class AwesomePrint
AP = :__awesome_print__
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct ]
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct, :method ]
def initialize(options = {})
@options = {
@ -29,7 +29,9 @@ class AwesomePrint
:string => :yellowish,
:symbol => :cyanish,
:time => :greenish,
:trueclass => :green
:trueclass => :green,
:method => :purple,
:args => :pale
}
}
@ -137,6 +139,13 @@ class AwesomePrint
end
alias :awesome_rational :awesome_bigdecimal
# Format a method.
#------------------------------------------------------------------------------
def awesome_method(m)
name, args, owner = method_tuple(m)
"#{colorize(owner, :class)}##{colorize(name, :method)}(#{colorize(args, :args)})"
end
# Catch all method to format an arbitrary object.
#------------------------------------------------------------------------------
def awesome_self(object, appear = {})
@ -199,6 +208,16 @@ class AwesomePrint
end
end
# Return [ name, argument, owner ] tuple for a given method.
#------------------------------------------------------------------------------
def method_tuple(method)
args = method.arity.abs.times.map { |i| "arg#{i+1}" }.join(', ')
args << ', ...' if method.arity < 0
owner = $1.sub('(', ' (') if method.to_s =~ /Method: (.*?)#/
[ method.name, args, owner || 'self' ]
end
# Format hash keys as plain string regardless of underlying data type.
#------------------------------------------------------------------------------
def plain_single_line

53
spec/methods_spec.rb Normal file
View file

@ -0,0 +1,53 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Single method" do
it "plain: should handle a method with no arguments" do
method = ''.method(:upcase)
method.ai(:plain => true).should == 'String#upcase()'
end
it "color: should handle a method with no arguments" do
method = ''.method(:upcase)
method.ai.should == "\e[1;33mString\e[0m#\e[1;35mupcase\e[0m(\e[0;37m\e[0m)"
end
it "plain: should handle a method with one argument" do
method = ''.method(:match)
method.ai(:plain => true).should == 'String#match(arg1)'
end
it "color: should handle a method with one argument" do
method = ''.method(:match)
method.ai.should == "\e[1;33mString\e[0m#\e[1;35mmatch\e[0m(\e[0;37marg1\e[0m)"
end
it "plain: should handle a method with two arguments" do
method = ''.method(:tr)
method.ai(:plain => true).should == 'String#tr(arg1, arg2)'
end
it "color: should handle a method with two arguments" do
method = ''.method(:tr)
method.ai.should == "\e[1;33mString\e[0m#\e[1;35mtr\e[0m(\e[0;37marg1, arg2\e[0m)"
end
it "plain: should handle a method with multiple arguments" do
method = ''.method(:split)
method.ai(:plain => true).should == 'String#split(arg1, ...)'
end
it "color: should handle a method with multiple arguments" do
method = ''.method(:split)
method.ai.should == "\e[1;33mString\e[0m#\e[1;35msplit\e[0m(\e[0;37marg1, ...\e[0m)"
end
it "plain: should handle a method defined in mixin" do
method = ''.method(:grep)
method.ai(:plain => true).should == 'String (Enumerable)#grep(arg1)'
end
it "color: should handle a method defined in mixin" do
method = ''.method(:grep)
method.ai.should == "\e[1;33mString (Enumerable)\e[0m#\e[1;35mgrep\e[0m(\e[0;37marg1\e[0m)"
end
end