Class: RASN2::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/rasn2/tracer.rb

Constant Summary collapse

TRACED_CLASSES =
[Types::Any, Types::Choice, Types::Sequence, Types::SequenceOf, Types::Base].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, color: false) ⇒ Tracer

Returns a new instance of Tracer.

Parameters:

  • io (IO)
  • color (Boolean) (defaults to: false)

    enable colorized output using pastel gem



29
30
31
32
33
34
# File 'lib/rasn2/tracer.rb', line 29

def initialize(io, color: false)
  @io = io
  @tracing_level = 0
  @rainbow = Rainbow.new
  @rainbow.enabled = color
end

Instance Attribute Details

#ioIO (readonly)

Returns:

  • (IO)


9
10
11
# File 'lib/rasn2/tracer.rb', line 9

def io
  @io
end

#tracing_levelInteger

Returns:

  • (Integer)


11
12
13
# File 'lib/rasn2/tracer.rb', line 11

def tracing_level
  @tracing_level
end

Instance Method Details

#colorObject



13
14
15
# File 'lib/rasn2/tracer.rb', line 13

def color
  @rainbow.enabled
end

#color=(enabled) ⇒ Object



17
18
19
# File 'lib/rasn2/tracer.rb', line 17

def color=(enabled)
  @rainbow.enabled = enabled
end

#colorize(str) ⇒ Object



21
22
23
# File 'lib/rasn2/tracer.rb', line 21

def colorize(str)
  @rainbow ? @rainbow.wrap(str) : Rainbow.new.wrap(str)
end

#indent(level = nil) ⇒ String

Return identation for given level. If nil, use #tracing_level.

Parameters:

  • level (Integer, nil) (defaults to: nil)

Returns:

  • (String)


47
48
49
50
# File 'lib/rasn2/tracer.rb', line 47

def indent(level=nil)
  level ||= @tracing_level
  "  " * level
end

#trace(msg) ⇒ void

This method returns an undefined value.

Puts msg onto #io.

Parameters:

  • msg (String)


39
40
41
42
# File 'lib/rasn2/tracer.rb', line 39

def trace(msg)
  indented = msg.split("\n").map { |line| indent + line }.join("\n")
  @io.puts(indented)
end