Permalink
Browse files

edit command

  • Loading branch information...
1 parent 8f3cdd5 commit 051471d4d4f31b2a1ed9fb1d190660aa1864ccc3 @txus committed Apr 17, 2012
View
@@ -7,3 +7,4 @@ module Commands
require 'mayl/commands/set'
require 'mayl/commands/get'
+require 'mayl/commands/edit'
View
@@ -0,0 +1,54 @@
+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
+ 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
View
@@ -27,7 +27,7 @@ def initialize(env, key)
# Public: Executes the command, iterating over each locale, asking the
# value for the key, and printing it out.
#
- # Returns nothing.
+ # Returns the key.
def execute
locales.each do |locale|
result = locale.get qualified_key
@@ -37,6 +37,7 @@ def execute
print " #{locale.to_s}: (empty)\n"
end
end
+ @key
end
#######
View
@@ -26,12 +26,13 @@ def initialize(env, key)
# Public: Executes the command, iterating over each locale, asking the
# user for a value, and setting it.
#
- # Returns nothing.
+ # Returns the key.
def execute
locales.each do |locale|
- print " #{locale}: "
+ print " #{locale.to_s}: "
locale.set qualified_key, gets.chomp
end
+ @key
end
#######
View
@@ -3,10 +3,12 @@ module Mayl
# ability to save locales to disk.
class Env
attr_reader :locales
+ attr_accessor :last_value
# Public: Initializes a new Env loading the locales from a path.
def initialize(path)
- @locales = Loader.load(path)
+ @locales = Loader.load(path)
+ @last_value = nil
end
# Public: Saves any changes to disk.
View
@@ -12,7 +12,7 @@ def initialize(env)
#
# Returns a Command.
def parse(input)
- operator, operands = input.split
+ operator, *operands = input.split
klass = Commands.const_get(operator.capitalize)
klass.new(@env, *Array(operands))
end
View
@@ -20,9 +20,15 @@ def start
locales = @env.locales.map(&:name)
puts "Detected locales: #{locales.join(', ')}"
while (print "> "; input = gets)
- @parser.parse(input.chomp).execute
- @env.commit
- print "\n"
+ begin
+ value = @parser.parse(input.chomp).execute
+ @env.last_value = value
+ @env.commit
+ rescue => e
+ print "Error: #{e.message}"
+ ensure
+ print "\n"
+ end
end
end
end
@@ -0,0 +1,19 @@
+# encoding: utf-8
+require 'test_helper'
+
+module Mayl
+ module Commands
+ describe Edit do
+ before do
+ @locales = [stub(name: 'ca'), stub(name: 'en')]
+ @env = stub locales: @locales, namespace: '', last_value: 'activerecord.models.post'
+ @command = Edit.new @env, 'en', 'Entry'
+ end
+
+ it 'edits the last key getted or setted' do
+ @locales.last.expects(:set).with('activerecord.models.post', 'Entry')
+ @command.execute
+ end
+ end
+ end
+end

0 comments on commit 051471d

Please sign in to comment.