Sha256: 807f028975ad76e568d0264044223ba281023b525489efc13c026d413e9bcd8a

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Mayl
  module Commands
    # Public: The Get command accepts a key and returns a summary of the
    # values for that key in every locale.
    #
    # Example
    # 
    #   command = Get.new(env, 'activerecord.models.post')
    #   command.execute
    #   # Outputs:
    #   #   ca: Article
    #   #   es: Artículo
    #   #   en: Post
    #
    class Get
      attr_reader :key

      # Public: Initializes a new Get command.
      #
      # env - the global environment
      # key - the String key to get the value of
      def initialize(env, key)
        @env = env
        @key = key
      end

      # Public: Executes the command, iterating over each locale, asking the
      # value for the key, and printing it out.
      #
      # Returns the key.
      def execute
        locales.each do |locale|
          result = locale.get qualified_key
          if result.is_a? String
            print "  #{locale.to_s}: #{result}\n"
          else
            print "  #{locale.to_s}: (empty)\n"
          end
        end
        @key
      end

      #######
      private
      #######

      # Public: Returns an Array with the locales of the environment.
      def locales
        @env.locales
      end

      # Public: Returns the given String key according to the qualified
      # namespace we are in.
      def qualified_key
        [@env.namespace.to_s, @key].reject(&:empty?).compact.join('.')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mayl-0.2.0 lib/mayl/commands/get.rb
mayl-0.1.0 lib/mayl/commands/get.rb
mayl-0.0.1 lib/mayl/commands/get.rb