awesome_print 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.2.1
2
+ - ap can now be used within Rails templates (ex. <%= ap object %>)
3
+ - Added support for printing Struct
4
+
1
5
  0.2.0
2
6
  - Added support for logger.ap (including Rails logger)
3
7
  - Added support for HashWithIndifferentAccess from ActiveSupport
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## Awesome Print ##
2
2
  Awesome Print is Ruby library that pretty prints Ruby objects in full color
3
3
  exposing their internal structure with proper indentation. Rails ActiveRecord
4
- objects are supported via included mixin.
4
+ objects and usage within Rails templates are supported via included mixins.
5
5
 
6
6
  ### Installation ###
7
7
  # Installing as Ruby gem
@@ -170,6 +170,12 @@ in the custom defaults (see below), or you can override on a per call basis with
170
170
 
171
171
  logger.ap object, :warn
172
172
 
173
+ ### ActionView Convenience Method ###
174
+ awesome_print adds an ap method to the ActionView::Base class making it available
175
+ within Rails templates. For example:
176
+
177
+ <%= ap @accounts.first %>
178
+
173
179
  ### Setting Custom Defaults ###
174
180
  You can set your own default options by creating ``.aprc`` file in your home
175
181
  directory. Within that file assign your defaults to ``AwesomePrint.defaults``.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/lib/ap.rb CHANGED
@@ -9,6 +9,7 @@ require File.dirname(__FILE__) + "/ap/awesome_print"
9
9
 
10
10
  require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(::Logger) or defined?(::ActiveSupport::BufferedLogger)
11
11
 
12
+ require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(::ActionView)
12
13
  require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
13
14
  require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)
14
15
 
@@ -7,7 +7,7 @@ require "shellwords"
7
7
 
8
8
  class AwesomePrint
9
9
  AP = :__awesome_print__
10
- CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational ]
10
+ CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct ]
11
11
 
12
12
  def initialize(options = {})
13
13
  @options = {
@@ -23,6 +23,7 @@ class AwesomePrint
23
23
  :fixnum => :blue,
24
24
  :float => :blue,
25
25
  :hash => :pale,
26
+ :struct => :pale,
26
27
  :nilclass => :red,
27
28
  :string => :yellowish,
28
29
  :symbol => :cyanish,
@@ -92,6 +93,14 @@ class AwesomePrint
92
93
  end
93
94
  end
94
95
 
96
+ # Format a Struct. If @options[:indent] if negative left align hash keys.
97
+ #------------------------------------------------------------------------------
98
+ def awesome_struct(s)
99
+ h = {}
100
+ s.each_pair { |k,v| h[k] = v }
101
+ awesome_hash(h)
102
+ end
103
+
95
104
  # Format Class object.
96
105
  #------------------------------------------------------------------------------
97
106
  def awesome_class(c)
@@ -152,8 +161,9 @@ class AwesomePrint
152
161
  #------------------------------------------------------------------------------
153
162
  def nested(object)
154
163
  case printable(object)
155
- when :array then colorize("[...]", :array)
156
- when :hash then colorize("{...}", :hash)
164
+ when :array then colorize("[...]", :array)
165
+ when :hash then colorize("{...}", :hash)
166
+ when :struct then colorize("{...}", :struct)
157
167
  else colorize("...#{object.class}...", :class)
158
168
  end
159
169
  end
@@ -167,7 +177,11 @@ class AwesomePrint
167
177
  # Turn class name into symbol, ex: Hello::World => :hello_world.
168
178
  #------------------------------------------------------------------------------
169
179
  def declassify(object)
170
- object.class.to_s.gsub(/:+/, "_").downcase.to_sym
180
+ if object.is_a?(Struct)
181
+ :struct
182
+ else
183
+ object.class.to_s.gsub(/:+/, "_").downcase.to_sym
184
+ end
171
185
  end
172
186
 
173
187
  # Pick the color and apply it to the given string as necessary.
@@ -5,6 +5,12 @@
5
5
  #------------------------------------------------------------------------------
6
6
  class String
7
7
 
8
+ # ANSI color codes:
9
+ # \033 => escape
10
+ # 30 => color base
11
+ # 1 => bright
12
+ # 0 => normal
13
+
8
14
  [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
9
15
  if STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb'
10
16
  define_method color do "\033[1;#{30+i}m#{self}\033[0m" end
@@ -0,0 +1,38 @@
1
+ # Copyright (c) 2010 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ module AwesomePrintActionView
7
+
8
+ def self.included(base)
9
+ unless base.const_defined?(:AP_ANSI_TO_HTML)
10
+ hash = {} # Build ANSI => HTML color map.
11
+ [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
12
+ hash["\033[1;#{30+i}m"] = color
13
+ end
14
+ [ :black, :darkred, :darkgreen, :brown, :navy, :darkmagenta, :darkcyan, :slategray ].each_with_index do |color, i|
15
+ hash["\033[0;#{30+i}m"] = color
16
+ end
17
+ base.const_set(:AP_ANSI_TO_HTML, hash.freeze)
18
+ end
19
+ end
20
+
21
+ def ap_debug(object, options = {})
22
+ formatted = h(object.ai(options))
23
+
24
+ unless options[:plain]
25
+ self.class::AP_ANSI_TO_HTML.each do |key, value|
26
+ formatted.gsub!(key, %Q|<font color="#{value}">|)
27
+ end
28
+ formatted.gsub!("\033[0m", "</font>")
29
+ end
30
+
31
+ content_tag(:pre, formatted, :class => "debug_dump")
32
+ end
33
+
34
+ alias_method :ap, :ap_debug
35
+
36
+ end
37
+
38
+ ActionView::Base.send(:include, AwesomePrintActionView) if defined?(::ActionView)
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'action_view'
4
+ require 'ap/mixin/action_view'
5
+
6
+ describe "AwesomePrint ActionView extensions" do
7
+ before(:each) do
8
+ @view = ActionView::Base.new
9
+ end
10
+
11
+ it "should wrap ap output with <pre> tag" do
12
+ obj = 42
13
+ @view.ap(obj, :plain => true).should == '<pre class="debug_dump">42</pre>'
14
+ end
15
+
16
+ it "should encode HTML entities" do
17
+ obj = " &<hello>"
18
+ @view.ap(obj, :plain => true).should == '<pre class="debug_dump">&quot; &amp;&lt;hello&gt;&quot;</pre>'
19
+ end
20
+
21
+ it "should convert primary ANSI colors to HTML" do
22
+ obj = 42
23
+ [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each do |color|
24
+ @view.ap(obj, :color => { :fixnum => color }).should == %Q|<pre class="debug_dump"><font color="#{color}">42</font></pre>|
25
+ end
26
+ end
27
+
28
+ it "should convert mixed ANSI colors to HTML" do
29
+ obj = 42
30
+ [ :grayish, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :whiteish, :black, :pale ].zip(
31
+ [ :black, :darkred, :darkgreen, :brown, :navy, :darkmagenta, :darkcyan, :slategray, :black, :slategray ]) do |ansi, html|
32
+ @view.ap(obj, :color => { :fixnum => ansi }).should == %Q|<pre class="debug_dump"><font color="#{html}">42</font></pre>|
33
+ end
34
+ end
35
+ end
@@ -164,11 +164,11 @@ EOS
164
164
  it "colored multiline (default)" do
165
165
  @hash.ai.should == <<-EOS.strip
166
166
  {
167
- 1\e[1;30m => \e[0m{
168
- :sym\e[1;30m => \e[0m{
169
- \"str\"\e[1;30m => \e[0m{
170
- [ 1, 2, 3 ]\e[1;30m => \e[0m{
171
- { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m
167
+ 1\e[0;37m => \e[0m{
168
+ :sym\e[0;37m => \e[0m{
169
+ \"str\"\e[0;37m => \e[0m{
170
+ [ 1, 2, 3 ]\e[0;37m => \e[0m{
171
+ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
172
172
  }
173
173
  }
174
174
  }
@@ -180,11 +180,11 @@ EOS
180
180
  it "colored multiline indented" do
181
181
  @hash.ai(:indent => 2).should == <<-EOS.strip
182
182
  {
183
- 1\e[1;30m => \e[0m{
184
- :sym\e[1;30m => \e[0m{
185
- \"str\"\e[1;30m => \e[0m{
186
- [ 1, 2, 3 ]\e[1;30m => \e[0m{
187
- { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m
183
+ 1\e[0;37m => \e[0m{
184
+ :sym\e[0;37m => \e[0m{
185
+ \"str\"\e[0;37m => \e[0m{
186
+ [ 1, 2, 3 ]\e[0;37m => \e[0m{
187
+ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
188
188
  }
189
189
  }
190
190
  }
@@ -194,7 +194,7 @@ EOS
194
194
  end
195
195
 
196
196
  it "colored single line" do
197
- @hash.ai(:multiline => false).should == "{ 1\e[1;30m => \e[0m{ :sym\e[1;30m => \e[0m{ \"str\"\e[1;30m => \e[0m{ [ 1, 2, 3 ]\e[1;30m => \e[0m{ { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m } } } } }"
197
+ @hash.ai(:multiline => false).should == "{ 1\e[0;37m => \e[0m{ :sym\e[0;37m => \e[0m{ \"str\"\e[0;37m => \e[0m{ [ 1, 2, 3 ]\e[0;37m => \e[0m{ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }"
198
198
  end
199
199
 
200
200
  end
@@ -288,4 +288,77 @@ EOS
288
288
  end
289
289
  end
290
290
 
291
+
292
+ #------------------------------------------------------------------------------
293
+ describe "Struct" do
294
+ before(:each) do
295
+ @struct = unless defined?(Struct::SimpleStruct)
296
+ Struct.new("SimpleStruct", :name, :address).new
297
+ else
298
+ Struct::SimpleStruct.new
299
+ end
300
+ @struct.name = "Herman Munster"
301
+ @struct.address = "1313 Mockingbird Lane"
302
+ end
303
+
304
+ it "empty struct" do
305
+ Struct.new("EmptyStruct").ai.should == "\e[1;33mStruct::EmptyStruct < Struct\e[0m"
306
+ end
307
+
308
+ it "plain multiline" do
309
+ s1 = <<-EOS.strip
310
+ {
311
+ :address => "1313 Mockingbird Lane",
312
+ :name => "Herman Munster"
313
+ }
314
+ EOS
315
+ s2 = <<-EOS.strip
316
+ {
317
+ :name => "Herman Munster",
318
+ :address => "1313 Mockingbird Lane"
319
+ }
320
+ EOS
321
+ @struct.ai(:plain => true).should satisfy { |match| match == s1 || match == s2 }
322
+ end
323
+
324
+ it "plain multiline indented" do
325
+ s1 = <<-EOS.strip
326
+ {
327
+ :address => "1313 Mockingbird Lane",
328
+ :name => "Herman Munster"
329
+ }
330
+ EOS
331
+ s2 = <<-EOS.strip
332
+ {
333
+ :name => "Herman Munster",
334
+ :address => "1313 Mockingbird Lane"
335
+ }
336
+ EOS
337
+ @struct.ai(:plain => true, :indent => 1).should satisfy { |match| match == s1 || match == s2 }
338
+ end
339
+
340
+ it "plain single line" do
341
+ s1 = "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
342
+ s2 = "{ :name => \"Herman Munster\", :address => \"1313 Mockingbird Lane\" }"
343
+ @struct.ai(:plain => true, :multiline => false).should satisfy { |match| match == s1 || match == s2 }
344
+ end
345
+
346
+ it "colored multiline (default)" do
347
+ s1 = <<-EOS.strip
348
+ {
349
+ :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
350
+ :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
351
+ }
352
+ EOS
353
+ s2 = <<-EOS.strip
354
+ {
355
+ :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m,
356
+ :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
357
+ }
358
+ EOS
359
+ @struct.ai.should satisfy { |match| match == s1 || match == s2 }
360
+ end
361
+ end
362
+
363
+
291
364
  end
@@ -14,5 +14,7 @@ describe "String extensions" do
14
14
  it "should have black and pale colors" do
15
15
  "black".send(:black).should == "black".send(:grayish)
16
16
  "pale".send(:pale).should == "pale".send(:whiteish)
17
+ "pale".send(:pale).should == "\e[0;37mpale\e[0m"
18
+ "whiteish".send(:whiteish).should == "\e[0;37mwhiteish\e[0m"
17
19
  end
18
20
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Dvorkin
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-05 00:00:00 -07:00
17
+ date: 2010-06-03 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -52,9 +52,11 @@ files:
52
52
  - lib/ap/core_ext/kernel.rb
53
53
  - lib/ap/core_ext/logger.rb
54
54
  - lib/ap/core_ext/string.rb
55
+ - lib/ap/mixin/action_view.rb
55
56
  - lib/ap/mixin/active_record.rb
56
57
  - lib/ap/mixin/active_support.rb
57
58
  - rails/init.rb
59
+ - spec/action_view_spec.rb
58
60
  - spec/active_record_spec.rb
59
61
  - spec/awesome_print_spec.rb
60
62
  - spec/logger_spec.rb
@@ -92,6 +94,7 @@ signing_key:
92
94
  specification_version: 3
93
95
  summary: Pretty print Ruby objects with proper indentation and colors.
94
96
  test_files:
97
+ - spec/action_view_spec.rb
95
98
  - spec/active_record_spec.rb
96
99
  - spec/awesome_print_spec.rb
97
100
  - spec/logger_spec.rb