Created
October 20, 2017 21:11
-
-
Save maxwells/b37d6ff772b7a8a660d351de0a59f066 to your computer and use it in GitHub Desktop.
garbage ruby decorators
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
module Decorator | |
def def_decorator(name, &blk) | |
define_method(name) do |method_name| | |
new_name = "#{method_name}_" | |
alias_method new_name, method_name | |
define_method(method_name) do | |
lmbda = lambda { send(new_name) } | |
blk.call(lmbda) | |
end | |
new_name | |
end | |
end | |
end | |
module TimingDecorator | |
extend Decorator | |
def_decorator(:time) do |lambda| | |
puts Time.now.to_i | |
lambda.call | |
puts Time.now.to_i | |
end | |
end | |
class Thing | |
extend TimingDecorator | |
time def do_the_thing | |
puts "doing the thing" | |
end | |
end | |
Thing.new.do_the_thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment