Class: Copland::ServiceModel::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/copland/models/proxy.rb

Overview

A proxy class to aid in deferred instantiation of service points. This is used primarily by the “deferred” service models.

Instance Method Summary collapse

Constructor Details

#initialize(service_point, &init) ⇒ Proxy

Create a new proxy that wraps the given service point.



44
45
46
47
48
# File 'lib/copland/models/proxy.rb', line 44

def initialize( service_point, &init )
  @service_point = service_point
  @mutex = Mutex.new
  @init = init
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Attempts to invoke the given message on the service. If the service has not yet been instantiated, it will be instantiated and stored.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/copland/models/proxy.rb', line 52

def method_missing( sym, *args, &block )
  unless @instance || @instantiation_failed
    @mutex.synchronize do
      unless @instance || @instantiation_failed
        begin
          @instance = @service_point.instantiate( &@init )
        rescue Exception
          @instantiation_failed = true
          raise
        end
      end
    end
  end

  unless @instantiation_failed
    @instance.__send__ sym, *args, &block
  else
    # just return nil... this way, a failed instantiation won't barf
    # more than once... I hope...
  end
end

Instance Method Details

#inspectObject



91
92
93
94
95
# File 'lib/copland/models/proxy.rb', line 91

def inspect
  "#<#{self.class.name}:#{"0x%08x"%self.id}:" +
  "service_point=>#{@service_point.full_name}:" +
  "instantiated=>#{@instance ? true : false}>"
end