Parent

Files

AwesomePrint

Constants

AP
CORE

Public Class Methods

new(options = {}) click to toggle source
    # File lib/ap/awesome_print.rb, line 12
12:   def initialize(options = {})
13:     @options = { 
14:       :multiline => true,
15:       :plain     => false,
16:       :indent    => 4,
17:       :color     => { 
18:         :array      => :white,
19:         :bigdecimal => :blue,
20:         :class      => :yellow,
21:         :date       => :greenish,
22:         :falseclass => :red,
23:         :fixnum     => :blue,
24:         :float      => :blue,
25:         :hash       => :pale,
26:         :struct     => :pale,
27:         :nilclass   => :red,
28:         :string     => :yellowish,
29:         :symbol     => :cyanish,
30:         :time       => :greenish,
31:         :trueclass  => :green
32:       }
33:     }
34: 
35:     # Merge custom defaults and let explicit options parameter override them.
36:     merge_custom_defaults!
37:     merge_options!(options)
38: 
39:     @indentation = @options[:indent].abs
40:     Thread.current[AP] ||= []
41:   end

Private Class Methods

defaults() click to toggle source
 Class accessors for custom defaults.

     # File lib/ap/awesome_print.rb, line 244
244:   def self.defaults
245:     @@defaults ||= {}
246:   end
defaults=(args = {}) click to toggle source
     # File lib/ap/awesome_print.rb, line 248
248:   def self.defaults=(args = {})
249:     @@defaults = args
250:   end

Private Instance Methods

awesome(object) click to toggle source
 Dispatcher that detects data nesting and invokes object-aware formatter.

     # File lib/ap/awesome_print.rb, line 143
143:   def awesome(object)
144:     if Thread.current[AP].include?(object.object_id)
145:       nested(object)
146:     else
147:       begin
148:         Thread.current[AP] << object.object_id
149:         send(:"awesome_#{printable(object)}", object)
150:       ensure
151:         Thread.current[AP].pop
152:       end
153:     end
154:   end
awesome_array(a) click to toggle source
 Format an array.

    # File lib/ap/awesome_print.rb, line 47
47:   def awesome_array(a)
48:     return "[]" if a == []
49: 
50:     if @options[:multiline]
51:       width = (a.size - 1).to_s.size 
52:       data = a.inject([]) do |arr, item|
53:         index = colorize("#{indent}[#{arr.size.to_s.rjust(width)}] ", :array)
54:         indented do
55:           arr << (index << awesome(item))
56:         end
57:       end
58:       "[\n" << data.join(",\n") << "\n#{outdent}]"
59:     else
60:       data = a.inject([]) { |arr, item| arr << awesome(item) }
61:       "[ #{data.join(', ')} ]"
62:     end
63:   end
awesome_bigdecimal(n) click to toggle source
 Format BigDecimal and Rational objects by convering them to Float.

     # File lib/ap/awesome_print.rb, line 130
130:   def awesome_bigdecimal(n)
131:     awesome_self(n.to_f, :as => :bigdecimal)
132:   end
Also aliased as: awesome_rational
awesome_class(c) click to toggle source
 Format Class object.

     # File lib/ap/awesome_print.rb, line 106
106:   def awesome_class(c)
107:     if superclass = c.superclass # <-- Assign and test if nil.
108:       awesome_self(c, :with => " < #{superclass}")
109:     else
110:       awesome_self(c)
111:     end
112:   end
awesome_dir(d) click to toggle source
 Format Dir object.

     # File lib/ap/awesome_print.rb, line 123
123:   def awesome_dir(d)
124:     ls = `ls -alF #{d.path.shellescape}`
125:     awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}")
126:   end
awesome_file(f) click to toggle source
 Format File object.

     # File lib/ap/awesome_print.rb, line 116
116:   def awesome_file(f)
117:     ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}`
118:     awesome_self(f, :with => ls.empty? ? nil : "\n#{ls.chop}")
119:   end
awesome_hash(h) click to toggle source
 Format a hash. If @options[:indent] if negative left align hash keys.

    # File lib/ap/awesome_print.rb, line 67
67:   def awesome_hash(h)
68:     return "{}" if h == {}
69: 
70:     data = h.keys.inject([]) do |arr, key|
71:       plain_single_line do
72:         arr << [ awesome(key), h[key] ]
73:       end
74:     end
75:       
76:     width = data.map { |key, | key.size }.max || 0
77:     width += @indentation if @options[:indent] > 0
78:   
79:     data = data.inject([]) do |arr, (key, value)|
80:       if @options[:multiline]
81:         formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width))
82:       else
83:         formatted_key = key
84:       end
85:       indented do
86:         arr << (formatted_key << colorize(" => ", :hash) << awesome(value))
87:       end
88:     end
89:     if @options[:multiline]
90:       "{\n" << data.join(",\n") << "\n#{outdent}}"
91:     else
92:       "{ #{data.join(', ')} }"
93:     end
94:   end
awesome_rational(n) click to toggle source
Alias for: awesome_bigdecimal
awesome_self(object, appear = {}) click to toggle source
 Catch all method to format an arbitrary object.

     # File lib/ap/awesome_print.rb, line 137
137:   def awesome_self(object, appear = {})
138:     colorize(object.inspect << appear[:with].to_s, appear[:as] || declassify(object))
139:   end
awesome_struct(s) click to toggle source
 Format a Struct. If @options[:indent] if negative left align hash keys.

     # File lib/ap/awesome_print.rb, line 98
 98:   def awesome_struct(s)
 99:     h = {}
100:     s.each_pair { |k,v| h[k] = v }
101:     awesome_hash(h)
102:   end
colorize(s, type) click to toggle source
 Pick the color and apply it to the given string as necessary.

     # File lib/ap/awesome_print.rb, line 189
189:   def colorize(s, type)
190:     if @options[:plain] || @options[:color][type].nil?
191:       s
192:     else
193:       s.send(@options[:color][type])
194:     end
195:   end
declassify(object) click to toggle source
 Turn class name into symbol, ex: Hello::World => :hello_world.

     # File lib/ap/awesome_print.rb, line 179
179:   def declassify(object)
180:     if object.is_a?(Struct)
181:       :struct
182:     else
183:       object.class.to_s.gsub(/:+/, "_").downcase.to_sym
184:     end
185:   end
indent() click to toggle source
     # File lib/ap/awesome_print.rb, line 215
215:   def indent
216:     @indent = ' ' * @indentation
217:   end
indented() click to toggle source

     # File lib/ap/awesome_print.rb, line 208
208:   def indented
209:     @indentation += @options[:indent].abs
210:     yield
211:   ensure
212:     @indentation -= @options[:indent].abs
213:   end
merge_custom_defaults!() click to toggle source
 Load ~/.aprc file with custom defaults that override default options.

     # File lib/ap/awesome_print.rb, line 232
232:   def merge_custom_defaults!
233:     dotfile = File.join(ENV["HOME"], ".aprc")
234:     if File.readable?(dotfile)
235:       load dotfile
236:       merge_options!(self.class.defaults)
237:     end
238:   rescue => e
239:     $stderr.puts "Could not load #{dotfile}: #{e}"
240:   end
merge_options!(options = {}) click to toggle source
 Update @options by first merging the :color hash and then the remaining keys.

     # File lib/ap/awesome_print.rb, line 225
225:   def merge_options!(options = {})
226:     @options[:color].merge!(options.delete(:color) || {})
227:     @options.merge!(options)
228:   end
nested(object) click to toggle source
 Format nested data, for example:
   arr = [1, 2]; arr << arr
   => [1,2, [...]]
   hsh = { :a => 1 }; hsh[:b] = hsh
   => { :a => 1, :b => {...} }

     # File lib/ap/awesome_print.rb, line 162
162:   def nested(object)
163:     case printable(object)
164:       when :array  then colorize("[...]", :array)
165:       when :hash   then colorize("{...}", :hash)
166:       when :struct then colorize("{...}", :struct)
167:       else colorize("...#{object.class}...", :class)
168:     end
169:   end
outdent() click to toggle source
     # File lib/ap/awesome_print.rb, line 219
219:   def outdent
220:     @outdent = ' ' * (@indentation - @options[:indent].abs)
221:   end
plain_single_line() click to toggle source
 Format hash keys as plain string regardless of underlying data type.

     # File lib/ap/awesome_print.rb, line 199
199:   def plain_single_line
200:     plain, multiline = @options[:plain], @options[:multiline]
201:     @options[:plain], @options[:multiline] = true, false
202:     yield
203:   ensure
204:     @options[:plain], @options[:multiline] = plain, multiline
205:   end
printable(object) click to toggle source
 Return one of the "core" types that have a formatter of :self otherwise.

     # File lib/ap/awesome_print.rb, line 173
173:   def printable(object)
174:     CORE.grep(declassify(object))[0] || :self
175:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.