Sha256: 1d5785df0858443a06f1bc46d29c6fe816d3330bc0f51c91fa250ab0df3898f1

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require "forwardable"

module TTY
  class Prompt
    # Immutable collection of converters for type transformation
    #
    # @api private
    class ConverterRegistry
      extend Forwardable

      def_delegators "@__registry", :keys

      # Create a registry of conversions
      #
      # @param [Hash] registry
      #
      # @api private
      def initialize(registry = {})
        @__registry = registry.dup
      end

      # Check if conversion is available
      #
      # @param [String] name
      #
      # @return [Boolean]
      #
      # @api public
      def contain?(name)
        conv_name = name.to_s.downcase.to_sym
        @__registry.key?(conv_name)
      end

      # Register a conversion
      #
      # @param [Symbol] name
      #   the converter name
      #
      # @api public
      def register(*names, &block)
        names.each do |name|
          if contain?(name)
            raise ConversionAlreadyDefined,
                  "converter for #{name.inspect} is already registered"
          end
          @__registry[name] = block
        end
      end

      # Execute converter
      #
      # @api public
      def [](name)
        conv_name = name.to_s.downcase.to_sym
        @__registry.fetch(conv_name) do
          raise UnsupportedConversion,
                "converter #{conv_name.inspect} is not registered"
        end
      end
      alias fetch []

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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
tty-prompt-0.23.1 lib/tty/prompt/converter_registry.rb
tty-prompt-0.23.0 lib/tty/prompt/converter_registry.rb
lotrd-0.1.9 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/converter_registry.rb
lotrd-0.1.8 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/converter_registry.rb
lotrd-0.1.6 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/converter_registry.rb
lotrd-0.1.5 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/converter_registry.rb
tty-prompt-0.22.0 lib/tty/prompt/converter_registry.rb