Sha256: c3792b22e92a733f3a310980b1ce6406e7d3a9672cd416839e2e8137dcee2990

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 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
          name   = locale.to_s
          if result.is_a? String
            print "  #{name}: #{result}\n"
          else
            print "  #{name}: (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

1 entries across 1 versions & 1 rubygems

Version Path
mayl-0.2.1 lib/mayl/commands/get.rb