Skip to content

Commit

Permalink
add the set command
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Apr 16, 2012
1 parent 483d946 commit a8369f1
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/mayl.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 8 additions & 0 deletions lib/mayl/commands.rb
Original file line number Diff line number Diff line change
@@ -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'
53 changes: 53 additions & 0 deletions lib/mayl/commands/set.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions mayl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions test/mayl/commands/set_test.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
gem 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'mocha'
require 'mayl'

0 comments on commit a8369f1

Please sign in to comment.