module TTY::Option::Converter

Public Instance Methods

[](name) click to toggle source

Retrieve a conversion type

@param [String] name

@return [Proc]

@api public

# File lib/tty/option/converter.rb, line 48
def [](name)
  conv_name = name.to_s.downcase.to_sym
  conversions.fetch(conv_name) { raise_unsupported_error(conv_name) }
end
Also aliased as: fetch
contain?(name) click to toggle source

Check if conversion is available

@param [String] name

@return [Boolean]

@api public

# File lib/tty/option/converter.rb, line 20
def contain?(name)
  conv_name = name.to_s.downcase.to_sym
  conversions.key?(conv_name)
end
conversions() click to toggle source

Store conversions

@api public

# File lib/tty/option/converter.rb, line 9
def conversions
  @conversions ||= {}
end
convert(*names, &block) click to toggle source

Register a new conversion type

@example

convert(:int) { |val| Float(val).to_i }

@api public

# File lib/tty/option/converter.rb, line 31
def convert(*names, &block)
  names.each do |name|
    if contain?(name)
      raise ConversionAlreadyDefined,
            "conversion #{name.inspect} is already defined"
    end
    conversions[name] = block
  end
end
fetch(name)
Alias for: []
raise_unsupported_error(conv_name) click to toggle source

Raise an error for unknown conversion type

@api public

# File lib/tty/option/converter.rb, line 57
def raise_unsupported_error(conv_name)
  raise UnsupportedConversion,
        "unsupported conversion type #{conv_name.inspect}"
end