class Benry::Cmdopt::SchemaItem

Attributes

callback[R]
enum[R]
help[R]
key[R]
long[R]
optdef[R]
optional[R]
optional_param?[R]
param[R]
pattern[R]
short[R]
type[R]

Public Class Methods

new(key, optdef, short, long, param, help, optional: nil, type: nil, pattern: nil, enum: nil, &callback) click to toggle source
# File lib/benry/cmdopt.rb, line 306
def initialize(key, optdef, short, long, param, help, optional: nil, type: nil, pattern: nil, enum: nil, &callback)
  @key      = key       unless key.nil?
  @optdef   = optdef    unless optdef.nil?
  @short    = short     unless short.nil?
  @long     = long      unless long.nil?
  @param    = param     unless param.nil?
  @help     = help      unless help.nil?
  @optional = optional  unless optional.nil?
  @type     = type      unless type.nil?
  @pattern  = pattern   unless pattern.nil?
  @enum     = enum      unless enum.nil?
  @callback = callback  unless callback.nil?
end

Public Instance Methods

validate_and_convert(val, optdict) click to toggle source
# File lib/benry/cmdopt.rb, line 323
def validate_and_convert(val, optdict)
  #; [!h0s0o] raises RuntimeError when value not matched to pattern.
  if @pattern && val != true
    val =~ @pattern  or
      raise "pattern unmatched."
  end
  #; [!j4fuz] calls type-specific callback when type specified.
  if @type && val != true
    proc_ = PARAM_TYPES[@type]
    val = proc_.call(val)
  end
  #; [!5jrdf] raises RuntimeError when value not in enum.
  if @enum && val != true
    @enum.include?(val)  or
      raise "expected one of #{@enum.join('/')}."
  end
  #; [!jn9z3] calls callback when callback specified.
  #; [!iqalh] calls callback with different number of args according to arity.
  if @callback
    n_args = @callback.arity
    val = n_args == 1 ? @callback.call(val) \
                      : @callback.call(optdict, @key, val)
  end
  #; [!x066l] returns new value.
  return val
end