Class: Synapse::Configuration::Container
- Inherits:
-
Object
- Object
- Synapse::Configuration::Container
- Defined in:
- lib/synapse/configuration/container.rb
Overview
Container used for storing and resolving definitions of services
Instance Method Summary collapse
-
#initialize ⇒ Container
constructor
A new instance of Container.
-
#inject_into(object) ⇒ undefined
Injects any configured dependencies into the given object.
- #inspect ⇒ String
-
#register(id, definition) ⇒ undefined
Registers a service definition with this container.
-
#registered?(id) ⇒ Boolean
Returns true if a service definition with the given identifier is registered.
-
#resolve(id, optional = false) ⇒ Object
(also: #[])
Locates the definition for a service with the given identifier and resolves it to the object being provided.
-
#resolve_tagged(tag) ⇒ Array
Resolves any definitions that have the given tag.
Constructor Details
#initialize ⇒ Container
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
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 |
#inspect ⇒ 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..inspect}\n" end result << ">" result end |
#register(id, definition) ⇒ undefined
Registers a service definition with this container
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
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
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
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. and definition..include? tag resolved.push definition.resolve end end resolved end |