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
The key under which this parameter is registered
@api public
Public Class Methods
A parameter factory
@api public
# File lib/tty/option/parameter.rb, line 23 def self.create(key, **settings, &block) new(key, **settings, &block) end
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
Compare this parameter name with the other
@api public
# File lib/tty/option/parameter.rb, line 203 def <=>(other) name <=> other.name end
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
# 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
# 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
# File lib/tty/option/parameter.rb, line 96 def convert? @settings.key?(:convert) && !@settings[:convert].nil? end
# 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
# File lib/tty/option/parameter.rb, line 109 def default? @settings.key?(:default) && !@settings[:default].nil? end
# File lib/tty/option/parameter.rb, line 68 def default_arity 1 end
# File lib/tty/option/parameter.rb, line 180 def default_name key.to_s.tr("_", "-") end
# 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
# File lib/tty/option/parameter.rb, line 121 def desc? @settings.key?(:desc) && !@settings[:desc].nil? end
# File lib/tty/option/parameter.rb, line 156 def display? desc? && !hidden? end
Make a duplicate of this parameter
@api public
# 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
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
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
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
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
# 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
# File lib/tty/option/parameter.rb, line 132 def optional @settings[:required] = false end
# File lib/tty/option/parameter.rb, line 136 def optional? !required? end
# 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
# File lib/tty/option/parameter.rb, line 168 def permit? @settings.key?(:permit) && !@settings[:permit].nil? end
# File lib/tty/option/parameter.rb, line 140 def required @settings[:required] = check_required(true) end
# File lib/tty/option/parameter.rb, line 144 def required? @settings.fetch(:required, false) end
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
# File lib/tty/option/parameter.rb, line 196 def to_sym self.class.name.to_s.split(/::/).last.downcase.to_sym end
# 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
# File lib/tty/option/parameter.rb, line 192 def validate? @settings.key?(:validate) && !@settings[:validate].nil? end
Private Instance Methods
@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
# 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
@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
@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
@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