File: dim_without_comments.rb

Project: DIM: Dependency Injection / Minimal

module DIM
  class MissingServiceError < StandardError; end

  class DuplicateServiceError < StandardError; end

  class Container
    def initialize(parent=nil)
      @services = {}
      @cache = {}
      @parent = parent || Container
    end

    def register(name, &block)
      if @services[name]
        fail DuplicateServiceError, "Duplicate Service Name '#{name}'"
      end
      @services[name] = block
    end

    def [](name)
      @cache[name] ||= service_block(name).call(self)
    end

    def method_missing(sym, *args, &block)
      self[sym]
    end

    def service_block(name)
      @services[name] || @parent.service_block(name)
    end

    def self.service_block(name)
      fail(MissingServiceError, "Unknown Service '#{name}'")
    end
  end
end


[ Index ][ Presentation ]
Generated by [ source2html ]