Sha256: 70449b6ee124818c338879e991c2764b1e66fdb49601ff397a536a93d5e5dfec

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module I18n::Tasks
  module Command
    module OptionParsers
      module Enum
        class Parser
          DEFAULT_ERROR = proc do |invalid, valid|
            I18n.t('i18n_tasks.cmd.enum_opt.invalid', invalid: invalid, valid: valid * ', ')
          end

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

          def call(value, *)
            return @valid.first unless value.present?
            if @valid.include?(value)
              value
            else
              fail CommandError, @error_message.call(value, @valid)
            end
          end
        end

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

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

          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
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-tasks-0.9.13 lib/i18n/tasks/command/option_parsers/enum.rb