lib/debug_helper.rb in debug_helper-1.5.0 vs lib/debug_helper.rb in debug_helper-1.6.0
- old
+ new
@@ -4,58 +4,58 @@
class DebugHelper
module Putd
- def putd(obj, message)
- DebugHelper.show(obj, message)
+ def putd(obj, message, options = {})
+ DebugHelper.show(obj, message, options)
end
end
attr_accessor \
:obj,
:message,
- :object_ids
+ :object_ids,
+ :depth
- def self.show(obj, message = obj.class)
- debug_helper = DebugHelper.new(obj, message)
+ def self.show(obj, message = obj.class, options = {})
+ debug_helper = DebugHelper.new(obj, message, options)
x = debug_helper.send(:_show, obj, message, info = {})
puts x.to_yaml
end
private
- def initialize(obj, message)
+ def initialize(obj, message, options)
self.obj = obj
self.message = message
+ self.depth = options[:depth] || 3
self.object_ids = []
end
def _show(obj, message, info)
if object_ids.include?(obj.object_id)
s = show_object(obj, message, info)
+ elsif depth == object_ids.size
+ s = show_object(obj, message, info)
else
object_ids.push(obj.object_id)
s = case
when obj.kind_of?(Array)
show_array(obj, message, info)
when obj.kind_of?(Hash)
show_hash(obj, message, info)
- when obj.kind_of?(String)
- show_string(obj, message, info)
when obj.kind_of?(Struct)
show_struct(obj, message, info)
- when obj.kind_of?(Symbol)
- show_symbol(obj, message, info)
# when obj.kind_of?(Range)
# when obj.kind_of?(Set)
else
show_object(obj, message, info)
end
+ object_ids.pop
end
- object_ids.pop
s
end
def show_array(obj, message, info)
content = {}
@@ -84,25 +84,34 @@
}
_show_item(obj.class.name, content, attrs, info)
end
def show_object(obj, message, info)
- message_info = message.nil? ? '' : " (message='#{message}')"
- "#{obj.class.name}#{message_info} #{obj}"
+ methods = methods_for_object(obj)
+ if methods.nil?
+ message_info = message.nil? ? '' : " (message='#{message}')"
+ "#{obj.class.name}#{message_info} #{obj}"
+ else
+ content = {}
+ attrs = {:message => message}
+ methods[:instance].each do |instance_method|
+ value = obj.send(instance_method)
+ if instance_method == :size
+ attrs.store(:size, value)
+ else
+ content.store(instance_method.to_s, value)
+ end
+ end
+ methods[:class].each do |pair|
+ class_method, arguments = *pair
+ value = Object.const_get(obj.class.to_s).send(class_method, *arguments)
+ content.store(class_method.to_s, value)
+ end
+ _show_item(obj.class.name, content, attrs, info)
+ end
end
- def show_string(obj, message, info)
- attrs = {
- :message => message,
- :size => obj.size,
- :encoding => obj.encoding,
- :ascii_only => obj.ascii_only?,
- :bytesize => obj.bytesize,
- }
- _show_item(obj.class.name, [obj], attrs, info)
- end
-
def show_struct(obj, message, info)
content = {}
i = 0
obj.each_pair do |member|
member_name, value = *member
@@ -115,19 +124,10 @@
:size => obj.size,
}
_show_item(obj.class.name, content, attrs, info)
end
- def show_symbol(obj, message, info)
- attrs = {
- :message => message,
- :size => obj.size,
- :encoding => obj.encoding,
- }
- _show_item(obj.class.name, obj, attrs, info)
- end
-
def _show_item(class_name, content, attrs, info)
message = attrs[:message]
unless message.nil?
attrs[:message] = "'#{message}'"
end
@@ -139,10 +139,51 @@
def label(class_name, attrs)
a = []
attrs.each_pair do |key, value|
a.push("#{key}=#{value}") unless value.nil?
end
+ return class_name if a.empty?
attrs_s = a.join(' ')
"#{class_name} (#{attrs_s})"
+ end
+
+ def methods_for_object(obj)
+ methods = case
+ when obj.kind_of?(File)
+ {
+ # The instance forms of some of these require the file to be open.
+ :class => {
+ :path => [obj.path],
+ :atime => [obj.path],
+ :ctime => [obj.path],
+ :mtime => [obj.path],
+ :size => [obj.path],
+ },
+ :instance => [],
+ }
+ when obj.kind_of?(String)
+ {
+ :class => {},
+ :instance => [
+ :to_s,
+ :size,
+ :encoding,
+ :ascii_only?,
+ :bytesize,
+ ]
+ }
+ when obj.kind_of?(Symbol)
+ {
+ :class => {},
+ :instance => [
+ :to_s,
+ :size,
+ :encoding,
+ ]
+ }
+ else
+ nil
+ end
+ methods
end
end