class TTY::Option::Parameter

Constants

ONE_OR_MORE_ARITY

One or more parameter arity pattern

ZERO_OR_MORE_ARITY

Zero or more parameter arity pattern

Attributes

key[R]

The key under which this parameter is registered

@api public

Public Class Methods

create(key, **settings, &block) click to toggle source

A parameter factory

@api public

# File lib/tty/option/parameter.rb, line 23
def self.create(key, **settings, &block)
  new(key, **settings, &block)
end
new(key, **settings, &block) click to toggle source

Create a parameter

@param [Symbol] key

the key to register this param under

@api private

# File lib/tty/option/parameter.rb, line 38
def initialize(key, **settings, &block)
  @key = key
  @settings = {}
  settings.each do |name, val|
    case name.to_sym
    when :arity
      val = check_arity(val)
    when :default
      val = check_default(val)
    when :optional
      name, val = :required, check_required(!val)
    when :permit
      val = check_permitted(val)
    when :validate
      val = check_validation(val)
    end
    @settings[name.to_sym] = val
  end

  instance_eval(&block) if block_given?
end

Public Instance Methods

<=>(other) click to toggle source

Compare this parameter name with the other

@api public

# File lib/tty/option/parameter.rb, line 203
def <=>(other)
  name <=> other.name
end
==(other) click to toggle source

Compare parameters for equality based on type and name

@api public

# File lib/tty/option/parameter.rb, line 210
def ==(other)
  return false unless instance_of?(other.class)

  name == other.name && to_h == other.to_h
end
arity(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 60
def arity(value = (not_set = true))
  if not_set
    @settings.fetch(:arity) { default_arity }
  else
    @settings[:arity] = check_arity(value)
  end
end
convert(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 88
def convert(value = (not_set = true))
  if not_set
    @settings[:convert]
  else
    @settings[:convert] = value
  end
end
convert?() click to toggle source
# File lib/tty/option/parameter.rb, line 96
def convert?
  @settings.key?(:convert) && !@settings[:convert].nil?
end
default(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 100
def default(value = (not_set = true))
  if not_set
    @settings[:default]
  else
    @settings[:default] = check_default(value)
  end
end
Also aliased as: defaults
default?() click to toggle source
# File lib/tty/option/parameter.rb, line 109
def default?
  @settings.key?(:default) && !@settings[:default].nil?
end
default_arity() click to toggle source
# File lib/tty/option/parameter.rb, line 68
def default_arity
  1
end
default_name() click to toggle source
# File lib/tty/option/parameter.rb, line 180
def default_name
  key.to_s.tr("_", "-")
end
defaults(value = (not_set = true))
Alias for: default
desc(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 113
def desc(value = (not_set = true))
  if not_set
    @settings[:desc]
  else
    @settings[:desc] = value
  end
end
desc?() click to toggle source
# File lib/tty/option/parameter.rb, line 121
def desc?
  @settings.key?(:desc) && !@settings[:desc].nil?
end
display?() click to toggle source
# File lib/tty/option/parameter.rb, line 156
def display?
  desc? && !hidden?
end
dup() click to toggle source

Make a duplicate of this parameter

@api public

Calls superclass method
# File lib/tty/option/parameter.rb, line 244
def dup
  super.tap do |param|
    param.instance_variable_set(:@key, DeepDup.deep_dup(@key))
    param.instance_variable_set(:@settings, DeepDup.deep_dup(@settings))
  end
end
eql?(other) click to toggle source

Compare parameters for equality based on type and name

@api public

# File lib/tty/option/parameter.rb, line 219
def eql?(other)
  return false unless instance_of?(other.class)

  name.eql?(other.name) && to_h.eql?(other.to_h)
end
hidden() click to toggle source
# File lib/tty/option/parameter.rb, line 148
def hidden
  @settings[:hidden] = true
end
hidden?() click to toggle source
# File lib/tty/option/parameter.rb, line 152
def hidden?
  @settings.fetch(:hidden, false)
end
min_arity() click to toggle source

Determine minimum boundary for arity parameter

@api private

# File lib/tty/option/parameter.rb, line 75
def min_arity
  arity < 0 ? arity.abs - 1 : arity
end
multi_argument?() click to toggle source

Check if this options is multi argument

@api public

# File lib/tty/option/parameter.rb, line 128
def multi_argument?
  !convert.to_s.match(/list|array|map|hash|\w+s/).nil?
end
multiple?() click to toggle source

Check if multiple occurrences are allowed

@return [Boolean]

@api public

# File lib/tty/option/parameter.rb, line 84
def multiple?
  arity < 0 || 1 < arity.abs
end
name(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 172
def name(value = (not_set = true))
  if not_set
    @settings.fetch(:name) { default_name }
  else
    @settings[:name] = value
  end
end
optional() click to toggle source
# File lib/tty/option/parameter.rb, line 132
def optional
  @settings[:required] = false
end
optional?() click to toggle source
# File lib/tty/option/parameter.rb, line 136
def optional?
  !required?
end
permit(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 160
def permit(value = (not_set = true))
  if not_set
    @settings[:permit]
  else
    @settings[:permit] = check_permitted(value)
  end
end
permit?() click to toggle source
# File lib/tty/option/parameter.rb, line 168
def permit?
  @settings.key?(:permit) && !@settings[:permit].nil?
end
required() click to toggle source
# File lib/tty/option/parameter.rb, line 140
def required
  @settings[:required] = check_required(true)
end
required?() click to toggle source
# File lib/tty/option/parameter.rb, line 144
def required?
  @settings.fetch(:required, false)
end
to_h(&block) click to toggle source

Return a hash of this parameter settings

@return [Hash] the names and values of this parameter

@api public

# File lib/tty/option/parameter.rb, line 230
def to_h(&block)
  if block_given?
    @settings.each_with_object({}) do |(key, val), acc|
      k, v = *block.(key, val)
      acc[k] = v
    end
  else
    DeepDup.deep_dup(@settings)
  end
end
to_sym() click to toggle source
# File lib/tty/option/parameter.rb, line 196
def to_sym
  self.class.name.to_s.split(/::/).last.downcase.to_sym
end
validate(value = (not_set = true)) click to toggle source
# File lib/tty/option/parameter.rb, line 184
def validate(value = (not_set = true))
  if not_set
    @settings[:validate]
  else
    @settings[:validate] = check_validation(value)
  end
end
validate?() click to toggle source
# File lib/tty/option/parameter.rb, line 192
def validate?
  @settings.key?(:validate) && !@settings[:validate].nil?
end

Private Instance Methods

check_arity(value) click to toggle source

@api private

# File lib/tty/option/parameter.rb, line 254
def check_arity(value)
  if value.nil?
    raise ConfigurationError,
          "#{to_sym} '#{name}' arity needs to be an Integer"
  end

  case value.to_s
  when ZERO_OR_MORE_ARITY then -1
  when ONE_OR_MORE_ARITY then -2
  else value.to_i
  end.tap do |val|
    if val.zero?
      raise ConfigurationError, "#{to_sym} '#{name}' arity cannot be zero"
    end
  end
end
check_default(value) click to toggle source
# File lib/tty/option/parameter.rb, line 281
def check_default(value)
  if !value.nil? && required?
    raise ConfigurationError,
          "#{to_sym} '#{name}' cannot have default value and be required"
  else
    value
  end
end
check_permitted(value) click to toggle source

@api private

# File lib/tty/option/parameter.rb, line 272
def check_permitted(value)
  if value.respond_to?(:include?)
    value
  else
    raise ConfigurationError,
          "#{to_sym} '#{name}' permitted value needs to be an Array"
  end
end
check_required(value) click to toggle source

@api private

# File lib/tty/option/parameter.rb, line 291
def check_required(value)
  if value && default?
    raise ConfigurationError,
          "#{to_sym} '#{name}' cannot be required and have default value"
  else
    value
  end
end
check_validation(value) click to toggle source

@api private

# File lib/tty/option/parameter.rb, line 301
def check_validation(value)
  case value
  when NilClass
    raise ConfigurationError,
          "#{to_sym} '#{name}' validation needs to be a Proc or a Regexp"
  when Proc
    value
  when Regexp, String
    Regexp.new(value.to_s)
  else
    raise ConfigurationError,
          "#{to_sym} '#{name}' validation can only be a Proc or a Regexp"
  end
end