Sha256: 1fbf0ab5b3d5f50395cea46d6cdde88230757381a9f113567b923c279b4ddacd

Contents?: true

Size: 869 Bytes

Versions: 1

Compression:

Stored size: 869 Bytes

Contents

module Habu
  class Container
    attr_reader :factory

    def initialize
      @factory = Factory.new(self)
    end

    def [](key, &block)
      if block
        @factory.register(key, &block)
      else
        @factory.resolve(key)
      end
    end

    def new(klass, &block)
      params = klass.instance_method(:initialize).parameters
      klass.new(*params.filter_map { @1 == :req && self[@2] }, &block)
    end

    def to_refinements
      refinements = Module.new
      refinements.instance_exec(self) do |container|
        Habu.annotation_collector.constructor_annotations.each do |klass_name|
          klass = const_get(klass_name)
          refine(klass.singleton_class) do
            define_method(:new) do |&block|
              container.new(klass, &block)
            end
          end
        end
      end
      refinements
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
habu-0.2.0 lib/habu/container.rb