Created
September 15, 2012 13:18
-
-
Save davybrion/3727807 to your computer and use it in GitHub Desktop.
code snippets for "Auto-Wiring Ruby Events" 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 Publisher | |
include EventPublisher | |
event :first_event | |
event :second_event | |
def trigger_events | |
trigger :first_event, "first event" | |
trigger :second_event, "second event" | |
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.subscribe :first_event, method(:first_event_handler) | |
@publisher.subscribe :second_event, method(:second_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.subscribe_all self |
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 initialize(publisher) | |
@publisher = publisher | |
@publisher.subscribe_all self | |
end | |
def stop_listening | |
@publisher.unsubscribe_all self | |
end | |
def first_event_handler(args) | |
puts args | |
end | |
def second_event_handler(args) | |
puts 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
def each_suitable_handler(subscriber) | |
possible_handlers = subscriber.class.instance_methods.select { |name| name =~ /\w_handler/ } | |
possible_handlers.each do |method_name| | |
event_name = /(?<event_name>.*)_handler/.match(method_name)[:event_name] | |
if EVENTS.include? event_name.to_sym | |
yield event_name.to_sym, method_name.to_sym | |
end | |
end | |
end | |
def subscribe_all(subscriber) | |
each_suitable_handler(subscriber) do |event_symbol, method_symbol| | |
subscribe event_symbol, subscriber.method(method_symbol) | |
end | |
end | |
def unsubscribe_all(subscriber) | |
each_suitable_handler(subscriber) do |event_symbol, method_symbol| | |
unsubscribe event_symbol, subscriber.method(method_symbol) | |
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
self.class.class_eval do | |
EVENTS = [] | |
def event(symbol) | |
getter = symbol | |
variable = :"@#{symbol}" | |
EVENTS << 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 | |
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 | |
subscriber = Subscriber.new(publisher) | |
publisher.trigger_events | |
subscriber.stop_listening | |
publisher.trigger_events |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment