rosette-proj/rosette-core

View on GitHub
lib/rosette/core/commands/translations/with_locale.rb

Summary

Maintainability
A
0 mins
Test Coverage
# encoding: UTF-8

module Rosette
  module Core
    module Commands

      # Mixin that handles configuration and validation of a locale code.
      # Meant to be mixed into the classes in {Rosette::Core::Commands}.
      #
      # @example
      #   class MyCommand < Command
      #     include WithRepoName
      #     include WithLocale
      #   end
      #
      #   cmd = MyCommand.new(configuration)
      #     .set_repo_name('my_repo')
      #     .set_locale('ja-JP')
      #
      #   cmd.locale    # => 'ja-JP'
      #   cmd.valid?    # => true
      #
      #   cmd.set_locale('foobar')
      #   cmd.valid?    # => false
      #   cmd.messages  # => { locale: ["Repo 'my_repo' doesn't support the 'foobar' locale"] }
      module WithLocale
        attr_reader :locale

        # Sets the locale code.
        #
        # @param [String] locale_code
        # @return [self]
        def set_locale(locale_code)
          @locale = locale_code
          self
        end

        protected

        def self.included(base)
          base.validate :locale, type: :locale
        end
      end

    end
  end
end