Class: I18n::Tasks::Command::OptionParsers::Enum::ListParser

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/tasks/command/option_parsers/enum.rb

Constant Summary collapse

DEFAULT_ERROR =
proc do |invalid, valid|
  I18n.t('i18n_tasks.cmd.enum_list_opt.invalid', invalid: invalid * ', ', valid: valid * ', ')
end

Instance Method Summary collapse

Constructor Details

#initialize(valid, error_message = DEFAULT_ERROR) ⇒ ListParser

Returns a new instance of ListParser.



33
34
35
36
# File 'lib/i18n/tasks/command/option_parsers/enum.rb', line 33

def initialize(valid, error_message = DEFAULT_ERROR)
  @valid         = valid.map(&:to_s)
  @error_message = error_message
end

Instance Method Details

#call(values) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/i18n/tasks/command/option_parsers/enum.rb', line 38

def call(values, *)
  values = Array(values)
  return @valid if values == %w[all]

  invalid = values - @valid
  if invalid.empty?
    if values.empty?
      @valid
    else
      values
    end
  else
    fail CommandError, @error_message.call(invalid, @valid)
  end
end