class DebugHelper

Constants

VERSION

Attributes

depth[RW]
message[RW]
obj[RW]
object_ids[RW]

Public Class Methods

new(obj, message, options) click to toggle source
# File lib/debug_helper.rb, line 53
def initialize(obj, message, options)
  self.obj = obj
  self.message = message
  self.depth = options[:depth] || 3
  self.object_ids = []
end
show(obj, message = obj.class, options = {}) click to toggle source
# File lib/debug_helper.rb, line 47
def self.show(obj, message = obj.class, options = {})
  debug_helper = DebugHelper.new(obj, message, options)
  s = debug_helper.show(obj, message, info = {})
  puts s.to_yaml
end

Public Instance Methods

depth_reached?() click to toggle source
# File lib/debug_helper.rb, line 64
def depth_reached?
  depth == object_ids.size
end
object_seen?(obj) click to toggle source
# File lib/debug_helper.rb, line 60
def object_seen?(obj)
  object_ids.include?(obj.object_id)
end
show(obj, message, info) click to toggle source
# File lib/debug_helper.rb, line 68
def show(obj, message, info)
  handler = nil
  if object_seen?(obj) || depth_reached?
    handler = GenericHandler.new(self, obj, message, info)
    s = handler.show
  else
    object_ids.push(obj.object_id)
    begin
      # If there's a handler for the class, use it.
      # Note that the class may be a custom class, not defined here,
      # but instead defined outside this project.
      # So if the user of this library has defined DebugHelper::FooHandler
      # and the object is a Foo, that handler will be selected and called.
      handler_class_name = "DebugHelper::#{obj.class.name}Handler"
      handler_class = Object.const_get(handler_class_name)
      handler = handler_class.new(method(__method__), obj, message, info)
    rescue
      # If there's not a handler for the class, try using :kind_of?.
      [
          Array,
          Dir,
          Exception,
          File,
          IO,
          Hash,
          OpenStruct,
          Range,
          Set,
          String,
          Struct,
          # There's no method Symbol.new, so cannot instantiate a subclass.
          # Symbol,
      ].each do |klass|
        if obj.kind_of?(klass)
          handler_class_name = "DebugHelper::#{klass.name}Handler"
          handler_class = Object.const_get(handler_class_name)
          handler = handler_class.new(method(__method__), obj, message, info)
          break
        end
      end
    end
    if handler.nil?
      if obj.instance_of?(Object)
        handler_class= ObjectHandler
      else
        handler_class = GenericHandler
      end
      handler = handler_class.new(self, obj, message, info)
    end
    s = handler.show
    object_ids.pop
  end
  s
end