class AwesomePrintLite::Inspector

Constants

AP

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/awesome_print_lite/inspector.rb, line 56
def initialize(options = {})
  @options = { 
    :indent     => 4,      # Indent using 4 spaces.
    :index      => true,   # Display array indices.
    :html       => false,  # Use ANSI color codes rather than HTML.
    :multiline  => true,   # Display in multiple lines.
    :plain      => false,  # Use colors.
    :raw        => false,  # Do not recursively format object instance variables.
    :sort_keys  => false,  # Do not sort hash keys.
    :limit      => false,  # Limit large output for arrays and hashes. Set to a boolean or integer.
    :color => { 
      :args       => :pale,
      :array      => :white,
      :bigdecimal => :blue,
      :class      => :yellow,
      :date       => :greenish,
      :falseclass => :red,
      :fixnum     => :blue,
      :float      => :blue,
      :hash       => :pale,
      :keyword    => :cyan,
      :method     => :purpleish,
      :nilclass   => :red,
      :rational   => :blue,
      :string     => :yellowish,
      :struct     => :pale,
      :symbol     => :cyanish,
      :time       => :greenish,
      :trueclass  => :green,
      :variable   => :cyanish
    }
  }

  # Merge custom defaults and let explicit options parameter override them.
  if RUBY_ENGINE != 'opal'
    merge_custom_defaults!
  end
  merge_options!(options)

  @formatter = AwesomePrintLite::Formatter.new(self)
  @ap ||= []
end

Public Instance Methods

awesome(object) click to toggle source

Dispatcher that detects data nesting and invokes object-aware formatter.

# File lib/awesome_print_lite/inspector.rb, line 101
def awesome(object)
  if @ap.include?(object.object_id)
    nested(object)
  else
    begin
      @ap << object.object_id
      unnested(object)
    ensure
      @ap.pop
    end
  end
end
colorize?() click to toggle source

Return true if we are to colorize the output.

# File lib/awesome_print_lite/inspector.rb, line 116
def colorize?
  AwesomePrintLite.force_colors ||= false
  AwesomePrintLite.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
end

Private Instance Methods

merge_custom_defaults!() click to toggle source

Load ~/.aprc file with custom defaults that override default options.

# File lib/awesome_print_lite/inspector.rb, line 166
def merge_custom_defaults!
  dotfile = File.join(ENV["HOME"], ".aprc")
  load dotfile if File.readable?(dotfile)
  merge_options!(AwesomePrintLite.defaults) if AwesomePrintLite.defaults.is_a?(Hash)
rescue => e
  $stderr.puts "Could not load #{dotfile}: #{e}"
end
merge_options!(options = {}) click to toggle source

Update @options by first merging the :color hash and then the remaining keys.

# File lib/awesome_print_lite/inspector.rb, line 159
def merge_options!(options = {})
  @options[:color].merge!(options.delete(:color) || {})
  @options.merge!(options)
end
nested(object) click to toggle source

Format nested data, for example:

arr = [1, 2]; arr << arr
=> [1,2, [...]]
hash = { :a => 1 }; hash[:b] = hash
=> { :a => 1, :b => {...} }
# File lib/awesome_print_lite/inspector.rb, line 129
def nested(object)
  case printable(object)
  when :array  then @formatter.colorize("[...]", :array)
  when :hash   then @formatter.colorize("{...}", :hash)
  when :struct then @formatter.colorize("{...}", :struct)
  else @formatter.colorize("...#{object.class}...", :class)
  end
end
printable(object) click to toggle source

Turn class name into symbol, ex: Hello::World => :hello_world. Classes that inherit from Array, Hash, File, Dir, and Struct are treated as the base class.

# File lib/awesome_print_lite/inspector.rb, line 146
def printable(object)
  case object
  when Array  then :array
  when Hash   then :hash
  when File   then :file
  when Dir    then :dir
  when Struct then :struct
  else object.class.to_s.gsub(/:+/, "_").downcase.to_sym
  end
end
unnested(object) click to toggle source
# File lib/awesome_print_lite/inspector.rb, line 139
def unnested(object)
  @formatter.format(object, printable(object))
end