Class: Orchestrator::Core::RequestProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrator/core/request_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread, mod, user = nil) ⇒ RequestProxy

Returns a new instance of RequestProxy.



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

def initialize(thread, mod, user = nil)
    @mod = mod
    @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

All other method calls are wrapped in a promise



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/orchestrator/core/request_proxy.rb', line 92

def method_missing(name, *args, &block)
    defer = @thread.defer

    if @mod.nil?
        err = Error::ModuleUnavailable.new "method '#{name}' request failed as the module is not available at this time"
        defer.reject(err)
        # TODO:: debug log here
    elsif ::Orchestrator::Core::PROTECTED[name]
        err = Error::ProtectedMethod.new "attempt to access module '#{@mod.settings.id}' protected method '#{name}'"
        defer.reject(err)
        @mod.logger.warn(err.message)
    else
        @trace = caller

        @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
    end

    defer.promise
end

Instance Attribute Details

#traceObject (readonly)

Returns the value of attribute trace.



56
57
58
# File 'lib/orchestrator/core/request_proxy.rb', line 56

def trace
  @trace
end

Instance Method Details

#[](name) ⇒ Object

Simplify access to status variables as they are thread safe



60
61
62
# File 'lib/orchestrator/core/request_proxy.rb', line 60

def [](name)
    @mod.instance[name]
end

#[]=(status, value) ⇒ Object



64
65
66
# File 'lib/orchestrator/core/request_proxy.rb', line 64

def []=(status, value)
    @mod.instance[status] = value
end

#arity(method) ⇒ Object

Looks up the arity of a method



87
88
89
# File 'lib/orchestrator/core/request_proxy.rb', line 87

def arity(method)
    @mod.instance.method(method.to_sym).arity
end

#nil?true|false

Returns true if there is no object to proxy

Returns:

  • (true|false)


71
72
73
# File 'lib/orchestrator/core/request_proxy.rb', line 71

def nil?
    @mod.nil?
end

#respond_to?(symbol, include_all = false) ⇒ true|false

Returns true if the module responds to the given method

Returns:

  • (true|false)


78
79
80
81
82
83
84
# File 'lib/orchestrator/core/request_proxy.rb', line 78

def respond_to?(symbol, include_all = false)
    if @mod
        @mod.instance.respond_to?(symbol, include_all)
    else
        false
    end
end