Class: TTY::Prompt::ConverterRegistry Private
- Inherits:
-
Object
- Object
- TTY::Prompt::ConverterRegistry
- 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
-
#call(name, input) ⇒ Object
(also: #[])
Execute converter.
-
#initialize(registry = {}) ⇒ ConverterRegistry
constructor
private
Create a registry of conversions.
- #inspect ⇒ Object private
-
#key?(key) ⇒ Boolean
Check if converter is registered.
-
#register(name, contents = nil, &block) ⇒ Object
Register converter.
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
14 15 16 17 |
# File 'lib/tty/prompt/converter_registry.rb', line 14 def initialize(registry = {}) @_registry = registry.dup.freeze freeze end |
Instance Method Details
#call(name, input) ⇒ Object Also known as: []
Execute converter
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tty/prompt/converter_registry.rb', line 47 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 |
#inspect ⇒ Object
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.
59 60 61 |
# File 'lib/tty/prompt/converter_registry.rb', line 59 def inspect @_registry.inspect end |
#key?(key) ⇒ Boolean
Check if converter is registered
40 41 42 |
# File 'lib/tty/prompt/converter_registry.rb', line 40 def key?(key) @_registry.key?(key) end |
#register(name, contents = nil, &block) ⇒ Object
Register converter
25 26 27 28 29 30 31 32 33 |
# File 'lib/tty/prompt/converter_registry.rb', line 25 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 |