Class: RubyPatterns::VirtualProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_patterns/virtual_proxy.rb

Constant Summary collapse

GHOST =
"GHOST"
LOADED =
"LOADED"

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ VirtualProxy

Returns a new instance of VirtualProxy.



13
14
15
16
# File 'lib/ruby_patterns/virtual_proxy.rb', line 13

def initialize(key)
  @__key = key
  @__real = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



36
37
38
39
40
41
# File 'lib/ruby_patterns/virtual_proxy.rb', line 36

def method_missing(name, *args, &block)
  if __real.nil?
    __load
  end
  __real.send(name, *args, &block)
end

Instance Method Details

#__loadObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/ruby_patterns/virtual_proxy.rb', line 26

def __load
  raise NotImplementedError, "Subclasses must implement `__load`."
end

#__stateObject



30
31
32
# File 'lib/ruby_patterns/virtual_proxy.rb', line 30

def __state
  __real ? LOADED : GHOST
end

#inspectObject



22
23
24
# File 'lib/ruby_patterns/virtual_proxy.rb', line 22

def inspect
  "#<#{self.class.name} key=#{__key.inspect} #{__state}>"
end

#to_sObject



18
19
20
# File 'lib/ruby_patterns/virtual_proxy.rb', line 18

def to_s
  __key
end