class AmazingPrint::Inspector

Constants

AP

Attributes

indentator[RW]
options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/amazing_print/inspector.rb, line 28
def initialize(options = {})
  @options = {
    indent: 4, # Number of spaces for indenting.
    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 instance variables.
    sort_keys: false,  # Do not sort hash keys.
    sort_vars: true,   # Sort instance variables.
    limit: false, # Limit arrays & hashes. Accepts bool or int.
    ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
    class_name: :class, # Method used to get Instance class name.
    object_id: true, # Show object_id.
    color: {
      args: :whiteish,
      array: :white,
      bigdecimal: :blue,
      class: :yellow,
      date: :greenish,
      falseclass: :red,
      fixnum: :blue,
      integer: :blue,
      float: :blue,
      hash: :whiteish,
      keyword: :cyan,
      method: :purpleish,
      nilclass: :red,
      rational: :blue,
      string: :yellowish,
      struct: :whiteish,
      symbol: :cyanish,
      time: :greenish,
      trueclass: :green,
      variable: :cyanish
    }
  }

  # Merge custom defaults and let explicit options parameter override them.
  merge_custom_defaults!
  merge_options!(options)

  @formatter = AmazingPrint::Formatter.new(self)
  @indentator = AmazingPrint::Indentator.new(@options[:indent].abs)
  Thread.current[AP] ||= []
end
reload_dotfile() click to toggle source

Unload the cached dotfile and load it again.

# File lib/amazing_print/inspector.rb, line 22
def self.reload_dotfile
  @@dotfile = nil
  new.send :load_dotfile
  true
end

Public Instance Methods

awesome(object) click to toggle source

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

# File lib/amazing_print/inspector.rb, line 85
def awesome(object)
  if Thread.current[AP].include?(object.object_id)
    nested(object)
  else
    begin
      Thread.current[AP] << object.object_id
      unnested(object)
    ensure
      Thread.current[AP].pop
    end
  end
end
colorize?() click to toggle source

Return true if we are to colorize the output.

# File lib/amazing_print/inspector.rb, line 100
def colorize?
  AmazingPrint.force_colors ||= false
  AmazingPrint.force_colors || (
    if defined? @colorize_stdout
      @colorize_stdout
    else
      @colorize_stdout = $stdout.tty? && (
        (
          ENV['TERM'] &&
          ENV['TERM'] != 'dumb'
        ) ||
        ENV['ANSICON']
      )
    end
  )
end
current_indentation() click to toggle source
# File lib/amazing_print/inspector.rb, line 75
def current_indentation
  indentator.indentation
end
increase_indentation(&blk) click to toggle source
# File lib/amazing_print/inspector.rb, line 79
def increase_indentation(&blk)
  indentator.indent(&blk)
end

Private Instance Methods

dotfile_readable?(dotfile) click to toggle source
# File lib/amazing_print/inspector.rb, line 182
def dotfile_readable?(dotfile)
  @@dotfile_readable = File.readable?(@@dotfile = dotfile) if @@dotfile_readable.nil? || @@dotfile != dotfile
  @@dotfile_readable
end
find_dotfile() click to toggle source
# File lib/amazing_print/inspector.rb, line 162
def find_dotfile
  xdg_config_home = File.expand_path(ENV.fetch('XDG_CONFIG_HOME', '~/.config'))
  xdg_config_path = File.join(xdg_config_home, 'aprc') # ${XDG_CONFIG_HOME}/aprc

  return xdg_config_path if File.exist?(xdg_config_path)

  # default to ~/.aprc
  File.join(Dir.home, '.aprc')
end
load_dotfile() click to toggle source

This method needs to be mocked during testing so that it always loads predictable values

# File lib/amazing_print/inspector.rb, line 175
def load_dotfile
  return if @@dotfile # Load the dotfile only once.

  dotfile = find_dotfile
  load dotfile if dotfile_readable?(dotfile)
end
merge_custom_defaults!() click to toggle source

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

# File lib/amazing_print/inspector.rb, line 190
def merge_custom_defaults!
  load_dotfile
  merge_options!(AmazingPrint.defaults) if AmazingPrint.defaults.is_a?(Hash)
rescue StandardError => e
  warn "Could not load '.aprc' from ENV['HOME']: #{e}"
end
merge_options!(options = {}) click to toggle source

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

# File lib/amazing_print/inspector.rb, line 157
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/amazing_print/inspector.rb, line 125
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/amazing_print/inspector.rb, line 143
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/amazing_print/inspector.rb, line 135
def unnested(object)
  @formatter.format(object, printable(object))
end