Sha256: c263560bff32b3aefacb93e26897eb8485411d030af0ec31ebd9403b9e45aea5

Contents?: true

Size: 1.68 KB

Versions: 8

Compression:

Stored size: 1.68 KB

Contents

require 'pp'
require 'stringio'

module Bones

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

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

  # 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.
  #
  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

  # Print a single attribute from the given _ostruct_ to stdout. The
  # attributed is identified by the given _key_.
  #
  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

end  # module Debug
end  # module Bones

# EOF

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
TwP-bones-2.4.1 lib/bones/debug.rb
TwP-bones-2.4.2 lib/bones/debug.rb
TwP-bones-2.5.0 lib/bones/debug.rb
TwP-bones-2.5.1 lib/bones/debug.rb
bones-2.5.1 lib/bones/debug.rb
bones-2.4.2 lib/bones/debug.rb
bones-2.5.0 lib/bones/debug.rb
bones-2.4.1 lib/bones/debug.rb