Sha256: b0851c9be3131f5e96718670725b4a5a8360e246f0bf02741d5616a036132b03

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true
module I18n::Tasks
  module Command
    module OptionParsers
      module Locale
        module Validator
          VALID_LOCALE_RE = /\A\w[\w\-\.]*\z/i

          def validate!(locale)
            if VALID_LOCALE_RE !~ locale
              raise CommandError.new(I18n.t('i18n_tasks.cmd.errors.invalid_locale', invalid: locale))
            end
            locale
          end
        end

        module Parser
          module_function
          extend Validator

          # @param [#base_locale, #locales] context
          def call(val, context)
            if val.blank? || val == 'base'
              context.base_locale
            else
              validate! val
            end
          end
        end

        module ListParser
          module_function
          extend Validator

          # @param [#base_locale,#locales] context
          def call(vals, context)
            if vals == %w(all) || vals.blank?
              context.locales
            else
              move_base_locale_to_front! vals.map { |v| v == 'base' ? context.base_locale : v }, context.base_locale
            end.tap do |locales|
              locales.each { |locale| validate! locale }
            end
          end

          def move_base_locale_to_front!(locales, base_locale)
            if (pos = locales.index(base_locale)) && pos > 0
              locales[pos], locales[0] = locales[0], locales[pos]
            end
            locales
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i18n-tasks-0.9.6 lib/i18n/tasks/command/option_parsers/locale.rb
i18n-tasks-0.9.5 lib/i18n/tasks/command/option_parsers/locale.rb
i18n-tasks-0.9.4 lib/i18n/tasks/command/option_parsers/locale.rb
i18n-tasks-0.9.3 lib/i18n/tasks/command/option_parsers/locale.rb