Sha256: 1fd7c180e67f25a06f9c7f82cf86302df45d8f2c4a6e1d37067c0538f6e9a32f

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Anyway
  using RubyNext

  module Loaders
    class Registry
      attr_reader :registry

      def initialize
        @registry = []
      end

      def prepend(id, handler = nil, &block)
        handler ||= block
        insert_at(0, id, handler)
      end

      def append(id, handler = nil, &block)
        handler ||= block
        insert_at(registry.size, id, handler)
      end

      def insert_before(another_id, id, handler = nil, &block)
        ind = registry.find_index { |(hid, _)| hid == another_id }
        raise ArgumentError, "Loader with ID #{another_id} hasn't been registered" if ind.nil?

        handler ||= block
        insert_at(ind, id, handler)
      end

      def insert_after(another_id, id, handler = nil, &block)
        ind = registry.find_index { |(hid, _)| hid == another_id }
        raise ArgumentError, "Loader with ID #{another_id} hasn't been registered" if ind.nil?

        handler ||= block
        insert_at(ind + 1, id, handler)
      end

      def override(id, handler)
        find(id).then do |id_to_handler|
          raise ArgumentError, "Loader with ID #{id} hasn't been registered" if id_to_handler.nil?
          id_to_handler[1] = handler
        end
      end

      def delete(id)
        find(id).then do |id_to_handler|
          raise ArgumentError, "Loader with ID #{id} hasn't been registered" if id_to_handler.nil?
          registry.delete id_to_handler
        end
      end

      def each(&block)
        registry.each(&block)
      end

      def freeze
        registry.freeze
      end

      private

      def insert_at(index, id, handler)
        raise ArgumentError, "Loader with ID #{id} has been already registered" unless find(id).nil?

        registry.insert(index, [id, handler])
      end

      def find(id)
        registry.find { |(hid, _)| hid == id }
      end
    end
  end
end

require "anyway/loaders/base"
require "anyway/loaders/yaml"
require "anyway/loaders/env"

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
anyway_config-2.0.2 lib/anyway/loaders.rb
anyway_config-2.0.1 lib/anyway/loaders.rb
anyway_config-2.0.0 lib/anyway/loaders.rb
anyway_config-2.0.0.rc1 lib/anyway/loaders.rb