Class: AwesomePrint::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_print/formatter.rb

Constant Summary collapse

CORE =
[ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct, :method, :unboundmethod ]
DEFAULT_LIMIT_SIZE =
7

Instance Method Summary collapse

Constructor Details

#initialize(inspector) ⇒ Formatter

Returns a new instance of Formatter.



15
16
17
18
19
# File 'lib/awesome_print/formatter.rb', line 15

def initialize(inspector)
  @inspector   = inspector
  @options     = inspector.options
  @indentation = @options[:indent].abs
end

Instance Method Details

#cast(object, type) ⇒ Object

Hook this when adding custom formatters.




35
36
37
# File 'lib/awesome_print/formatter.rb', line 35

def cast(object, type)
  CORE.grep(type)[0] || :self
end

#colorize(s, type) ⇒ Object

Pick the color and apply it to the given string as necessary.




41
42
43
44
45
46
47
48
# File 'lib/awesome_print/formatter.rb', line 41

def colorize(s, type)
  s = CGI.escapeHTML(s) if @options[:html]
  if @options[:plain] || !@options[:color][type] || !@inspector.colorize?
    s
  else
    s.send(@options[:color][type], @options[:html])
  end
end

#format(object, type = nil) ⇒ Object

Main entry point to format an object.




23
24
25
26
27
28
29
30
31
# File 'lib/awesome_print/formatter.rb', line 23

def format(object, type = nil)
  core_class = cast(object, type)
  awesome = if core_class != :self
    send(:"awesome_#{core_class}", object) # Core formatters.
  else
    awesome_self(object, type) # Catch all that falls back on object.inspect.
  end
  @options[:html] ? "<pre>#{awesome}</pre>" : awesome
end