Sha256: 68d3b28ecfffb5ba73a2f94bbc71c600ac1deb40eb3843b604f5f2dcaa977d57

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Spank
  class ContainerError < Exception
    def intialize(message)
      @message = message
    end
  end

  class Container
    def initialize
      @items = {}
      register(:container) { self }
    end

    def register(key, &block)
      component = Component.new(key, &block)
      components_for(key).push(component)
      component
    end

    def resolve(key)
      instantiate(components_for(key).first, key)
    end

    def resolve_all(key)
      components_for(key).map {|item| instantiate(item, key) }
    end

    def build(type)
      try("I could not create: #{type}"){ build!(type) }
    end

    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

    private

    def components_for(key)
      @items[key] = [] unless @items[key]
      @items[key]
    end

    def instantiate(component, key)
      raise ContainerError.new("#{key} is not a registered component.") unless component
      component.create(self)
    end

    def try(error = nil, &lambda)
      begin
        lambda.call
      rescue => e
        raise Spank::ContainerError.new(error ||= "Oops: #{e}")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spank-0.0.1385937112 lib/spank/container.rb
spank-0.0.1369197619 lib/spank/container.rb
spank-0.0.1369197478 lib/spank/container.rb