Skip to content

Commit

Permalink
some stuff working
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Apr 17, 2012
1 parent c633f39 commit 8f3cdd5
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp test/tmp
test/version_tmp test/version_tmp
tmp tmp
config/locales
2 changes: 1 addition & 1 deletion bin/mayl
Expand Up @@ -2,4 +2,4 @@
$: << 'lib' $: << 'lib'
require 'mayl' require 'mayl'


Mayl::Repl.new.start ARGV[1] Mayl::Repl.new(ARGV[1]).start
9 changes: 7 additions & 2 deletions lib/mayl/commands/get.rb
Expand Up @@ -30,7 +30,12 @@ def initialize(env, key)
# Returns nothing. # Returns nothing.
def execute def execute
locales.each do |locale| locales.each do |locale|
print " #{locale}: #{locale.get qualified_key}\n" result = locale.get qualified_key
if result.is_a? String
print " #{locale.to_s}: #{result}\n"
else
print " #{locale.to_s}: (empty)\n"
end
end end
end end


Expand All @@ -46,7 +51,7 @@ def locales
# Public: Returns the given String key according to the qualified # Public: Returns the given String key according to the qualified
# namespace we are in. # namespace we are in.
def qualified_key def qualified_key
[@env.namespace.to_s, @key].join('.') [@env.namespace.to_s, @key].reject(&:empty?).compact.join('.')
end end
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/mayl/commands/set.rb
Expand Up @@ -46,7 +46,7 @@ def locales
# Public: Returns the given String key according to the qualified # Public: Returns the given String key according to the qualified
# namespace we are in. # namespace we are in.
def qualified_key def qualified_key
[@env.namespace.to_s, @key].join('.') [@env.namespace.to_s, @key].reject(&:empty?).compact.join('.')
end end
end end
end end
Expand Down
5 changes: 5 additions & 0 deletions lib/mayl/env.rb
Expand Up @@ -13,5 +13,10 @@ def initialize(path)
def commit def commit
@locales.each(&:commit) @locales.each(&:commit)
end end

# Public: The current namespace. For now it's just an empty slime.
def namespace
""
end
end end
end end
13 changes: 9 additions & 4 deletions lib/mayl/locale.rb
Expand Up @@ -35,13 +35,13 @@ def set(key, value)
qualifier = ary[0..-2] qualifier = ary[0..-2]
name = ary.last name = ary.last


path = qualifier.inject(@data) do |acc, name| _data = @data
acc[name] ||= {} qualifier.each do |path|
_data = _data[path]
end end
_data[name] = value


@dirty = true @dirty = true

path[name] = value
end end


# Public: Gets the value for a given key. # Public: Gets the value for a given key.
Expand All @@ -65,5 +65,10 @@ def commit
f.write YAML.dump({ @name.to_s => @data }) f.write YAML.dump({ @name.to_s => @data })
end end
end end

# Public: Returns a String representation of the Locale.
def to_s
@name
end
end end
end end
3 changes: 2 additions & 1 deletion lib/mayl/repl.rb
Expand Up @@ -7,7 +7,8 @@ class Repl
# Public: Initializes a new REPL from a given path. # Public: Initializes a new REPL from a given path.
# #
# path - The path to get the locales from (defaults to 'config/locales'). # path - The path to get the locales from (defaults to 'config/locales').
def initialize(path='config/locales') def initialize(path)
path ||= 'config/locales'
@env = Env.new(path) @env = Env.new(path)
@parser = Parser.new(@env) @parser = Parser.new(@env)
end end
Expand Down
4 changes: 4 additions & 0 deletions test/mayl/locale_test.rb
Expand Up @@ -7,6 +7,10 @@ module Mayl
@locale = Locale.new('some/ca.yml', hash) @locale = Locale.new('some/ca.yml', hash)
end end


it 'gets a key' do
@locale.get('activerecord.models.post').must_equal 'Article'
end

it 'sets and retrieves a given key' do it 'sets and retrieves a given key' do
@locale.set('activerecord.models.comment', 'Comentari') @locale.set('activerecord.models.comment', 'Comentari')
@locale.get('activerecord.models.comment').must_equal 'Comentari' @locale.get('activerecord.models.comment').must_equal 'Comentari'
Expand Down
2 changes: 1 addition & 1 deletion test/mayl/repl_test.rb
Expand Up @@ -3,7 +3,7 @@
module Mayl module Mayl
describe Repl do describe Repl do
before do before do
@repl = Mayl::Repl.new @repl = Mayl::Repl.new('some/path.yml')
end end


it 'parses and executes commands' do it 'parses and executes commands' do
Expand Down

0 comments on commit 8f3cdd5

Please sign in to comment.