Skip to content
Browse files

implement Locale#get and Locale#set

  • Loading branch information...
1 parent aa5ee82 commit 787a91597bfd753ca97794dd340459e2c9bdbc4b @txus committed
Showing with 79 additions and 7 deletions.
  1. +58 −7 lib/mayl/locale.rb
  2. +21 −0 test/mayl/locale_test.rb
View
65 lib/mayl/locale.rb
@@ -1,27 +1,78 @@
require 'ostruct'
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.
+ #
+ # Example
+ #
+ # locale = Locale.new(:ca, {'ca' => {'activerecord' => ... }})
+ # locale.activerecord.models.post
+ # # => 'Article'
+ # locale.set('activerecord.models.comment', 'Comentari')
+ # locale.get('activerecord.models.comment')
+ # # => 'Comentari
+ #
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
-
- if elem.last.is_a? String
- element = elem.last
- else
- element = elem.last.inject(OpenStruct.new, &Iterator)
- end
+ 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)
+ # hash - the Hash outputted by the YAML parser
def initialize(name, hash)
@name = name
@data = hash[name.to_s].inject(OpenStruct.new, &Iterator)
end
+ # Public: Sets a key to a given value.
+ #
+ # key - the String key to be set, fully qualified
+ # value - the new value to give to that key
+ #
+ # Returns nothing.
+ def set(key, value)
+ ary = key.split('.')
+ qualifier = ary[0..-2]
+ name = ary.last
+
+ path = qualifier.inject(@data) do |acc, name|
+ acc.send(:"#{name}=", OpenStruct.new) unless acc.respond_to?(name)
+ acc.send(name)
+ end
+
+ path.send("#{name}=", value)
+ end
+
+ # Public: Gets the value for a given key.
+ #
+ # 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)
+ end
+ end
+
+ # Public: Convenience DSL to traverse the OpenStruct more comfortably.
+ #
+ # m - the property to get
+ #
+ # Returns the String value of that property.
def method_missing(m, *args, &block)
@data.send(m) || super
end
View
21 test/mayl/locale_test.rb
@@ -1,2 +1,23 @@
require 'test_helper'
+module Mayl
+ describe Locale do
+ before do
+ hash = { 'ca' => { 'activerecord' => { 'models' => { 'post' => 'Article' } } } }
+ @locale = Locale.new(:ca, hash)
+ end
+
+ it 'wraps a YAML hash' do
+ @locale.activerecord.models.post.must_equal 'Article'
+ end
+
+ it 'sets a given key' do
+ @locale.set('activerecord.models.comment', 'Comentari')
+ @locale.activerecord.models.comment.must_equal 'Comentari'
+ end
+
+ it 'gets a given key' do
+ @locale.get('activerecord.models.post').must_equal 'Article'
+ end
+ end
+end

0 comments on commit 787a915

Please sign in to comment.
Something went wrong with that request. Please try again.