Skip to content

Instantly share code, notes, and snippets.

@backus
Last active December 15, 2020 17:14
Show Gist options
  • Save backus/2d5a77acd37767a54fc496bbaba1cb8b to your computer and use it in GitHub Desktop.
Save backus/2d5a77acd37767a54fc496bbaba1cb8b to your computer and use it in GitHub Desktop.
module Shitlist
def shitlist(method_name, whitelist)
original_method = instance_method(method_name)
undef_method(method_name)
define_method(method_name) do |*args, &block|
call = caller_locations.first
passes_whitelist = whitelist.any? do |label, file_pattern|
call.label == label && call.absolute_path.end_with?(file_pattern)
end
unless passes_whitelist
fail "Shitlisted method! Permitted callers: #{whitelist}"
end
original_method.bind(self).call(*args, &block)
end
end
end
class Example
extend Shitlist
def not_on_shitlist
qux
end
def baz
qux
end
def qux
puts 'Only some methods can call me :)'
end
shitlist :qux, 'baz' => 'shitlist.rb'
end
example = Example.new
# This will work
example.baz
# This will fail
example.not_on_shitlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment