Class: Spank::Container
- Inherits:
-
Object
- Object
- Spank::Container
- Defined in:
- lib/spank/container.rb
Instance Method Summary collapse
- #build(type) ⇒ Object
- #build!(type) ⇒ Object
-
#initialize ⇒ Container
constructor
A new instance of Container.
- #register(key, &block) ⇒ Object
- #resolve(key) ⇒ Object
- #resolve_all(key) ⇒ Object
Constructor Details
#initialize ⇒ Container
Returns a new instance of Container.
3 4 5 6 |
# File 'lib/spank/container.rb', line 3 def initialize @items = {} register(:container) { self } end |
Instance Method Details
#build(type) ⇒ Object
22 23 24 |
# File 'lib/spank/container.rb', line 22 def build(type) try("I could not create: #{type}"){ build!(type) } end |
#build!(type) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/spank/container.rb', line 26 def build!(type) constructor = type.instance_method('initialize') parameters = constructor.parameters.map do |req, parameter| resolve(parameter.to_sym) end type.send(:new, *parameters) end |
#register(key, &block) ⇒ Object
8 9 10 11 12 |
# File 'lib/spank/container.rb', line 8 def register(key, &block) component = Component.new(key, &block) components_for(key).push(component) component end |
#resolve(key) ⇒ Object
14 15 16 |
# File 'lib/spank/container.rb', line 14 def resolve(key) instantiate(components_for(key).first, key) end |
#resolve_all(key) ⇒ Object
18 19 20 |
# File 'lib/spank/container.rb', line 18 def resolve_all(key) components_for(key).map {|item| instantiate(item, key) } end |