Module: Protoboard::CircuitProxyFactory

Defined in:
lib/protoboard/circuit_proxy_factory.rb

Overview

This module is responsible to manage a proxy module that executes the circuit.

Class Method Summary collapse

Class Method Details

.create_module(circuits, class_name) ⇒ Object

Creates the module that executes the circuit



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/protoboard/circuit_proxy_factory.rb', line 11

def create_module(circuits, class_name)
  module_name = infer_module_name(class_name, circuits.map(&:method_name))
  proxy_module = Module.new

  # Encapsulates instance methods in a module to be used later
  instance_methods = Module.new do
    circuits.each do |circuit|
      unless circuit.singleton_method?
        define_method(circuit.method_name) do |*args|
          Protoboard.config.adapter.run_circuit(circuit) { super(*args) }
        end
      end
    end
  end

  proxy_module.const_set('InstanceMethods', instance_methods)

  # Encapsulates singleton methods in a module to be used later
  class_methods = Module.new do
    circuits.each do |circuit|
      if circuit.singleton_method?
        define_method(circuit.method_name) do |*args|
          Protoboard.config.adapter.run_circuit(circuit) { super(*args) }
        end
      end
    end
  end

  proxy_module.const_set('ClassMethods', class_methods)

  Protoboard.const_set(module_name, proxy_module)
end