Module: Asynchronize

Defined in:
lib/asynchronize.rb

Overview

Include this module to allow a declarative syntax for defining asynch methods

Defines only one method on the including class: `asynchronize`

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Defines the asynchronize method



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/asynchronize.rb', line 8

def self.included(base)
  base.class_eval do
    ##
    # Call to asynchronize a method.
    #
    #   This does two things
    #   1. Creates and prepends a module <BaseName>::Asynchronized.
    #   2. Defines each of the passed methods on that module.
    #
    #   Additional notes:
    #   - The new methods wrap the old method within Thread.new.
    #   - Subsequent calls only add methods to the existing Module.
    #   - Will silently fail if the method has already been asynchronized
    #
    # @param methods [Symbol] The methods to be asynchronized.
    # @example To add any number of methods to be asynchronized.
    #   asynchronize :method1, :method2, :methodn
    #
    def self.asynchronize(*methods)
      return if methods.empty?
      async_container = Asynchronize._get_container_for(self)
      Asynchronize._define_methods_on_object(methods, async_container)
    end
  end
end