Module: Bones::Debug

Defined in:
lib/bones/debug.rb

Overview

Helper module that will pretty print OpenStruct objects. It is used mainly for debugging the Mr Bones project open struct.

Constant Summary collapse

KEY_LENGTH =

:stopdoc:

20
VAR_LENGTH =
78 - 6 - KEY_LENGTH
SEP =
"\n" + ' '*(KEY_LENGTH+6)
FMT =
"  %-#{KEY_LENGTH}s => %s"

Class Method Summary collapse

Class Method Details

.show(ostruct, prefix = '') ⇒ Object

Print all the keys for the given ostruct to stdout. If a prefix is given, then the open struct keys will be prefixed with this string.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bones/debug.rb', line 22

def self.show( ostruct, prefix = '' )
  sio = StringIO.new

  h = ostruct.instance_variable_get(:@table)
  h.keys.map {|k| k.to_s}.sort.each do |k|
    sio.seek 0
    sio.truncate 0
    next if k =~ %r/^_/o

    val = h[k.to_sym]
    if val.instance_of?(OpenStruct)
      self.show(val, prefix + k + '.')
    else
      PP.pp(val, sio, VAR_LENGTH)
      sio.seek 0
      val = sio.read
      val = val.split("\n").join(SEP)

      key = prefix + k
      key[(KEY_LENGTH-3)..-1] = '...' if key.length > KEY_LENGTH
      puts(FMT % [key, val])
    end
  end
end

.show_attr(ostruct, key) ⇒ Object

Print a single attribute from the given ostruct to stdout. The attributed is identified by the given key.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bones/debug.rb', line 50

def self.show_attr( ostruct, key )
  sio = StringIO.new

  key = key.dup if key.frozen?
  val = key.split('.').inject(ostruct) {|os,k| os.send(k)}

  if val.instance_of?(OpenStruct)
    self.show(val, key + '.')
  else
    PP.pp(val, sio, VAR_LENGTH)
    sio.seek 0
    val = sio.read
    val = val.split("\n").join(SEP)

    key[(KEY_LENGTH-3)..-1] = '...' if key.length > KEY_LENGTH
    puts(FMT % [key, val])
  end
end