Last active
August 29, 2015 14:10
-
-
Save markburns/18a7fc13e71dd9a02c90 to your computer and use it in GitHub Desktop.
Unsure what methods are used on an object in a Rails application?
This file contains 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
if Rails.env.development? | |
class BasicSpy < BasicObject | |
include SpyStuff | |
end | |
end |
This file contains 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
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 |
This file contains 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
[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 []) |
This file contains 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
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 |
This file contains 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
if Rails.env.development? | |
class TryableSpy | |
include SpyStuff | |
end | |
end |
This file contains 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
@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