Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

locales commit themselves to disk

  • Loading branch information...
commit c633f39e67095a46a722c4fa6a9a8a95c5e66e5a 1 parent 42f8334
@txus authored
Showing with 50 additions and 44 deletions.
  1. +1 −2  lib/mayl/loader.rb
  2. +23 −34 lib/mayl/locale.rb
  3. +26 −8 test/mayl/locale_test.rb
View
3  lib/mayl/loader.rb
@@ -18,8 +18,7 @@ module Loader
# Returns an Array of Locale objects.
def self.load(path)
Dir[File.expand_path(path) << "/*.yml"].map { |filename|
- name = filename.split('/').last.gsub('.yml','').to_sym
- Locale.new name, YAML.load(File.read(filename))
+ Locale.new filename, YAML.load(File.read(filename))
}
end
end
View
57 lib/mayl/locale.rb
@@ -1,15 +1,11 @@
-require 'ostruct'
+require 'yaml'
module Mayl
- # Public: A Locale is the representation of a YAML translation file, but
- # traversable as an OpenStruct, and with some convenience methods to set and
- # get keys.
+ # Public: A Locale is the representation of a YAML translation file.
#
# Example
#
# locale = Locale.new(:ca, {'ca' => {'activerecord' => ... }})
- # locale.activerecord.models.post
- # # => 'Article'
# locale.set('activerecord.models.comment', 'Comentari')
# locale.get('activerecord.models.comment')
# # => 'Comentari
@@ -17,25 +13,15 @@ module Mayl
class Locale
attr_reader :name
- # Anonymous function used to generate a deep-openstruct from a deep Hash
- # object. Meant to use with #inject on a Hash.
- Iterator = lambda do |acc, elem|
- attr = Array(elem).first
- element = elem.last
- # Deep-generate an OpenStruct if it's not the leaf of the tree
- element = element.inject(OpenStruct.new, &Iterator) unless element.is_a?(String)
-
- acc.send(:"#{attr}=", element)
- acc
- end
-
# Public: Initializes a new Locale.
#
- # name - the Symbol ISO for the locale (:ca, :es)
+ # path - the filename path of the YAML file
# hash - the Hash outputted by the YAML parser
- def initialize(name, hash)
- @name = name
- @data = hash[name.to_s].inject(OpenStruct.new, &Iterator)
+ def initialize(path, hash)
+ @path = path
+ @name = path.split('/').last.gsub('.yml','').to_sym
+ @data = hash[name.to_s]
+ @dirty = false
end
# Public: Sets a key to a given value.
@@ -50,31 +36,34 @@ def set(key, value)
name = ary.last
path = qualifier.inject(@data) do |acc, name|
- acc.send(:"#{name}=", OpenStruct.new) unless acc.respond_to?(name)
- acc.send(name)
+ acc[name] ||= {}
end
-
- path.send("#{name}=", value)
+
+ @dirty = true
+
+ path[name] = value
end
# Public: Gets the value for a given key.
#
- # key - the String key to be set, fully qualified
+ # key - the String key to be set, fully qualified
#
# Returns the String value.
def get(key)
key.split('.').inject(@data) do |acc, name|
- acc.send(name)
+ acc[name] ||= {}
end
end
- # Public: Convenience DSL to traverse the OpenStruct more comfortably.
- #
- # m - the property to get
+ # Public: Saves any changes to disk.
#
- # Returns the String value of that property.
- def method_missing(m, *args, &block)
- @data.send(m) || super
+ # Returns nothing.
+ def commit
+ return false unless @dirty
+
+ File.open(@path, 'w') do |f|
+ f.write YAML.dump({ @name.to_s => @data })
+ end
end
end
end
View
34 test/mayl/locale_test.rb
@@ -4,20 +4,38 @@ module Mayl
describe Locale do
before do
hash = { 'ca' => { 'activerecord' => { 'models' => { 'post' => 'Article' } } } }
- @locale = Locale.new(:ca, hash)
+ @locale = Locale.new('some/ca.yml', hash)
end
- it 'wraps a YAML hash' do
- @locale.activerecord.models.post.must_equal 'Article'
+ it 'sets and retrieves a given key' do
+ @locale.set('activerecord.models.comment', 'Comentari')
+ @locale.get('activerecord.models.comment').must_equal 'Comentari'
end
- it 'sets a given key' do
- @locale.set('activerecord.models.comment', 'Comentari')
- @locale.activerecord.models.comment.must_equal 'Comentari'
+ describe '#commit' do
+ it 'does not commit if there are no changes' do
+ @locale.commit.must_equal false
+ end
end
- it 'gets a given key' do
- @locale.get('activerecord.models.post').must_equal 'Article'
+ it 'saves changes to disk' do
+ @locale.set('activerecord.whatever', 'foo')
+ YAML.expects(:dump).with({
+ 'ca' => {
+ 'activerecord' => {
+ 'models' => {
+ 'post' => 'Article'
+ },
+
+ 'whatever' => 'foo'
+ }
+ }
+ }).returns dumped_contents = stub
+
+ File.expects(:open).with('some/ca.yml', 'w').yields file = stub
+ file.expects(:write).with dumped_contents
+
+ @locale.commit
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.