Last active
July 1, 2019 19:28
-
-
Save rjlynch/65f805deb998341808e1eaf0977bf001 to your computer and use it in GitHub Desktop.
event handler
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
| class EventHandler | |
| attr_accessor :some_error, | |
| :some_compelted_task, | |
| :something_else | |
| def initialize | |
| instance_variables.each do |symbol| | |
| instance_varialbe_set(symbol, proc {}) | |
| end | |
| end | |
| def on(event, &block) | |
| public_send "#{event}=", block | |
| end | |
| end |
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
| class Example | |
| attr_reader :events | |
| def initialize | |
| @events = EventHandler.new | |
| end | |
| def do_thing | |
| # ... | |
| events.some_completed_task.call 'info' | |
| end | |
| end | |
| example = Example.new | |
| example.events.on :some_compelted_task do |msg| | |
| handle msg | |
| end | |
| example.do_thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment