2010-04-03 00:43:46 -04:00
|
|
|
## Awesome Print ##
|
2010-04-03 02:51:23 -04:00
|
|
|
Awesome Print is Ruby library that pretty prints Ruby objects in full color
|
|
|
|
exposing their internal structure with proper indentation. Rails ActiveRecord
|
2010-06-03 22:53:47 -04:00
|
|
|
objects and usage within Rails templates are supported via included mixins.
|
2010-04-03 00:43:46 -04:00
|
|
|
|
|
|
|
### Installation ###
|
2010-04-07 00:30:12 -04:00
|
|
|
# Installing as Ruby gem
|
2010-04-03 00:43:46 -04:00
|
|
|
$ gem install awesome_print
|
|
|
|
|
|
|
|
# Installing as Rails plugin
|
2010-06-04 01:04:12 -04:00
|
|
|
$ ruby script/plugin install http://github.com/michaeldv/awesome_print.git
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2010-04-07 00:30:12 -04:00
|
|
|
# Cloning the repository
|
2010-06-04 01:04:12 -04:00
|
|
|
$ git clone git://github.com/michaeldv/awesome_print.git
|
2010-04-03 00:43:46 -04:00
|
|
|
|
|
|
|
### Usage ###
|
2010-04-06 00:21:57 -04:00
|
|
|
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-04-06 00:21:57 -04:00
|
|
|
ap object, options = {}
|
|
|
|
|
|
|
|
Default options:
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2011-05-07 14:50:04 -04:00
|
|
|
:multiline => true, # Display in multiple lines.
|
|
|
|
:plain => false, # Use colors.
|
|
|
|
:indent => 4, # Indent using 4 spaces.
|
|
|
|
:index => true, # Display array indices.
|
|
|
|
:html => false, # Use ANSI color codes rather than HTML.
|
2011-02-03 01:43:55 -05:00
|
|
|
:sorted_hash_keys => false, # Do not sort hash keys.
|
2010-04-08 08:01:39 -04:00
|
|
|
:color => {
|
2010-04-06 00:21:57 -04:00
|
|
|
:array => :white,
|
|
|
|
:bignum => :blue,
|
|
|
|
:class => :yellow,
|
|
|
|
:date => :greenish,
|
|
|
|
:falseclass => :red,
|
|
|
|
:fixnum => :blue,
|
|
|
|
:float => :blue,
|
|
|
|
:hash => :gray,
|
|
|
|
:nilclass => :red,
|
|
|
|
:string => :yellowish,
|
|
|
|
:symbol => :cyanish,
|
|
|
|
:time => :greenish,
|
|
|
|
:trueclass => :green
|
|
|
|
}
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2010-04-06 00:21:57 -04:00
|
|
|
Supported color names:
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2010-04-06 00:21:57 -04:00
|
|
|
:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
|
|
|
|
:black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale
|
|
|
|
|
|
|
|
### Examples ###
|
|
|
|
$ cat > 1.rb
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-04-08 08:01:39 -04:00
|
|
|
data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
|
2010-04-06 00:21:57 -04:00
|
|
|
ap data
|
|
|
|
^D
|
|
|
|
$ ruby 1.rb
|
2010-04-03 00:43:46 -04:00
|
|
|
[
|
|
|
|
[0] false,
|
|
|
|
[1] 42,
|
|
|
|
[2] [
|
2010-04-08 08:01:39 -04:00
|
|
|
[0] "forty",
|
2010-04-03 00:43:46 -04:00
|
|
|
[1] "two"
|
|
|
|
],
|
|
|
|
[3] {
|
|
|
|
:class => Time < Object,
|
|
|
|
:now => Fri Apr 02 19:55:53 -0700 2010,
|
|
|
|
:distance => 4.2e+43
|
|
|
|
}
|
|
|
|
]
|
2010-04-06 00:21:57 -04:00
|
|
|
|
|
|
|
$ cat > 2.rb
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-04-06 00:21:57 -04:00
|
|
|
data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
|
|
|
|
ap data, :indent => -2 # <-- Left align hash keys.
|
|
|
|
^D
|
|
|
|
$ ruby 2.rb
|
2010-04-04 23:18:19 -04:00
|
|
|
{
|
|
|
|
:class => Time < Object,
|
|
|
|
:now => Fri Apr 02 19:55:53 -0700 2010,
|
|
|
|
:distance => 4.2e+43
|
|
|
|
}
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2010-04-06 00:21:57 -04:00
|
|
|
$ cat > 3.rb
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-04-08 08:01:39 -04:00
|
|
|
data = [ false, 42, %w(forty two) ]
|
2010-04-06 00:21:57 -04:00
|
|
|
data << data # <-- Nested array.
|
|
|
|
ap data, :multiline => false
|
|
|
|
^D
|
|
|
|
$ ruby 3.rb
|
2010-04-08 08:01:39 -04:00
|
|
|
[ false, 42, [ "forty", "two" ], [...] ]
|
2010-04-06 00:21:57 -04:00
|
|
|
|
2010-11-06 19:01:38 -04:00
|
|
|
$ cat > 4.rb
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-11-08 22:24:07 -05:00
|
|
|
class Hello
|
|
|
|
def self.world(x, y, z = nil, &blk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ap Hello.methods - Class.methods
|
2010-11-06 19:01:38 -04:00
|
|
|
^D
|
|
|
|
$ ruby 4.rb
|
2010-11-08 22:24:07 -05:00
|
|
|
[
|
|
|
|
[0] world(x, y, *z, &blk) Hello
|
|
|
|
]
|
|
|
|
|
|
|
|
$ cat > 5.rb
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-11-08 22:24:07 -05:00
|
|
|
ap (''.methods - Object.methods).grep(/!/)
|
|
|
|
^D
|
|
|
|
$ ruby 5.rb
|
2010-11-06 19:01:38 -04:00
|
|
|
[
|
|
|
|
[ 0] capitalize!() String
|
|
|
|
[ 1] chomp!(*arg1) String
|
|
|
|
[ 2] chop!() String
|
|
|
|
[ 3] delete!(*arg1) String
|
|
|
|
[ 4] downcase!() String
|
|
|
|
[ 5] encode!(*arg1) String
|
|
|
|
[ 6] gsub!(*arg1) String
|
|
|
|
[ 7] lstrip!() String
|
|
|
|
[ 8] next!() String
|
|
|
|
[ 9] reverse!() String
|
|
|
|
[10] rstrip!() String
|
|
|
|
[11] slice!(*arg1) String
|
|
|
|
[12] squeeze!(*arg1) String
|
|
|
|
[13] strip!() String
|
|
|
|
[14] sub!(*arg1) String
|
|
|
|
[15] succ!() String
|
|
|
|
[16] swapcase!() String
|
|
|
|
[17] tr!(arg1, arg2) String
|
|
|
|
[18] tr_s!(arg1, arg2) String
|
|
|
|
[19] upcase!() String
|
|
|
|
]
|
|
|
|
|
2011-05-02 19:33:14 -04:00
|
|
|
$ cat > 6.rb
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2011-05-07 14:50:04 -04:00
|
|
|
ap 42 == ap(42)
|
2011-05-02 19:33:14 -04:00
|
|
|
^D
|
|
|
|
$ ruby 6.rb
|
|
|
|
42
|
|
|
|
true
|
|
|
|
|
2010-04-06 00:21:57 -04:00
|
|
|
### Example (Rails console) ###
|
2011-05-13 18:10:06 -04:00
|
|
|
$ rails console
|
|
|
|
rails> require "awesome_print"
|
2010-04-03 00:43:46 -04:00
|
|
|
rails> ap Account.all(:limit => 2)
|
|
|
|
[
|
|
|
|
[0] #<Account:0x1033220b8> {
|
|
|
|
:id => 1,
|
|
|
|
:user_id => 5,
|
|
|
|
:assigned_to => 7,
|
|
|
|
:name => "Hayes-DuBuque",
|
|
|
|
:access => "Public",
|
|
|
|
:website => "http://www.hayesdubuque.com",
|
|
|
|
:toll_free_phone => "1-800-932-6571",
|
|
|
|
:phone => "(111)549-5002",
|
|
|
|
:fax => "(349)415-2266",
|
|
|
|
:deleted_at => nil,
|
|
|
|
:created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
|
|
|
|
:updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
|
|
|
|
:email => "info@hayesdubuque.com",
|
|
|
|
:background_info => nil
|
|
|
|
},
|
|
|
|
[1] #<Account:0x103321ff0> {
|
|
|
|
:id => 2,
|
|
|
|
:user_id => 4,
|
|
|
|
:assigned_to => 4,
|
|
|
|
:name => "Ziemann-Streich",
|
|
|
|
:access => "Public",
|
|
|
|
:website => "http://www.ziemannstreich.com",
|
|
|
|
:toll_free_phone => "1-800-871-0619",
|
|
|
|
:phone => "(042)056-1534",
|
|
|
|
:fax => "(106)017-8792",
|
|
|
|
:deleted_at => nil,
|
|
|
|
:created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
|
|
|
|
:updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
|
|
|
|
:email => "info@ziemannstreich.com",
|
|
|
|
:background_info => nil
|
|
|
|
}
|
|
|
|
]
|
|
|
|
rails> ap Account
|
|
|
|
class Account < ActiveRecord::Base {
|
|
|
|
:id => :integer,
|
|
|
|
:user_id => :integer,
|
|
|
|
:assigned_to => :integer,
|
|
|
|
:name => :string,
|
|
|
|
:access => :string,
|
|
|
|
:website => :string,
|
|
|
|
:toll_free_phone => :string,
|
|
|
|
:phone => :string,
|
|
|
|
:fax => :string,
|
|
|
|
:deleted_at => :datetime,
|
|
|
|
:created_at => :datetime,
|
|
|
|
:updated_at => :datetime,
|
|
|
|
:email => :string,
|
|
|
|
:background_info => :string
|
|
|
|
}
|
|
|
|
rails>
|
|
|
|
|
2010-05-06 00:30:00 -04:00
|
|
|
### IRB integration ###
|
|
|
|
To use awesome_print as default formatter in irb and Rails console add the following
|
|
|
|
lines into your ~/.irbrc file:
|
|
|
|
|
|
|
|
require "rubygems"
|
2011-05-13 18:10:06 -04:00
|
|
|
require "awesome_print"
|
2010-10-13 02:03:37 -04:00
|
|
|
|
|
|
|
unless IRB.version.include?('DietRB')
|
|
|
|
IRB::Irb.class_eval do
|
|
|
|
def output_value
|
|
|
|
ap @context.last_value
|
|
|
|
end
|
2010-05-06 00:30:00 -04:00
|
|
|
end
|
2010-10-13 02:03:37 -04:00
|
|
|
else # MacRuby
|
|
|
|
IRB.formatter = Class.new(IRB::Formatter) do
|
|
|
|
def inspect_object(object)
|
|
|
|
object.ai
|
|
|
|
end
|
|
|
|
end.new
|
2010-05-06 00:30:00 -04:00
|
|
|
end
|
|
|
|
|
2010-04-22 00:57:21 -04:00
|
|
|
### Logger Convenience Method ###
|
2011-05-07 14:50:04 -04:00
|
|
|
awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
|
|
|
|
letting you call:
|
2010-04-22 00:57:21 -04:00
|
|
|
|
|
|
|
logger.ap object
|
|
|
|
|
2011-05-07 14:50:04 -04:00
|
|
|
By default, this logs at the :debug level. You can override that globally with:
|
2010-04-22 00:57:21 -04:00
|
|
|
|
|
|
|
:log_level => :info
|
|
|
|
|
2011-05-07 14:50:04 -04:00
|
|
|
in the custom defaults (see below). You can also override on a per call basis with:
|
2010-04-22 00:57:21 -04:00
|
|
|
|
|
|
|
logger.ap object, :warn
|
|
|
|
|
2010-06-03 22:53:47 -04:00
|
|
|
### ActionView Convenience Method ###
|
2011-05-07 14:50:04 -04:00
|
|
|
awesome_print adds the 'ap' method to the ActionView::Base class making it available
|
2010-06-03 22:53:47 -04:00
|
|
|
within Rails templates. For example:
|
|
|
|
|
|
|
|
<%= ap @accounts.first %>
|
|
|
|
|
2011-05-07 14:50:04 -04:00
|
|
|
With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML
|
|
|
|
formatting:
|
|
|
|
|
|
|
|
<%= ap @accounts.first, :html => true %>
|
|
|
|
|
2010-04-08 00:31:21 -04:00
|
|
|
### Setting Custom Defaults ###
|
|
|
|
You can set your own default options by creating ``.aprc`` file in your home
|
|
|
|
directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
# ~/.aprc file.
|
|
|
|
AwesomePrint.defaults = {
|
|
|
|
:indent => -2,
|
|
|
|
:color => {
|
|
|
|
:hash => :pale,
|
|
|
|
:class => :white
|
|
|
|
}
|
|
|
|
}
|
2010-04-04 00:28:49 -04:00
|
|
|
|
2010-11-06 19:01:38 -04:00
|
|
|
### Running Specs ###
|
|
|
|
|
2011-05-06 18:20:52 -04:00
|
|
|
$ gem install rspec # RSpec 2.x is the requirement.
|
|
|
|
$ rake spec # Run the entire spec suite.
|
|
|
|
$ rspec spec/logger_spec.rb # Run individual spec file.
|
2010-11-06 19:01:38 -04:00
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
### Note on Patches/Pull Requests ###
|
2010-04-03 02:51:23 -04:00
|
|
|
* Fork the project on Github.
|
|
|
|
* Make your feature addition or bug fix.
|
|
|
|
* Add specs for it, making sure $ rake spec is all green.
|
|
|
|
* Commit, do not mess with rakefile, version, or history.
|
|
|
|
* Send me a pull request.
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2010-05-06 00:30:00 -04:00
|
|
|
### Contributors ###
|
|
|
|
|
2011-05-06 20:35:49 -04:00
|
|
|
* Andrew O'Brien -- https://github.com/AndrewO
|
2010-05-06 00:30:00 -04:00
|
|
|
* Daniel Bretoi -- http://github.com/danielb2
|
2010-11-06 19:01:38 -04:00
|
|
|
* Eloy Duran -- http://github.com/alloy
|
2011-05-06 20:35:49 -04:00
|
|
|
* Elpizo Choi -- https://github.com/fuJiin
|
2010-11-06 19:01:38 -04:00
|
|
|
* Benoit Daloze -- http://github.com/eregon
|
|
|
|
* Sean Gallagher -- http://github.com/torandu
|
2011-05-06 20:35:49 -04:00
|
|
|
* Stephan Hagemann -- https://github.com/shageman
|
2010-11-13 12:49:24 -05:00
|
|
|
* Tim Harper -- http://github.com/timcharper
|
2010-05-06 00:30:00 -04:00
|
|
|
* Tobias Crawley -- http://github.com/tobias
|
2011-05-06 20:35:49 -04:00
|
|
|
* Viktar Basharymau -- https://github.com/DNNX
|
2010-05-06 00:30:00 -04:00
|
|
|
|
2010-04-03 00:43:46 -04:00
|
|
|
### License ###
|
2011-02-03 01:48:27 -05:00
|
|
|
Copyright (c) 2010-2011 Michael Dvorkin
|
2011-05-07 14:50:04 -04:00
|
|
|
|
2010-11-13 12:49:24 -05:00
|
|
|
twitter.com/mid
|
2011-05-07 14:50:04 -04:00
|
|
|
|
2010-04-03 02:51:23 -04:00
|
|
|
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
2010-04-03 00:43:46 -04:00
|
|
|
|
2010-11-13 12:49:24 -05:00
|
|
|
Released under the MIT license. See LICENSE file for details.
|