Class: Mayl::Locale
- Inherits:
-
Object
- Object
- Mayl::Locale
- Defined in:
- lib/mayl/locale.rb
Overview
Public: A Locale is the representation of a YAML translation file.
Example
locale = Locale.new(:ca, {'ca' => {'activerecord' => ... }})
locale.set('activerecord.models.comment', 'Comentari')
locale.get('activerecord.models.comment')
# => 'Comentari
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#commit ⇒ Object
Public: Saves any changes to disk.
-
#get(key) ⇒ Object
Public: Gets the value for a given key.
-
#initialize(path, hash) ⇒ Locale
constructor
Public: Initializes a new Locale.
-
#peek(key) ⇒ Object
Public: Returns an Array of nodes inside a key, or nil if the key represents a leaf.
-
#set(key, value) ⇒ Object
Public: Sets a key to a given value.
-
#to_s ⇒ Object
Public: Returns a String representation of the Locale.
Constructor Details
#initialize(path, hash) ⇒ Locale
Public: Initializes a new Locale.
path - the filename path of the YAML file hash - the Hash outputted by the YAML parser
20 21 22 23 24 25 |
# File 'lib/mayl/locale.rb', line 20 def initialize(path, hash) @path = path @name = path.split('/').last.gsub('.yml','').to_sym @data = hash[name.to_s] @dirty = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/mayl/locale.rb', line 14 def name @name end |
Instance Method Details
#commit ⇒ Object
Public: Saves any changes to disk.
Returns nothing.
70 71 72 73 74 75 76 |
# File 'lib/mayl/locale.rb', line 70 def commit return false unless @dirty File.open(@path, 'w') do |f| f.write YAML.dump({ @name.to_s => @data }) end end |
#get(key) ⇒ Object
Public: Gets the value for a given key.
key - the String key to be set, fully qualified
Returns the String value.
52 53 54 55 56 |
# File 'lib/mayl/locale.rb', line 52 def get(key) key.split('.').inject(@data) do |acc, name| acc[name] ||= {} end end |
#peek(key) ⇒ Object
Public: Returns an Array of nodes inside a key, or nil if the key represents a leaf.
60 61 62 63 64 65 |
# File 'lib/mayl/locale.rb', line 60 def peek(key) result = get(key) if result.is_a?(Hash) result.keys end end |
#set(key, value) ⇒ Object
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.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mayl/locale.rb', line 33 def set(key, value) ary = key.split('.') qualifier = ary[0..-2] name = ary.last _data = @data qualifier.each do |path| _data = _data[path] end _data[name] = value @dirty = true end |
#to_s ⇒ Object
Public: Returns a String representation of the Locale.
79 80 81 |
# File 'lib/mayl/locale.rb', line 79 def to_s @name end |