Class: I18n::Tasks::Command::OptionParsers::Enum::Parser

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_opt.invalid', invalid: invalid, valid: valid * ', ')
end

Instance Method Summary collapse

Constructor Details

#initialize(valid, error_message = DEFAULT_ERROR) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
# File 'lib/i18n/tasks/command/option_parsers/enum.rb', line 12

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

Instance Method Details

#call(value) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/i18n/tasks/command/option_parsers/enum.rb', line 17

def call(value, *)
  return @valid.first unless value.present?

  if @valid.include?(value)
    value
  else
    fail CommandError, @error_message.call(value, @valid)
  end
end