Module: TTY::Option::ParamConversion

Defined in:
lib/tty/option/param_conversion.rb

Class Method Summary collapse

Class Method Details

.[]Object

Convert parameter value to another type

Examples:

param = Parameter::Argument.create(:foo, convert: :int)
result = ParamConversion[param, "12"]
result.value # => 12


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tty/option/param_conversion.rb', line 32

def call(param, value)
  return Result.success(value) if !param.convert? || value.nil?

  cast = param.convert
  cast = cast.is_a?(Proc) ? cast : Conversions[cast]
  converted = cast.(value)

  if converted == Const::Undefined
    Result.failure(InvalidConversionArgument.new(param, value))
  else
    Result.success(converted)
  end
end

.call(param, value) ⇒ Object

Convert parameter value to another type

Examples:

param = Parameter::Argument.create(:foo, convert: :int)
result = ParamConversion[param, "12"]
result.value # => 12


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tty/option/param_conversion.rb', line 17

def call(param, value)
  return Result.success(value) if !param.convert? || value.nil?

  cast = param.convert
  cast = cast.is_a?(Proc) ? cast : Conversions[cast]
  converted = cast.(value)

  if converted == Const::Undefined
    Result.failure(InvalidConversionArgument.new(param, value))
  else
    Result.success(converted)
  end
end