Sha256: 6afb745ea46c176fb8743632eef513f0adc98af3369d0c570c6b16f8276ddaa8

Contents?: true

Size: 1.43 KB

Versions: 26

Compression:

Stored size: 1.43 KB

Contents

require_relative "base_registry"

module Foobara
  class Namespace
    class UnambiguousRegistry < BaseRegistry
      def registry
        @registry ||= {}
      end

      def register(scoped)
        new_entries = {}

        to_keys(scoped).each do |key|
          if registry.key?(key)
            raise WouldMakeRegistryAmbiguousError,
                  "Ambiguous match for #{key.inspect}. Matches the following: #{registry[key].inspect}"
          end

          new_entries[key] = scoped
        end

        all << scoped

        registry.merge!(new_entries)
      end

      def unregister(scoped)
        all.delete(scoped)

        to_keys(scoped).each do |key|
          unless registry.key?(key)
            raise NotRegisteredError, "Not registered: #{key.inspect}"
          end

          registry.delete(key)
        end
      end

      def lookup(path, filter = nil)
        apply_filter(registry[path], filter)
      end

      def each_scoped_without_filter(&)
        # records can be unregistered in the block so dup first
        all.dup.each(&)
      end

      private

      # TODO: why don't we do this in UnambiguousRegistry??
      def to_keys(scoped)
        short_name = scoped.scoped_short_name
        prefixes = scoped.scoped_prefix ? Util.power_set(scoped.scoped_prefix) : [[]]

        prefixes.map do |prefix|
          [*prefix, *short_name]
        end
      end

      def all
        @all ||= []
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.25 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.24 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.23 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.22 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.21 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.20 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.19 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.18 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.17 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.16 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.15 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.14 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.13 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.12 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.11 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.10 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.9 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.8 projects/namespace/src/unambiguous_registry.rb
foobara-0.0.7 projects/namespace/src/unambiguous_registry.rb