A "service point" is a definition of a service. Just as a class defines the behavior of an object, so does a service point describe a service. In particular, a service point also knows how to instantiate a service.
A ServicePoint should never be directly instantiated. Instead, define services via the interfaces provided by Container.
[R] | container | A reference to the container that contains this service point. |
[R] | name | The name of this service point, as it is known to the container that it was registered in. |
[R] | pipeline | The reference to the instantiation pipeline used by this service point. |
Create a new service point that references the given container and has the given name. The associated callback will be used to instantiate the service on demand.
The :model option is used to tell Needle which style of life-cycle management should be used for the service. It defaults to :singleton. The model must be a symbol that refers to a service model that has been registered in the root :service_models service.
The :pipeline option is mutually exclusive with :model. It must be an array of symbols (or strings) that define the instantiation pipeline to use for this service. Each element must correspond to an entry in the :pipeline_elements service.
[ show source ]
# File lib/needle/service-point.rb, line 54 54: def initialize( container, name, opts={}, &callback ) 55: @name = name 56: @container = container 57: @callback = callback 58: @pipeline = Needle::Pipeline::Collection.new self 59: @chain = nil 60: 61: @chain_mutex = QueryableMutex.new 62: @element_mutex = QueryableMutex.new 63: 64: if opts[:pipeline] 65: elements = opts[:pipeline] 66: else 67: model = opts[:model] || :singleton 68: elements = @container[:service_models][model] 69: end 70: 71: elements.concat [ *opts[:include] ] if opts[:include] 72: elements.each { |element| @pipeline.add element, opts } 73: end
Returns the fully-qualified name of the service point, with the point’s name, its container’s name, and all of its container’s ancestors’ names concatenated together with dot characters, i.e. "one.two.three".
[ show source ]
# File lib/needle/service-point.rb, line 78 78: def fullname 79: container_name = @container.fullname 80: if container_name 81: "#{container_name}.#{@name}" 82: else 83: @name.to_s 84: end 85: end
Return the service instance represented by this service point. Depending on the style of lifecycle management chosen for this service point, this may or may not be a new instance for every invocation of this method.
Any arguments to this method will be passed through to the chain, which may cause an error if there is an element in the pipeline that does not accept additional arguments. Regardless, the first two parameters to the chain will always be the container and the service point.
[ show source ]
# File lib/needle/service-point.rb, line 110 110: def instance( *args ) 111: unless @chain 112: @chain_mutex.synchronize do 113: @chain = @pipeline.chain_to( @callback ) unless @chain 114: end 115: end 116: 117: @chain.call( @container, self, *args ) 118: end
Adds the given interceptor definition to this service point. The parameter should act like an instance of Interceptor.
[ show source ]
# File lib/needle/service-point.rb, line 89 89: def interceptor( interceptor ) 90: @element_mutex.synchronize do 91: element = @pipeline.get( :interceptor ) 92: unless element 93: @pipeline.add( :interceptor ) 94: element = @pipeline.get( :interceptor ) 95: end 96: element.interceptors << interceptor 97: @pipeline.reset! 98: @chain = nil 99: end 100: end