Class: Eventually::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/eventually/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#callablesObject (readonly)

Returns the value of attribute callables.



6
7
8
# File 'lib/eventually/event.rb', line 6

def callables
  @callables
end

#nameObject (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

Returns:

  • (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?

Returns:

  • (Boolean)


42
43
44
# File 'lib/eventually/event.rb', line 42

def emittable?
  @emittable
end

#remove_all_callablesObject



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