Skip to content

Instantly share code, notes, and snippets.

@markburns
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markburns/18a7fc13e71dd9a02c90 to your computer and use it in GitHub Desktop.
Save markburns/18a7fc13e71dd9a02c90 to your computer and use it in GitHub Desktop.
Unsure what methods are used on an object in a Rails application?
if Rails.env.development?
class BasicSpy < BasicObject
include SpyStuff
end
end
if Rails.env.development?
class IterableSpy < TryableSpy
Enumerable.instance_methods.each do |m|
define_method(m) do |*args|
called_method(m, *args)
end
end
def each(&block)
called_method(:each)
result = called_method("block-inside-each")
block.call(result)
end
def to_s
"IterableSpy `#{@name}': #{@parent_method}"
end
end
end
[2014-12-02 09:47:22.581] INFO Region requires .display_name (called with [])
[2014-12-02 09:47:22.581] INFO Region requires display_name.downcase (called with [])
[2014-12-02 09:47:22.977] INFO Region requires .countries (called with [])
[2014-12-02 09:47:22.977] INFO Region requires countries.select (called with [])
[2014-12-02 09:47:22.977] INFO Region requires countries.select.sort_by (called with [])
[2014-12-02 09:47:23.113] INFO Region requires display_name.downcase.to_ascii (called with [])
[2014-12-02 09:47:23.121] INFO regions requires .display_name (called with [])
[2014-12-02 09:47:23.123] INFO regions requires .sid (called with [])
[2014-12-02 09:47:23.127] INFO Region requires countries.select.sort_by.each (called with [])
[2014-12-02 09:47:23.129] INFO Region requires countries.select.sort_by.block-inside-each (called with [])
[2014-12-02 09:47:23.130] INFO Region requires countries.select.sort_by.block-inside-each.display_name (called with [])
[2014-12-02 09:47:23.132] INFO Region requires .sid (called with [])
[2014-12-02 09:47:23.133] INFO Region requires countries.select.sort_by.block-inside-each.sid (called with [])
if Rails.env.development?
module SpyStuff
extend ActiveSupport::Concern
def initialize(name)
@name = name
end
def parent_method=(m)
@parent_method = m
end
def new_child_node(name, parent_method=nil)
self.class.new(name).tap{|r|r.parent_method = parent_method}
end
def method_missing(method_name, *args)
called_method(method_name, *args)
end
def called_method(method_name, *args)
args = if args.empty?
""
else
"with args #{args.to_s}"
end
call_info = caller[1..1][0].to_s.gsub(/:in `.*haml[\d|_]*'$/, "")
call_info.gsub!(/#{Rails.root}/, "")
Rails.logger.info "\n#{@name} #{@parent_method}.#{method_name} (called from #{call_info} #{args})"
new_child_node(@name, [@parent_method, method_name].compact.join("."))
end
def to_s
"Spy `#{@name}': #{@parent_method}"
end
end
end
if Rails.env.development?
class TryableSpy
include SpyStuff
end
end
@region = IterableSpy.new "Region"
@regions = IterableSpy.new "regions"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment