Permalink
Browse files

Ls command

  • Loading branch information...
1 parent 2df3941 commit 35ee1bf8fa3a175bbc7b744a75d990ee77af0008 @txus committed Apr 17, 2012
Showing with 80 additions and 0 deletions.
  1. +1 −0 lib/mayl/commands.rb
  2. +40 −0 lib/mayl/commands/ls.rb
  3. +9 −0 lib/mayl/locale.rb
  4. +24 −0 test/mayl/commands/ls_test.rb
  5. +6 −0 test/mayl/locale_test.rb
View
@@ -10,3 +10,4 @@ module Commands
require 'mayl/commands/edit'
require 'mayl/commands/cd'
require 'mayl/commands/exit'
+require 'mayl/commands/ls'
View
@@ -0,0 +1,40 @@
+module Mayl
+ module Commands
+ # Public: The Cd command navigates through YAML namespaces.
+ #
+ # Example
+ #
+ # command = Ls.new(env)
+ # command.execute
+ #
+ class Ls
+ # Public: Initializes a new Cd command.
+ #
+ # env - the global environment
+ def initialize(env)
+ @env = env
+ end
+
+ # Public: Adds the path to the namespace.
+ #
+ # Returns nil.
+ def execute
+ locales.map { |locale|
+ locale.peek(@env.namespace)
+ }.flatten.uniq.each do |option|
+ print "#{option} "
+ end
+ nil
+ end
+
+ #######
+ private
+ #######
+
+ # Public: Returns an Array with the locales of the environment.
+ def locales
+ @env.locales
+ end
+ end
+ end
+end
View
@@ -55,6 +55,15 @@ def get(key)
end
end
+ # Public: Returns an Array of nodes inside a key, or nil if the key
+ # represents a leaf.
+ def peek(key)
+ result = get(key)
+ if result.is_a?(Hash)
+ result.keys
+ end
+ end
+
# Public: Saves any changes to disk.
#
# Returns nothing.
@@ -0,0 +1,24 @@
+# encoding: utf-8
+require 'test_helper'
+
+module Mayl
+ module Commands
+ describe Ls do
+ before do
+ @locales = [stub(to_s: 'ca'), stub(to_s: 'en')]
+ @env = stub locales: @locales, namespace: 'activerecord'
+ @command = Ls.new @env
+ end
+
+ it 'prints the current keys inside the namespace' do
+ @locales.first.expects(:peek).with('activerecord').returns ['models']
+ @locales.last.expects(:peek).with('activerecord').returns ['attributes', 'models']
+
+ @command.expects(:print).with('models ')
+ @command.expects(:print).with('attributes ')
+
+ @command.execute
+ end
+ end
+ end
+end
View
@@ -41,5 +41,11 @@ module Mayl
@locale.commit
end
+
+ it 'peeks the contents of a given key' do
+ @locale.peek('activerecord').must_equal ['models']
+ @locale.peek('activerecord.models').must_equal ['post']
+ @locale.peek('activerecord.models.post').must_equal nil
+ end
end
end

0 comments on commit 35ee1bf

Please sign in to comment.