From 14e4fd5e7dfa3e2f9934cec55191d31f874338dd Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Sat, 30 Oct 2010 13:50:48 -0700 Subject: [PATCH] Added method formatting --- lib/ap/awesome_print.rb | 23 ++++++++++++++++-- spec/methods_spec.rb | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 spec/methods_spec.rb diff --git a/lib/ap/awesome_print.rb b/lib/ap/awesome_print.rb index a7d67b6..32f40ad 100755 --- a/lib/ap/awesome_print.rb +++ b/lib/ap/awesome_print.rb @@ -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 diff --git a/spec/methods_spec.rb b/spec/methods_spec.rb new file mode 100644 index 0000000..307c722 --- /dev/null +++ b/spec/methods_spec.rb @@ -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