Class: AwesomePrint::Inspector

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

Constant Summary collapse

AP =
:__awesome_print__

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Inspector

Returns a new instance of Inspector.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/awesome_print/inspector.rb', line 14

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.
    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.
  merge_custom_defaults!
  merge_options!(options)

  @formatter = AwesomePrint::Formatter.new(self)
  @indentator = AwesomePrint::Indentator.new(@options[:indent].abs)
  Thread.current[AP] ||= []
end

Instance Attribute Details

#indentatorObject

Returns the value of attribute indentator.



10
11
12
# File 'lib/awesome_print/inspector.rb', line 10

def indentator
  @indentator
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/awesome_print/inspector.rb', line 10

def options
  @options
end

Instance Method Details

#awesome(object) ⇒ Object

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




68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/awesome_print/inspector.rb', line 68

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?Boolean

Return true if we are to colorize the output.


Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/awesome_print/inspector.rb', line 83

def colorize?
  AwesomePrint.force_colors ||= false
  AwesomePrint.force_colors || (
    STDOUT.tty? && (
      (
        ENV['TERM'] &&
        ENV['TERM'] != 'dumb'
      ) ||
      ENV['ANSICON']
    )
  )
end

#current_indentationObject



58
59
60
# File 'lib/awesome_print/inspector.rb', line 58

def current_indentation
  indentator.indentation
end

#increase_indentationObject



62
63
64
# File 'lib/awesome_print/inspector.rb', line 62

def increase_indentation
  indentator.indent(&Proc.new)
end