Class: TTY::Prompt::ConverterRegistry Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tty/prompt/converter_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Immutable collection of converters for type transformation

Instance Method Summary collapse

Constructor Details

#initialize(registry = {}) ⇒ ConverterRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a registry of conversions

Parameters:

  • registry (Hash) (defaults to: {})

20
21
22
# File 'lib/tty/prompt/converter_registry.rb', line 20

def initialize(registry = {})
  @__registry = registry.dup
end

Instance Method Details

#[](name) ⇒ Object Also known as: fetch

Execute converter


55
56
57
58
59
60
61
# File 'lib/tty/prompt/converter_registry.rb', line 55

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

#contain?(name) ⇒ Boolean

Check if conversion is available

Parameters:

  • name (String)

Returns:

  • (Boolean)

31
32
33
34
# File 'lib/tty/prompt/converter_registry.rb', line 31

def contain?(name)
  conv_name = name.to_s.downcase.to_sym
  @__registry.key?(conv_name)
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


64
65
66
# File 'lib/tty/prompt/converter_registry.rb', line 64

def inspect
  @_registry.inspect
end

#register(*names, &block) ⇒ Object

Register a conversion

Parameters:

  • name (Symbol)

    the converter name


42
43
44
45
46
47
48
49
50
# File 'lib/tty/prompt/converter_registry.rb', line 42

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