Class: AwesomePrint::Formatter

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colorize

#colorize

Constructor Details

#initialize(inspector) ⇒ Formatter

Returns a new instance of Formatter.



24
25
26
27
# File 'lib/awesome_print/formatter.rb', line 24

def initialize(inspector)
  @inspector   = inspector
  @options     = inspector.options
end

Instance Attribute Details

#inspectorObject (readonly)

Returns the value of attribute inspector.



20
21
22
# File 'lib/awesome_print/formatter.rb', line 20

def inspector
  @inspector
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/awesome_print/formatter.rb', line 20

def options
  @options
end

Instance Method Details

#cast(object, type) ⇒ Object

Hook this when adding custom formatters. Check out lib/awesome_print/ext directory for custom formatters that ship with awesome_print.




44
45
46
# File 'lib/awesome_print/formatter.rb', line 44

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

#format(object, type = nil) ⇒ Object

Main entry point to format an object.




31
32
33
34
35
36
37
38
39
# File 'lib/awesome_print/formatter.rb', line 31

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 to object.inspect.
  end
  awesome
end