Class: Copland::Instantiator::Complex

Inherits:
Abstract
  • Object
show all
Defined in:
lib/copland/instantiator/complex.rb

Overview

The Complex instantiator is a more sophisticated way of instantiating services. It provides for the wiring of dependencies into the new service “automatically” (based on specified dependencies). This instantiator is what allows Copland to claim support for “dependency injection.”

Instance Attribute Summary collapse

Attributes inherited from Abstract

#definition, #point

Instance Method Summary collapse

Methods inherited from Abstract

register_as

Constructor Details

#initialize(point, definition) ⇒ Complex

Create a new Complex instantiator for the given service point, using the given definition hash.



56
57
58
59
60
61
62
63
64
# File 'lib/copland/instantiator/complex.rb', line 56

def initialize( point, definition )
  super point, definition

  @factory_id = definition[ "factory" ] || "copland.BuilderFactory"
  @factory_id = @factory_id.value if @factory_id.respond_to?( :value )

  @construction_parms = definition.dup
  @construction_parms.delete "factory"
end

Instance Attribute Details

#construction_parmsObject (readonly)

The hash of parameters to pass to the instantiating factory.



49
50
51
# File 'lib/copland/instantiator/complex.rb', line 49

def construction_parms
  @construction_parms
end

#factory_pointObject (readonly)

The service point of the factory to use to instantiate the service.



52
53
54
# File 'lib/copland/instantiator/complex.rb', line 52

def factory_point
  @factory_point
end

Instance Method Details

#instantiateObject

Create a new instance of the service via the factory service. The constructor parameters are passed through the factory’s schema (if any) before passing them to the factory’s create_instance method.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/copland/instantiator/complex.rb', line 81

def instantiate
  @factory = @factory_point.instance unless @factory

  parms = @construction_parms
  schema = @factory_point.schema
  if schema.respond_to?( :process )
    parms = schema.process( @factory_point, point, parms )
  end
  
  @factory.create_instance( point, parms )
end

#validate!Object

Validates the parameters to this instantiator by running them through the schema of the associated factory service point (if any).



68
69
70
71
72
73
74
75
# File 'lib/copland/instantiator/complex.rb', line 68

def validate!
  @factory_point = point.find_service_point( @factory_id )

  schema = @factory_point.schema
  if schema.respond_to?( :validate )
    schema.validate @factory_point, point, construction_parms
  end
end