Class: ParseArgv::Result::Value
- Inherits:
-
Object
- Object
- ParseArgv::Result::Value
- Includes:
- Comparable
- Defined in:
- lib/parse-argv.rb
Overview
This is a helper class to get parsed arguments from ParseArgv::Result to be converted.
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
The argument value.
-
#value? ⇒ Boolean
readonly
Whenever the value is nil and not false.
Instance Method Summary collapse
-
#as(type, *args, default: nil, **kwargs) ⇒ Object
Requests the value to be converted to a specified +type+.
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the argument value.
194 195 196 |
# File 'lib/parse-argv.rb', line 194 def value @value end |
#value? ⇒ Boolean (readonly)
Returns whenever the value is nil and not false.
206 207 208 |
# File 'lib/parse-argv.rb', line 206 def value? @value != nil && @value != false end |
Instance Method Details
#as(type, *args, default: nil, **kwargs) ⇒ Object
Requests the value to be converted to a specified +type+. It uses Conversion.[] to obtain the conversion procedure.
Some conversion procedures allow additional parameters which will be forwarded.
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/parse-argv.rb', line 236 def as(type, *args, default: nil, **kwargs) return default if @value.nil? conv = Conversion[type] if value.is_a?(Array) value.map { |v| conv.call(v, *args, **kwargs, &@error_proc) } else conv.call(@value, *args, **kwargs, &@error_proc) end rescue Error => e ParseArgv.on_error&.call(e) or raise end |