Sha256: b2541969997daaeb1fd8cdf8b8f86cf754740ac90d55b0e114875fadc3238f65

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Mayl
  module Commands
    # Public: The Edit command quickly edits the last value consulted with Get
    # or Set.
    #
    # Example
    # 
    #   command = Edit.new(env, 'es', 'Artículo')
    #   command.execute
    #
    class Edit
      attr_reader :locale, :value

      # Public: Initializes a new Get command.
      #
      # env    - the global environment
      # locale - the locale that we want to edit
      # value  - the value to set
      def initialize(env, locale, value)
        @env    = env
        @key    = @env.last_value
        raise ArgumentError, "You must get or set a key before calling edit" unless @key
        @locale = locale
        @value  = value
      end

      # Public: Executes the command, editing the @key for the given @locale.
      #
      # Returns the key.
      def execute
        key = qualified_key
        locale = locales.detect do |locale|
          locale.name.to_s == @locale.to_s
        end

        locale.set key, @value
        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/edit.rb