Class: Orchestrator::Core::RequestsProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/orchestrator/core/requests_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread, modules, user = nil) ⇒ RequestsProxy

Returns a new instance of RequestsProxy.



9
10
11
12
13
14
15
16
17
18
# File 'lib/orchestrator/core/requests_proxy.rb', line 9

def initialize(thread, modules, user = nil)
    if modules.nil?
        @modules = []
    else
        @modules = modules.is_a?(Array) ? modules : [modules]
    end
    @thread = thread
    @user = user
    @trace = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/orchestrator/core/requests_proxy.rb', line 64

def method_missing(name, *args, &block)
    if ::Orchestrator::Core::PROTECTED[name]
        err = Error::ProtectedMethod.new "attempt to access a protected method '#{name}' in multiple modules"
        ::Libuv::Q.reject(@thread, err)
        # TODO:: log warning err.message
    else
        @trace = caller

        promises = @modules.map do |mod|
            defer = mod.thread.defer
            mod.thread.schedule do
                # Keep track of previous in case of recursion
                previous = nil
                begin
                    if @user
                        previous = mod.current_user
                        mod.current_user = @user
                    end

                    defer.resolve(
                        mod.instance.public_send(name, *args, &block)
                    )
                rescue => e
                    mod.logger.print_error(e, '', @trace)
                    defer.reject(e)
                ensure
                    mod.current_user = previous if @user
                end
            end
            defer.promise
        end

        @thread.finally(*promises)
    end
end

Instance Attribute Details

#traceObject (readonly)

Returns the value of attribute trace.



21
22
23
# File 'lib/orchestrator/core/requests_proxy.rb', line 21

def trace
  @trace
end

Instance Method Details

#[](index) ⇒ Object Also known as: at



48
49
50
51
52
# File 'lib/orchestrator/core/requests_proxy.rb', line 48

def [](index)
    mod = @modules[index]
    return nil unless mod
    return RequestProxy.new(@thread, mod, @user)
end

#eachObject

Provide Enumerable support



25
26
27
28
29
30
31
# File 'lib/orchestrator/core/requests_proxy.rb', line 25

def each
    return enum_for(:each) unless block_given?

    @modules.each do |mod|
        yield RequestProxy.new(@thread, mod, @user)
    end
end

#firstObject



42
43
44
45
46
# File 'lib/orchestrator/core/requests_proxy.rb', line 42

def first
    mod = @modules.first
    return nil unless mod
    return RequestProxy.new(@thread, mod, @user)
end

#lastObject



36
37
38
39
40
# File 'lib/orchestrator/core/requests_proxy.rb', line 36

def last
    mod = @modules.last
    return nil unless mod
    return RequestProxy.new(@thread, mod, @user)
end

#nil?true|false

Returns true if there is no object to proxy Allows RequestProxy and RequestsProxy to be used interchangably

Returns:

  • (true|false)


59
60
61
# File 'lib/orchestrator/core/requests_proxy.rb', line 59

def nil?
    @modules.empty?
end