Class: IoC::Container
- Includes:
- Singleton
- Defined in:
- lib/ioc/container.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Container
constructor
A new instance of Container.
- #register(name, value = nil, &block) ⇒ Object
- #retrieve(name) ⇒ Object
Constructor Details
#initialize ⇒ Container
Returns a new instance of Container.
10 11 12 13 |
# File 'lib/ioc/container.rb', line 10 def initialize @depedencies = Hash.new @mutex = Mutex.new end |
Class Method Details
.init {|instance| ... } ⇒ Object
15 16 17 |
# File 'lib/ioc/container.rb', line 15 def self.init yield(instance) end |
Instance Method Details
#register(name, value = nil, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ioc/container.rb', line 19 def register(name, value = nil, &block) raise ArgumentError, "WTF dude?" unless name && (value || block) synchronized do dependency = if block_given? Dependency.new(block) else Dependency.new(value, singleton: true) end depedencies[name] = dependency end end |
#retrieve(name) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/ioc/container.rb', line 33 def retrieve(name) synchronized do dependency = depedencies.fetch(name) dependency.call end rescue KeyError raise DependencyNotFound, "Dependency not found" end |