Last active
December 19, 2015 18:49
-
-
Save benfalk/6001868 to your computer and use it in GitHub Desktop.
Just playin
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
#!/usr/bin/env ruby | |
module DarkHorse | |
class << self | |
def included(base) | |
base.send(:extend, DarkHorse::SingletonMethods) | |
end | |
end | |
# | |
# | |
# | |
module ClassMethods | |
end | |
# | |
# | |
# | |
module SingletonMethods | |
def dark_horse(method) | |
if method_defined? method | |
saddle_up method | |
else | |
@saddle_stack ||= [] | |
@saddle_stack << method.to_sym | |
end | |
end | |
def method_added(method) | |
if @saddle_stack.include? method.to_sym | |
@saddle_stack.delete method.to_sym | |
saddle_up method | |
end | |
super | |
end | |
def saddle_up(method) | |
alias_method "saddled_#{method}", method | |
define_method method do |*args| | |
puts "Saddling #{method}" | |
send "saddled_#{method}", *args | |
end | |
end | |
end | |
end | |
class Test | |
include DarkHorse | |
dark_horse :the_night | |
dark_horse :the_day | |
def the_day | |
puts "it's the day" | |
end | |
def the_night | |
puts "it's the night" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment