Sha256: 7928f00a91a691410e8d9d5c3f109242c8bf6a70bbb3ba07825876beeb42e466

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

# encoding: utf-8

module TTY
  class Prompt
    class ConverterRegistry
      def initialize
        @_registry = {}
      end

      # Register converter
      #
      # @api public
      def register(key, contents = nil, &block)
        if block_given?
          item = block
        else
          item = contents
        end

        if key?(key)
          fail ArgumentError, "Converter for #{key.inspect} already registered"
        else
          @_registry[key] = item
        end
        self
      end

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

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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tty-prompt-0.10.1 lib/tty/prompt/converter_registry.rb
tty-prompt-0.10.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.9.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.8.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.7.1 lib/tty/prompt/converter_registry.rb
tty-prompt-0.7.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.6.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.5.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.4.0 lib/tty/prompt/converter_registry.rb
tty-prompt-0.3.0 lib/tty/prompt/converter_registry.rb