Sha256: c2c8583b391977d86311ed6e42fda8a1d76fbf649eaff51f123897250228af8d

Contents?: true

Size: 951 Bytes

Versions: 5

Compression:

Stored size: 951 Bytes

Contents

module Dry
  module AutoInject
    DuplicateDependencyError = Class.new(StandardError)

    class DependencyMap
      def initialize(*dependencies)
        @map = {}

        dependencies = dependencies.dup
        aliases = dependencies.last.is_a?(Hash) ? dependencies.pop : {}

        dependencies.each do |identifier|
          name = identifier.to_s.split(".").last
          add_dependency(name, identifier)
        end

        aliases.each do |name, identifier|
          add_dependency(name, identifier)
        end
      end

      def inspect
        @map.inspect
      end

      def names
        @map.keys
      end

      def to_h
        @map.dup.to_h
      end
      alias_method :to_hash, :to_h

      private

      def add_dependency(name, identifier)
        name = name.to_sym
        raise DuplicateDependencyError, "name +#{name}+ is already used" if @map.key?(name)
        @map[name] = identifier
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-auto_inject-0.4.3 lib/dry/auto_inject/dependency_map.rb
dry-auto_inject-0.4.2 lib/dry/auto_inject/dependency_map.rb
dry-auto_inject-0.4.1 lib/dry/auto_inject/dependency_map.rb
dry-auto_inject-0.4.0 lib/dry/auto_inject/dependency_map.rb
dry-auto_inject-0.3.0 lib/dry/auto_inject/dependency_map.rb