Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add the set command

  • Loading branch information...
commit a8369f12dbcc88137387c57e0c23f35b2b8f6a61 1 parent 483d946
@txus authored
View
1  lib/mayl.rb
@@ -1,4 +1,5 @@
require "mayl/version"
+require "mayl/commands"
# Public: Mayl is an anagram of YAML, and also a console to create, edit and
# maintain YAML files in any kind of Ruby projects.
View
8 lib/mayl/commands.rb
@@ -0,0 +1,8 @@
+module Mayl
+ # Public: The Commands module is a namespace for all the commands that Mayl
+ # uses.
+ module Commands
+ end
+end
+
+require 'mayl/commands/set'
View
53 lib/mayl/commands/set.rb
@@ -0,0 +1,53 @@
+module Mayl
+ module Commands
+ # Public: The Set command accepts a key and asks the user to type in the
+ # translations for that key in each of the locales.
+ #
+ # Example
+ #
+ # command = Set.new(env, 'activerecord.models.post')
+ # command.execute
+ # ca: <type something>
+ # en: <type something>
+ # # Now locales have those values set.
+ #
+ class Set
+ attr_reader :key
+
+ # Public: Initializes a new Set command.
+ #
+ # env - the global environment
+ # key - the String key to be set
+ def initialize(env, key)
+ @env = env
+ @key = key
+ end
+
+ # Public: Executes the command, iterating over each locale, asking the
+ # user for a value, and setting it.
+ #
+ # Returns nothing.
+ def execute
+ locales.each do |locale|
+ print " #{locale}: "
+ locale.set qualified_key, gets.chomp
+ end
+ 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].join('.')
+ end
+ end
+ end
+end
View
2  mayl.gemspec
@@ -18,5 +18,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'yard'
gem.add_development_dependency 'yard-tomdoc'
gem.add_development_dependency 'redcarpet'
+
gem.add_development_dependency 'minitest'
+ gem.add_development_dependency 'mocha'
end
View
22 test/mayl/commands/set_test.rb
@@ -0,0 +1,22 @@
+require 'test_helper'
+
+module Mayl
+ module Commands
+ describe Set do
+ before do
+ @locales = [stub(to_s: 'ca'), stub(to_s: 'en')]
+ @env = stub locales: @locales, namespace: 'activerecord'
+ @command = Set.new @env, 'models.post'
+ end
+
+ it 'lets the user enter a value for the key in each of the locales' do
+ @command.expects(:gets).twice.returns('Article', 'Post') # User interaction
+
+ @locales.first.expects(:set).with('activerecord.models.post', 'Article')
+ @locales.last.expects(:set).with('activerecord.models.post', 'Post')
+
+ @command.execute
+ end
+ end
+ end
+end
View
1  test/test_helper.rb
@@ -1,4 +1,5 @@
gem 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
+require 'mocha'
require 'mayl'
Please sign in to comment.
Something went wrong with that request. Please try again.