Created
May 9, 2012 12:00
-
-
Save quamen/2644014 to your computer and use it in GitHub Desktop.
A module that allows you to conditionally define a module. Useful for isolated testing, when you just need something to stub without requiring the actual class or module in the test.
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
class ConditionallyDefine | |
def self.stub_module(full_name) | |
full_name.to_s.split(/::/).inject(Object) do |context, name| | |
begin | |
context.const_get(name) | |
rescue NameError | |
context.const_set(name, Module.new) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment