Created
September 15, 2012 14:18
-
-
Save davybrion/3728156 to your computer and use it in GitHub Desktop.
code snippets for "Using C#-style Events In Ruby" post
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 Event | |
| attr_reader :name | |
| def initialize(name) | |
| @name = name | |
| @handlers = [] | |
| end | |
| def +(eventhandler) | |
| raise TypeError "Method expected" unless eventhandler.is_a? Method | |
| @handlers[@handlers.size] = eventhandler | |
| self | |
| end | |
| def -(eventhandler) | |
| @handlers.delete eventhandler | |
| self | |
| end | |
| def trigger(args) | |
| @handlers.each { |handler| handler.call args } | |
| 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
| publisher.some_event += method(:my_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
| publisher.some_event -= method(:my_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 Object | |
| private | |
| def define_event(symbol) | |
| getter = symbol | |
| setter = :"#{symbol}=" | |
| variable = :"@#{symbol}" | |
| define_method getter do | |
| event = instance_variable_get variable | |
| if event == nil | |
| event = Event.new(symbol.to_s) | |
| instance_variable_set variable, event | |
| end | |
| event | |
| end | |
| define_method setter do |value| | |
| instance_variable_set variable, value | |
| end | |
| end | |
| def trigger_event(symbol, args) | |
| event = instance_variable_get :"@#{symbol}" | |
| event.trigger *args | |
| 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
| publisher.some_event += method(:my_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
| publisher.some_event = publisher.some_event + method(:my_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 Publisher | |
| define_event :notify | |
| def trigger_notify | |
| trigger_event :notify, "hello world!" | |
| 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 Subscriber | |
| def start_listening_to(publisher) | |
| raise TypeError "Publisher expected" unless publisher.is_a? Publisher | |
| @publisher = publisher | |
| @publisher.notify += method(:event_handler) | |
| end | |
| def stop_listening | |
| @publisher.notify -= method(:event_handler) | |
| @publiser = nil | |
| end | |
| def event_handler(args) | |
| puts "#{object_id} #{Time.now} received: #{args}" | |
| 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
| publisher = Publisher.new | |
| subscriber1 = Subscriber.new | |
| subscriber2 = Subscriber.new | |
| subscriber1.start_listening_to publisher | |
| subscriber2.start_listening_to publisher | |
| publisher.trigger_notify | |
| subscriber1.stop_listening | |
| publisher.trigger_notify |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment