class OptParseValidator::OptMultiChoices

Implementation of the MultiChoices Option

Public Class Methods

new(option, attrs = {}) click to toggle source

@param [ Array ] option See OptBase#new @param [ Hash ] attrs @option attrs [ Hash ] :choices @option attrs [ Array<Array> ] :incompatible @options attrs [ String ] :separator See OptArray#new

Calls superclass method
# File lib/opt_parse_validator/opts/multi_choices.rb, line 11
def initialize(option, attrs = {})
  raise Error, 'The :choices attribute is mandatory' unless attrs.key?(:choices)
  raise Error, 'The :choices attribute must be a hash' unless attrs[:choices].is_a?(Hash)

  super(option, attrs)
end

Public Instance Methods

append_choices_help_messages() click to toggle source
# File lib/opt_parse_validator/opts/multi_choices.rb, line 28
def append_choices_help_messages
  max_spaces = choices.keys.max_by(&:size).size

  choices.each do |key, opt|
    first_line_prefix  = " #{key} #{' ' * (max_spaces - key.length)}"
    other_lines_prefix = ' ' * first_line_prefix.size

    opt_help_messages(opt).each_with_index do |message, index|
      option << "#{index.zero? ? first_line_prefix : other_lines_prefix} #{message}"
    end
  end
end
append_help_messages() click to toggle source
# File lib/opt_parse_validator/opts/multi_choices.rb, line 18
def append_help_messages
  option << 'Available Choices:'

  append_choices_help_messages

  super

  append_incompatible_help_messages
end
append_incompatible_help_messages() click to toggle source
# File lib/opt_parse_validator/opts/multi_choices.rb, line 63
def append_incompatible_help_messages
  return if incompatible.empty?

  option << 'Incompatible choices (only one of each group/s can be used):'

  incompatible.each do |a|
    option << " - #{a.map(&:to_s).join(', ')}"
  end
end
help_message_for_default() click to toggle source
# File lib/opt_parse_validator/opts/multi_choices.rb, line 41
def help_message_for_default
  msg = +''

  default.each do |key, value|
    msg << if value == true
             key.to_s.titleize
           else
             "#{key.to_s.titleize}: #{value}"
           end
    msg << ', '
  end

  msg.chomp(', ')
end
incompatible() click to toggle source

@return [ Array<Array<Symbol>> ]

# File lib/opt_parse_validator/opts/multi_choices.rb, line 106
def incompatible
  Array(attrs[:incompatible])
end
normalize(value) click to toggle source

No normalization

# File lib/opt_parse_validator/opts/multi_choices.rb, line 131
def normalize(value)
  value
end
opt_help_messages(opt) click to toggle source

@param [ OptBase ] opt

@return [ Array<String> ]

# File lib/opt_parse_validator/opts/multi_choices.rb, line 59
def opt_help_messages(opt)
  opt.help_messages.empty? ? [opt.to_s.humanize] : opt.help_messages
end
validate(value) click to toggle source

@param [ String ] value

@return [ Hash ]

Calls superclass method OptParseValidator::OptArray#validate
# File lib/opt_parse_validator/opts/multi_choices.rb, line 76
def validate(value)
  results = {}

  super(value).each do |item|
    opt = choices[item.to_sym]

    if opt
      opt_value = opt.value_if_empty.nil? ? true : opt.value_if_empty
    else
      opt, opt_value = value_from_pattern(item)
    end

    results[opt.to_sym] = opt.normalize(opt.validate(opt_value))
  end

  verify_compatibility(results)
end
value_from_pattern(item) click to toggle source

@return [ Array ]

# File lib/opt_parse_validator/opts/multi_choices.rb, line 95
def value_from_pattern(item)
  choices.each do |key, opt|
    next unless item =~ /\A#{key}(.*)\z/

    return [opt, Regexp.last_match[1]]
  end

  raise Error, "Unknown choice: #{item}"
end
verify_compatibility(values) click to toggle source

@param [ Hash ] values

@return [ Hash ]

# File lib/opt_parse_validator/opts/multi_choices.rb, line 113
def verify_compatibility(values)
  incompatible.each do |a|
    last_match = ''

    a.each do |key|
      sym = choices[key].to_sym

      next unless values.key?(sym)

      raise Error, "Incompatible choices detected: #{last_match}, #{key}" unless last_match.empty?

      last_match = key
    end
  end
  values
end