Skip to content

Commit

Permalink
Refactor Cordon.blacklist and add Cordon.monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksam committed Mar 15, 2012
1 parent b3ccb0e commit 22454bd
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/cordon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ def __cordon__wrap_method__(method)
module Cordon
# Shorthand for blacklisting the undesirable methods in specific frameworks
def self.embargo(framework)
case framework
when :rspec
blacklist Kernel, [:should, :should_not]
when :minitest_spec
must_and_wont = MiniTest::Expectations.instance_methods.map(&:to_s).select {|e| e =~ /^(must|wont)_/}
blacklist MiniTest::Expectations, must_and_wont
else
raise "I don't know how to embargo #{framework}!"
end
wrap_framework(framework, :blacklist)
end

# Shorthand for watchlisting the undesirable methods in specific frameworks
def self.monitor(framework)
wrap_framework(framework, :watchlist)
end

# Declare specific methods as off-limits so that invocations raise an exception
Expand Down Expand Up @@ -76,4 +73,29 @@ def self.filter_violation_backtrace(&proc)
def self.dont_filter_violation_backtrace!
Violation.clear_custom_backtrace_filters
end

protected

def self.wrap_framework(framework, technique)
raise "Don't know how to wrap framework with #{technique.inspect}!" unless [:blacklist, :watchlist].include?(technique)

# Figure out which methods to wrap
list = []
case framework
when :rspec
list << [Kernel, [:should, :should_not]]
when :minitest_spec
must_and_wont = MiniTest::Expectations.instance_methods.map(&:to_s).select {|e| e =~ /^(must|wont)_/}
list << [MiniTest::Expectations, must_and_wont]
else
raise "I don't know how to embargo #{framework}!"
end

# Wrap them using the appropriate technique
# (which should be either :blacklist or :watchlist)
list.each do |subject, methods|
send technique, subject, methods
end
end

end

0 comments on commit 22454bd

Please sign in to comment.