Class: Core::Handler::Callable

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

Overview

public

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Callable

Returns a new instance of Callable.



38
39
40
41
42
43
44
45
# File 'lib/core/handler/callable.rb', line 38

def initialize(object)
  @object = object
  @mutex = Mutex.new
  @compiled = false
  @pipelines = {
    __core_event_global__: Core::Pipeline::Callable.new(@object)
  }
end

Instance Attribute Details

#pipelinesObject (readonly)

Returns the value of attribute pipelines.



36
37
38
# File 'lib/core/handler/callable.rb', line 36

def pipelines
  @pipelines
end

Class Method Details

.build_nameObject



15
16
17
18
19
20
21
22
23
# File 'lib/core/handler/callable.rb', line 15

def build_name
  suffix = generate_random_suffix
  if __used_random_suffixes__.include?(suffix)
    build_name
  else
    __used_random_suffixes__ << suffix
    "callback_#{suffix}"
  end
end

.generate_random_suffixObject



25
26
27
# File 'lib/core/handler/callable.rb', line 25

def generate_random_suffix
  SecureRandom.hex(8)
end

Instance Method Details

#finalizeObject

public


98
99
100
101
102
# File 'lib/core/handler/callable.rb', line 98

def finalize
  compile

  self
end

#handle(*events, global: false, method: nil, context: nil, &block) ⇒ Object

public


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/core/handler/callable.rb', line 67

def handle(*events, global: false, method: nil, context: nil, &block)
  @mutex.synchronize do
    if global == true
      @pipelines[:__core_event_global__].action(method, context: context, curry: true, &block)
    end

    needs_recompile = false

    events.each do |event|
      event = event.to_sym if event.is_a?(String)

      unless @pipelines.include?(event)
        @pipelines[event] = Core::Pipeline::Callable.new(@object)
        needs_recompile = true
      end

      @pipelines[event].action(method, context: context, curry: true, &block)
    end

    recompile if compiled? && needs_recompile
  end
end

#initialize_copyObject



47
48
49
50
51
52
53
# File 'lib/core/handler/callable.rb', line 47

def initialize_copy(...)
  @pipelines = @pipelines.each_pair.each_with_object({}) { |(key, value), hash|
    hash[key] = value.clone
  }

  super
end

#relocate(object) ⇒ Object

public

Relocates to another object context.



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

def relocate(object)
  @object = object

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

#trigger(object, event) ⇒ Object

public


92
93
94
# File 'lib/core/handler/callable.rb', line 92

def trigger(object, event, ...)
  finalize.trigger(object, event, ...)
end