Class: Core::Event::Callable

Inherits:
Object
  • Object
show all
Includes:
Is::Inspectable
Defined in:
lib/core/event/callable.rb

Overview

public

Manages and calls an event pipeline.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Callable

Returns a new instance of Callable.



17
18
19
20
21
22
# File 'lib/core/event/callable.rb', line 17

def initialize(object)
  @object = object
  @mutex = Mutex.new
  @events = []
  @pipelines = {}
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



34
35
36
# File 'lib/core/event/callable.rb', line 34

def events
  @events
end

#pipelinesObject (readonly)

Returns the value of attribute pipelines.



35
36
37
# File 'lib/core/event/callable.rb', line 35

def pipelines
  @pipelines
end

Instance Method Details

#callback(*during, before: [], after: [], method: nil, context: nil, &block) ⇒ Object

public

Defines a callback to be called during, before, and/or after the given events.



59
60
61
62
63
# File 'lib/core/event/callable.rb', line 59

def callback(*during, before: [], after: [], method: nil, context: nil, &block)
  define_callback(:before, *before, method: method, context: context, &block)
  define_callback(:during, *during, method: method, context: context, &block)
  define_callback(:after, *after, method: method, context: context, &block)
end

#finished(object, event) ⇒ Object

public

Calls after callbacks for the given event with the given arguments.



95
96
97
# File 'lib/core/event/callable.rb', line 95

def finished(object, event, ...)
  compile.finished(object, event, ...)
end

#initialize_copyObject



24
25
26
27
28
29
30
31
32
# File 'lib/core/event/callable.rb', line 24

def initialize_copy(...)
  @pipelines = @pipelines.each_pair.each_with_object({}) { |(event, pipelines), pipelines_hash|
    pipelines_hash[event] = pipelines.each_pair.each_with_object({}) { |(type, pipeline), events_hash|
      events_hash[type] = pipeline.clone
    }
  }

  super
end

#performing(object, event) ⇒ Object

public

Calls callbacks for the given event with the given arguments.



83
84
85
# File 'lib/core/event/callable.rb', line 83

def performing(object, event, ...)
  compile.performing(object, event, ...)
end

#register(*events) ⇒ Object

public

Registers one or more events by name.



51
52
53
54
55
# File 'lib/core/event/callable.rb', line 51

def register(*events)
  @mutex.synchronize do
    @events.concat(events.map(&:to_sym)).uniq!
  end
end

#relocate(object) ⇒ Object

public

Relocates to another object context.



39
40
41
42
43
44
45
46
47
# File 'lib/core/event/callable.rb', line 39

def relocate(object)
  @object = object

  @pipelines.each_value do |pipelines_hash|
    pipelines_hash.each_value do |pipeline|
      pipeline.relocate(object)
    end
  end
end

#starting(object, event) ⇒ Object

public

Calls before callbacks for the given event with the given arguments.



89
90
91
# File 'lib/core/event/callable.rb', line 89

def starting(object, event, ...)
  compile.starting(object, event, ...)
end