Class: HasHelpers::Constant::Proxy

Inherits:
BasicObject
Defined in:
lib/has_helpers/constant.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, constant_name) ⇒ Proxy

Returns a new instance of Proxy.



251
252
253
254
255
# File 'lib/has_helpers/constant.rb', line 251

def initialize(klass, constant_name)
  @klass = klass
  @constant_name = constant_name
  @mutex = ::Mutex.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs, &block) ⇒ Object

Delegate all methods to the underlying object.



267
268
269
# File 'lib/has_helpers/constant.rb', line 267

def method_missing(method_name, *args, **kwargs, &block)
  __getobj__.send(method_name, *args, **kwargs, &block)
end

Instance Method Details

#__getobj__Object



257
258
259
260
261
262
263
264
# File 'lib/has_helpers/constant.rb', line 257

def __getobj__
  @mutex.synchronize do  # Make sure this does not get evaluated simultaneously across threads.
    @__getobj__ ||= begin
               @klass.send(:remove_const, @constant_name) if @klass.const_defined?(@constant_name, false) # Remove the Proxy object (self) which has served as a constant place-holder.
               @klass.const_get(@constant_name, false)
             end
  end
end