Class: Synapse::Configuration::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/configuration/container.rb

Overview

Container used for storing and resolving definitions of services

See Also:

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



6
7
8
9
# File 'lib/synapse/configuration/container.rb', line 6

def initialize
  @definitions = Hash.new
  @logger = Logging.logger[self.class]
end

Instance Method Details

#inject_into(object) ⇒ undefined

Injects any configured dependencies into the given object

Parameters:

Returns:

  • (undefined)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/synapse/configuration/container.rb', line 15

def inject_into(object)
  return unless object.is_a? Dependent

  dependencies = object.class.dependencies
  dependencies.each_pair do |id, attribute|
    # Don't replace existing attributes
    next if object.public_send attribute

    resolved = resolve id
    object.public_send "#{attribute}=", resolved
  end
end

#inspectString

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
92
# File 'lib/synapse/configuration/container.rb', line 83

def inspect
  result = "#<#{self.class}\n"
  @definitions.keys.sort.each do |key|
    definition = @definitions[key]
    result << "\t#{key} => #{definition.tags.inspect}\n"
  end
  result << ">"

  result
end

#register(id, definition) ⇒ undefined

Registers a service definition with this container

Parameters:

Returns:

  • (undefined)


66
67
68
69
70
71
72
# File 'lib/synapse/configuration/container.rb', line 66

def register(id, definition)
  if @definitions.has_key? id
    @logger.info 'Definition [%s] is being replaced' % id
  end

  @definitions.store id, definition
end

#registered?(id) ⇒ Boolean

Returns true if a service definition with the given identifier is registered

Parameters:

  • id (Symbol)

Returns:

  • (Boolean)


78
79
80
# File 'lib/synapse/configuration/container.rb', line 78

def registered?(id)
  @definitions.has_key? id
end

#resolve(id, optional = false) ⇒ Object Also known as: []

Locates the definition for a service with the given identifier and resolves it to the object being provided

Parameters:

  • id (Symbol)
  • optional (Boolean) (defaults to: false)

    Default is false

Returns:

  • (Object)

Raises:

  • (ArgumentError)

    If definition could not be found and is required



35
36
37
38
39
40
41
# File 'lib/synapse/configuration/container.rb', line 35

def resolve(id, optional = false)
  if @definitions.has_key? id
    @definitions[id].resolve
  elsif not optional
    raise ConfigurationError, 'Definition for service [%s] not found' % id
  end
end

#resolve_tagged(tag) ⇒ Array

Resolves any definitions that have the given tag

Parameters:

  • tag (Symbol)

Returns:

  • (Array)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/synapse/configuration/container.rb', line 49

def resolve_tagged(tag)
  resolved = Array.new

  @definitions.each_value do |definition|
    if definition.tags and definition.tags.include? tag
      resolved.push definition.resolve
    end
  end

  resolved
end