Created
January 22, 2015 22:31
-
-
Save ravinggenius/70abb768676f7bdad1ee to your computer and use it in GitHub Desktop.
debug methods for rip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.debug(indent, label, context: nil, stack: nil) | |
body = if context && stack | |
[ | |
"#{indent * 2} context", | |
*context_chain(indent * 4, context), | |
"#{indent * 2} stack", | |
*call_stack(indent * 4, stack) | |
] | |
elsif context | |
[ | |
"#{indent * 2} context", | |
context_chain(indent * 4, context) | |
] | |
elsif stack | |
[ | |
"#{indent * 2} stack", | |
call_stack(indent * 4, stack) | |
] | |
else | |
[] | |
end | |
[ | |
(indent + label), | |
*body | |
] | |
end | |
def self.context_chain(indent, context) | |
extract_links(context).map do |link| | |
[ | |
link.class, | |
link.object_id, | |
(link.origin if link.respond_to?(:origin)), | |
link.symbols.to_a | |
] | |
end.map do |(klass, object_id, origin, symbols)| | |
"#{indent}#{klass}:#{object_id} (#{origin}) [ #{symbols.sort.join(', ')} ]" | |
end | |
end | |
def self.extract_links(context) | |
if context.respond_to?(:outer_context) && context.outer_context | |
[ | |
context, | |
*extract_links(context.outer_context) | |
] | |
else | |
[ | |
context | |
] | |
end | |
end | |
def self.call_stack(indent, native_stack) | |
native_stack.select do |frame| | |
/rip-rip/ =~ frame | |
end.map do |frame| | |
indent + frame | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment