Last active
July 27, 2018 10:34
-
-
Save stevo/bc457cac6a5832109e4e387853662f48 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def with_enumerated(subject, method_name) | |
begin | |
subject.define_singleton_method("#{method_name}_with_enum") do |*args, &block| | |
return enum_for(method_name, *args) unless block.present? | |
public_send("#{method_name}_without_enum",*args, &block) | |
end | |
subject.singleton_class.alias_method "#{method_name}_without_enum", method_name | |
subject.singleton_class.alias_method method_name, "#{method_name}_with_enum" | |
yield | |
ensure | |
subject.singleton_class.alias_method method_name, "#{method_name}_without_enum" | |
subject.singleton_class.remove_method "#{method_name}_with_enum" | |
subject.singleton_class.remove_method "#{method_name}_without_enum" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment