Class: Copland::Implementation::BuilderFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/copland/impl/builder-factory.rb

Overview

This is the implementation of the BuilderFactory service. It constructs service implementations and wires services together automatically, as determined by their dependencies in the configuration files.

Constant Summary collapse

SETTERS =

This is the list of patterns that are recognized as “setter” methods. These are used when determining how to set properties.

[ "%s=", "set_%s" ]

Instance Method Summary collapse

Instance Method Details

#create_instance(point, parms) ⇒ Object

This is the factory method used to construct new services. Based on the parms argument, the new service is instantiated, initialized, and returned.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/copland/impl/builder-factory.rb', line 53

def create_instance( point, parms )
  ensure_log( point )

  klass = Copland::get_class( parms['class'] )

  args = ( parms['parameters'] || [] )

  if klass.include?( Singleton )
    if args.length > 0
      raise CoplandException,
        "Singleton instances cannot have constructor parameters"
    end
    obj = klass.instance
  else
    obj = klass.new( *args )
  end

  if parms['properties']
    parms['properties'].each_pair do |name, value|
      catch( :property_set ) do
        SETTERS.each do |pattern|
          setter = pattern % name
          if obj.respond_to?( setter )
            obj.__send__( setter, value )
            throw :property_set
          end
        end

        @log.warn "no setter for #{name.inspect} found on " +
                  "#{klass} (#{point.full_name})"
        obj.instance_variable_set "@#{name}".intern, value
      end
    end
  end

  if parms['invoke']
    parms['invoke'].each_pair do |name, value|
      obj.__send__( name, *( value || [] ) )
    end
  end

  initialize_method = parms[ 'initialize-method' ] ||
    "initialize_service"

  if obj.respond_to?( initialize_method )
    obj.__send__( initialize_method )
  end

  return obj
end

#ensure_log(point) ⇒ Object



104
105
106
107
# File 'lib/copland/impl/builder-factory.rb', line 104

def ensure_log( point )
  return if @log
  @log = point.owner.registry.logs.get( "copland.BuilderFactory" )
end