Sha256: 0ae6a61b58a83c7e3b0d3f45cd1261577af52982970cc5f9d3d05fb45e095fe8

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module TTY
  class Prompt
    # Immutable collection of converters for type transformation
    #
    # @api private
    class ConverterRegistry
      # Create a registry of conversions
      #
      # @param [Hash] registry
      #
      # @api private
      def initialize(registry = {})
        @_registry = registry.dup.freeze
        freeze
      end

      # Register converter
      #
      # @param [Symbol] name
      #   the converter name
      #
      # @api public
      def register(name, contents = nil, &block)
        item = block_given? ? block : contents

        if key?(name)
          raise ArgumentError,
                "Converter for #{name.inspect} already registered"
        end
        self.class.new(@_registry.merge(name => item))
      end

      # Check if converter is registered
      #
      # @return [Boolean]
      #
      # @api public
      def key?(key)
        @_registry.key?(key)
      end

      # Execute converter
      #
      # @api public
      def call(name, input)
        if name.respond_to?(:call)
          converter = name
        else
          converter = @_registry.fetch(name) do
            raise ArgumentError, "#{name.inspect} is not registered"
          end
        end
        converter[input]
      end
      alias [] call

      def inspect
        @_registry.inspect
      end
    end # ConverterRegistry
  end # Prompt
end # TTY

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tty-prompt-0.17.2 lib/tty/prompt/converter_registry.rb
tty-prompt-0.17.1 lib/tty/prompt/converter_registry.rb
tty-prompt-0.17.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.16.1 lib/tty/prompt/converter_registry.rb
tty-prompt-0.16.0 lib/tty/prompt/converter_registry.rb