class Synapse::Configuration::Container

Container used for storing and resolving definitions of services @see ContainerBuilder

Public Class Methods

new() click to toggle source
# File lib/synapse/configuration/container.rb, line 6
def initialize
  @definitions = Hash.new
  @logger = Logging.logger[self.class]
end

Public Instance Methods

[](id, optional = false)
Alias for: resolve
inject_into(object) click to toggle source

Injects any configured dependencies into the given object

@param [Dependent] object @return [undefined]

# 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
inspect() click to toggle source

@return [String]

# 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) click to toggle source

Registers a service definition with this container

@param [Symbol] id @param [Definition] definition @return [undefined]

# 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) click to toggle source

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

@param [Symbol] id @return [Boolean]

# File lib/synapse/configuration/container.rb, line 78
def registered?(id)
  @definitions.has_key? id
end
resolve(id, optional = false) click to toggle source

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

@raise [ArgumentError] If definition could not be found and is required @param [Symbol] id @param [Boolean] optional Default is false @return [Object]

# 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
Also aliased as: []
resolve_tagged(tag) click to toggle source

Resolves any definitions that have the given tag

@param [Symbol] tag @return [Array]

# 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