Class: LazyMethods::AsyncProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/lazy_methods.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Proxy

Proxy::PROTECTED_METHODS

Instance Method Summary collapse

Methods inherited from Proxy

#method_missing

Constructor Details

#initialize(&block) ⇒ AsyncProxy

Returns a new instance of AsyncProxy.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/lazy_methods.rb', line 113

def initialize(&block)
  @proxy_result = nil
  @proxy_exception = nil
  if defined?(Thread.critical) && Thread.critical
    @proxy_result = block.call
  else
    @thread = Thread.new do
      begin
        @proxy_result = block.call
      rescue Exception => e
        @proxy_exception = e
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class LazyMethods::Proxy

Instance Method Details

#__proxy_loaded__Object



137
138
139
# File 'lib/lazy_methods.rb', line 137

def __proxy_loaded__
  !(@thread && @thread.alive?)
end

#__proxy_result__Object

Get the result of the original method call. The original method will only be called once.

Raises:

  • (@proxy_exception)


130
131
132
133
134
135
# File 'lib/lazy_methods.rb', line 130

def __proxy_result__
  @thread.join if @thread && @thread.alive?
  @thread = nil
  raise @proxy_exception if @proxy_exception
  return @proxy_result
end