Created
May 7, 2014 12:40
-
-
Save cforth/e74b82b053cb6342bdf3 to your computer and use it in GitHub Desktop.
How to improve dsl.rb? How to make a new "CleanRoom Class" let events.rb can use the method "event" and "setup"?
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
lambda { | |
setups = [] | |
events = {} | |
Kernel.send :define_method, :event do |name, &block| | |
events[name] = block | |
end | |
Kernel.send :define_method, :setup do |&block| | |
setups << block | |
end | |
Kernel.send :define_method, :each_event do |&block| | |
events.each_pair do |name, event| | |
block.call name, event | |
end | |
end | |
Kernel.send :define_method, :each_setup do |&block| | |
setups.each do |setup| | |
block.call setup | |
end | |
end | |
}.call | |
Dir.glob('*events.rb').each do |file| | |
load file | |
each_event do |name, event| | |
env = Object.new | |
each_setup do |setup| | |
env.instance_eval &setup | |
end | |
puts "ALERT: #{name}" if env.instance_eval &event | |
end | |
end |
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
event "the sky is falling" do | |
@sky_height < 300 | |
end | |
event "it's getting closer" do | |
@sky_height < @mountains_height | |
end | |
setup do | |
puts "Setting up sky" | |
@sky_height = 100 | |
end | |
setup do | |
puts "Setting up mountains" | |
@mountains_height = 200 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment