Class: Eventually::Event
- Inherits:
-
Object
- Object
- Eventually::Event
- Defined in:
- lib/eventually/event.rb
Instance Attribute Summary collapse
-
#callables ⇒ Object
readonly
Returns the value of attribute callables.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_callable(callable) ⇒ Object
- #callable_valid?(callable) ⇒ Boolean
- #emit(*payload) ⇒ Object
- #emittable? ⇒ Boolean (also: #registerable?)
-
#initialize(name, emittable = true) ⇒ Event
constructor
A new instance of Event.
- #remove_all_callables ⇒ Object
- #remove_callable(callable_to_remove) ⇒ Object
Constructor Details
#initialize(name, emittable = true) ⇒ Event
Returns a new instance of Event.
8 9 10 11 12 |
# File 'lib/eventually/event.rb', line 8 def initialize(name, emittable=true) @name = name.to_sym @emittable = !!emittable @callables = [] end |
Instance Attribute Details
#callables ⇒ Object (readonly)
Returns the value of attribute callables.
6 7 8 |
# File 'lib/eventually/event.rb', line 6 def callables @callables end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/eventually/event.rb', line 6 def name @name end |
Instance Method Details
#add_callable(callable) ⇒ Object
14 15 16 17 |
# File 'lib/eventually/event.rb', line 14 def add_callable(callable) raise "Event type :#{name} will not be emitted." unless emittable? @callables << callable if callable_valid?(callable) end |
#callable_valid?(callable) ⇒ Boolean
38 39 40 |
# File 'lib/eventually/event.rb', line 38 def callable_valid?(callable) callable.is_a?(Eventually::Callable) end |
#emit(*payload) ⇒ Object
32 33 34 35 36 |
# File 'lib/eventually/event.rb', line 32 def emit(*payload) raise "Event type :#{name} cannot be emitted." unless emittable? @callables.each {|callable| callable.call(*payload) } @callables.delete_if {|callable| !callable.continuous? } end |
#emittable? ⇒ Boolean Also known as: registerable?
42 43 44 |
# File 'lib/eventually/event.rb', line 42 def emittable? @emittable end |
#remove_all_callables ⇒ Object
28 29 30 |
# File 'lib/eventually/event.rb', line 28 def remove_all_callables @callables = [] end |
#remove_callable(callable_to_remove) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/eventually/event.rb', line 19 def remove_callable(callable_to_remove) if callable_valid?(callable_to_remove) delete_handler = proc{|callable| callable == callable_to_remove } else delete_handler = proc{|callable| callable.callable == callable_to_remove } end @callables.delete_if(&delete_handler) end |