Permalink
Browse files

add cd

  • Loading branch information...
1 parent 051471d commit bcc9603464e6102c097056a85976bb0614b165ef @txus committed Apr 17, 2012
Showing with 71 additions and 0 deletions.
  1. +1 −0 lib/mayl/commands.rb
  2. +38 −0 lib/mayl/commands/cd.rb
  3. +32 −0 test/mayl/commands/cd_test.rb
View
@@ -8,3 +8,4 @@ module Commands
require 'mayl/commands/set'
require 'mayl/commands/get'
require 'mayl/commands/edit'
+require 'mayl/commands/cd'
View
@@ -0,0 +1,38 @@
+module Mayl
+ module Commands
+ # Public: The Cd command enters in a YAML namespace.
+ #
+ # Example
+ #
+ # command = Edit.new(env, 'es', 'Artículo')
+ # command.execute
+ #
+ class Cd
+ # Public: Initializes a new Cd command.
+ #
+ # env - the global environment
+ # path - the path to cd in
+ def initialize(env, path)
+ @env = env
+ @path = path
+ end
+
+ # Public: Adds the path to the namespace.
+ #
+ # Returns nil.
+ def execute
+ case @path
+ when ".."
+ ns = @env.namespace.split('.')
+ ns.pop
+ @env.namespace = ns.join('.')
+ when "."
+ @env.namespace = ""
+ else
+ @env.namespace << '.' << @path
+ end
+ nil
+ end
+ end
+ end
+end
@@ -0,0 +1,32 @@
+# encoding: utf-8
+require 'test_helper'
+require 'ostruct'
+
+module Mayl
+ module Commands
+ describe Cd do
+ before do
+ @locales = [stub(to_s: 'ca'), stub(to_s: 'en')]
+ @env = OpenStruct.new(locales: @locales, namespace: 'activerecord.models')
+ end
+
+ it 'enters a directory' do
+ @command = Cd.new @env, 'post.attributes'
+ @command.execute
+ @env.namespace.must_equal 'activerecord.models.post.attributes'
+ end
+
+ it 'goes down one level' do
+ @command = Cd.new @env, '..'
+ @command.execute
+ @env.namespace.must_equal 'activerecord'
+ end
+
+ it 'goes to the root level' do
+ @command = Cd.new @env, '.'
+ @command.execute
+ @env.namespace.must_equal ''
+ end
+ end
+ end
+end

0 comments on commit bcc9603

Please sign in to comment.