Sha256: f53919969e92a7b1d604800831d9661207f8b2d5c78d7da35cfe32e66d0ec73f

Contents?: true

Size: 1.51 KB

Versions: 20

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

20 entries across 20 versions & 1 rubygems

Version Path
i18n-tasks-0.9.33 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.32 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.31 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.30 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.29 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.28 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.27 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.26 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.25 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.24 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.23 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.22 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.21 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.20 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.19 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.18 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.17 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.16 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.15 lib/i18n/tasks/command/option_parsers/enum.rb
i18n-tasks-0.9.14 lib/i18n/tasks/command/option_parsers/enum.rb