class Spank::Container
Public Class Methods
new()
click to toggle source
# File lib/spank/container.rb, line 3 def initialize @items = {} register(:container) { self } end
Public Instance Methods
build(type)
click to toggle source
# File lib/spank/container.rb, line 22 def build(type) try("I could not create: #{type}"){ build!(type) } end
build!(type)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/spank/container.rb, line 14 def resolve(key) instantiate(components_for(key).first, key) end
resolve_all(key)
click to toggle source
# File lib/spank/container.rb, line 18 def resolve_all(key) components_for(key).map {|item| instantiate(item, key) } end
Private Instance Methods
components_for(key)
click to toggle source
# File lib/spank/container.rb, line 36 def components_for(key) @items[key] = [] unless @items[key] @items[key] end
instantiate(component, key)
click to toggle source
# File lib/spank/container.rb, line 41 def instantiate(component, key) raise ContainerError.new("#{key} is not a registered component.") unless component component.create(self) end
try(error = nil, &lambda)
click to toggle source
# File lib/spank/container.rb, line 46 def try(error = nil, &lambda) begin lambda.call rescue => e raise Spank::ContainerError.new(error ||= "Oops: #{e}") end end