Class: TTY2::Prompt::ConverterRegistry Private
- Inherits:
-
Object
- Object
- TTY2::Prompt::ConverterRegistry
- Extended by:
- Forwardable
- Defined in:
- lib/tty2/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
-
#[](name) ⇒ Object
(also: #fetch)
Execute converter.
-
#contain?(name) ⇒ Boolean
Check if conversion is available.
-
#initialize(registry = {}) ⇒ ConverterRegistry
constructor
private
Create a registry of conversions.
- #inspect ⇒ Object private
-
#register(*names, &block) ⇒ Object
Register a conversion.
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
20 21 22 |
# File 'lib/tty2/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/tty2/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
31 32 33 34 |
# File 'lib/tty2/prompt/converter_registry.rb', line 31 def contain?(name) conv_name = name.to_s.downcase.to_sym @__registry.key?(conv_name) 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.
64 65 66 |
# File 'lib/tty2/prompt/converter_registry.rb', line 64 def inspect @_registry.inspect end |
#register(*names, &block) ⇒ Object
Register a conversion
42 43 44 45 46 47 48 49 50 |
# File 'lib/tty2/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 |