Module: RubyPatterns::VirtualProxy

Defined in:
lib/ruby_patterns/virtual_proxy.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(name, *args, &block)
  if __real.nil?
    self.__real = self.class.loader.call(__key)
  end
  __real.send(name, *args, &block)
end

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/ruby_patterns/virtual_proxy.rb', line 4

def self.included(base)
  base.class_eval do
    attr_reader :__key
    attr_accessor :__real

    class << self
      attr_accessor :loader
    end
  end
end

Instance Method Details

#initialize(key) ⇒ Object

Parameters:

  • key (Object)

    key used by the loader to load the real object



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

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