Module: Actor::Base

Defined in:
lib/actor/base.rb

Overview

Module that actors should include

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Adds a hook before object initialization to automatically sets up the thread that executes actions and sets up the callback data structures.

  • Args:

    • klass: the class whose initialization is hooked into.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/actor/base.rb', line 50

def self.included klass
  class << klass
    alias_method :__actor_new, :new
    
    ##
    # Hooks into the initialization of the object to initialize the
    # mailbox. Also starts the thread executing async method calls
    def new *args
      instance = __actor_new *args
      instance.instance_variable_set :@audience, {}
      instance.instance_variable_set :@mailbox, Queue.new
        
      audience = {}
      audience[:before] = {}
      audience[:after] = {}
      instance.instance_variable_set :@audience, audience

      Thread.new do
        loop do
          mailbox = instance.instance_variable_get :@mailbox
          method_name, args, block = mailbox.pop
          
          if audience[:before][method_name]
            audience[:before][method_name].each { |callback| callback.call }
          end

          result = instance.method(method_name).call *args, &block
          
          if audience[:after][method_name]
            audience[:after][method_name].each { |callback| callback.yield result }
          end
        end
      end

      Proxy.new instance
    end
  end
end

Instance Method Details

#after_action(action, &block) ⇒ Object

Adds a listener that is called after the including object performs the specified action.

  • Args:

    • action: the action the hook into

    • block: the block to execute after the specified action

  • Yields: the value returned by the action



39
40
41
42
# File 'lib/actor/base.rb', line 39

def after_action action, &block
  @audience[:after][action] ||= Set.new
  @audience[:after][action] << block
end

#before_action(action, &block) ⇒ Object

Adds a listener that is called before the including object performs the specified action.

  • Args:

    • action: the action (symbol) the hook into

    • block: the block to execute before the specified action



26
27
28
29
# File 'lib/actor/base.rb', line 26

def before_action action, &block
  @audience[:before][action] ||= Set.new
  @audience[:before][action] << block
end

#send(method_name, *args, &block) ⇒ Object

Adds a new message to the queue. Args and block are optional

  • Args:

    • method_name: symbol representation of the action (method) to preform

    • args: the arguments to pass to the action when it is executed

    • block: the block to pass to the action when it is executed



15
16
17
# File 'lib/actor/base.rb', line 15

def send method_name, *args, &block
  @mailbox << [method_name, args, block]
end