Class: AMQP::Events::Event
- Inherits:
-
Object
- Object
- AMQP::Events::Event
- Defined in:
- lib/amqp-events/event.rb
Overview
Represent Event that can be subscribed to (by subscribers/observers) All subscribed observers will be called when this Event ‘fires’.
TODO: Mutexes to synchronize @subscribers update/event fire ? github.com/snuxoll/ruby-event/blob/master/lib/ruby-event/event.rb TODO: Meta-methods that allow Events to fire on method invocations: github.com/nathankleyn/ruby_events/blob/85f8e6027fea22e9d828c91960ce2e4099a9a52f/lib/ruby_events.rb TODO: Add exception handling and subscribe/unsubscribe notifications: github.com/matsadler/rb-event-emitter/blob/master/lib/events.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subscribers ⇒ Object
(also: #listeners)
readonly
Returns the value of attribute subscribers.
Class Method Summary collapse
-
.create(host, name, opts = {}, &block) ⇒ Object
Creates Event of appropriate subclass, depending on arguments.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#clear ⇒ Object
Clears all the subscribers for a given Event.
-
#fire(*args) ⇒ Object
(also: #call)
TODO: make fire async: just fire and continue, instead of waiting for all subscribers to return, as it is right now.
-
#initialize(host, name) ⇒ Event
constructor
A new instance of Event.
-
#subscribe(*args, &block) ⇒ Object
(also: #listen, #+)
You can subscribe anything callable to the event, such as lambda/proc, method(:method_name), attached block or a custom Handler.
-
#unsubscribe(name) ⇒ Object
(also: #remove, #-)
Unsubscribe existing subscriber by name.
Constructor Details
#initialize(host, name) ⇒ Event
Returns a new instance of Event.
27 28 29 30 31 |
# File 'lib/amqp-events/event.rb', line 27 def initialize(host, name) @host = host @name = name.to_sym @subscribers = {} end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/amqp-events/event.rb', line 24 def host @host end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/amqp-events/event.rb', line 24 def name @name end |
#subscribers ⇒ Object (readonly) Also known as: listeners
Returns the value of attribute subscribers.
24 25 26 |
# File 'lib/amqp-events/event.rb', line 24 def subscribers @subscribers end |
Class Method Details
.create(host, name, opts = {}, &block) ⇒ Object
Creates Event of appropriate subclass, depending on arguments
19 20 21 |
# File 'lib/amqp-events/event.rb', line 19 def create host, name, opts={}, &block opts.empty? ? Event.new(host, name, &block ) : ExternalEvent.new(host, name, opts, &block) end |
Instance Method Details
#==(other) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/amqp-events/event.rb', line 88 def == (other) case other when Event super when nil @subscribers.empty? else false end end |
#clear ⇒ Object
Clears all the subscribers for a given Event
84 85 86 |
# File 'lib/amqp-events/event.rb', line 84 def clear @subscribers.clear end |
#fire(*args) ⇒ Object Also known as: call
TODO: make fire async: just fire and continue, instead of waiting for all subscribers to return, as it is right now. AMQP callbacks and EM:Deferrable?
71 72 73 74 75 |
# File 'lib/amqp-events/event.rb', line 71 def fire(*args) @subscribers.each do |key, subscriber| subscriber.call *args end end |
#subscribe(*args, &block) ⇒ Object Also known as: listen, +
You can subscribe anything callable to the event, such as lambda/proc, method(:method_name), attached block or a custom Handler. The only requirements, it should respond to a #call and accept arguments given to Event#fire.
You can give optional name to your subscriber. If you do, you can later unsubscribe it using this name. If you do not give subscriber a name, it will be auto-generated using its #name method and uuid.
You can unsubscribe your subscriber later, provided you know its name.
:call-seq:
event.subscribe("subscriber_name", proc{|*args| "Subscriber 1"})
event.subscribe("subscriber_name", method(:method_name))
event.subscribe(method(:method_name) # Implicit subscriber name == :method_name
event.subscribe("subscriber_name") {|*args| "Named subscriber block" }
event += method(:method_name) # C# compatible syntax, just without useless "delegates"
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/amqp-events/event.rb', line 50 def subscribe(*args, &block) subscriber = block ? block : args.pop name = args.empty? ? generate_subscriber_name(subscriber) : args.first.to_sym raise HandlerError.new "Handler #{subscriber.inspect} does not respond to #call" unless subscriber.respond_to? :call raise HandlerError.new "Handler name #{name} already in use" if @subscribers.has_key? name @subscribers[name] = subscriber self # This allows C#-like syntax : my_event += subscriber end |
#unsubscribe(name) ⇒ Object Also known as: remove, -
Unsubscribe existing subscriber by name
62 63 64 65 66 67 |
# File 'lib/amqp-events/event.rb', line 62 def unsubscribe(name) raise HandlerError.new "Unable to unsubscribe handler #{name}" unless @subscribers.has_key? name @subscribers.delete(name) self # This allows C#-like syntax : my_event -= subscriber end |