Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 35ee1bf8fa
Fetching contributors…

Cannot retrieve contributors at this time

file 83 lines (73 sloc) 1.905 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
require 'yaml'

module Mayl
  # 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
  #
  class Locale
    attr_reader :name

    # Public: Initializes a new Locale.
    #
    # path - the filename path of the YAML file
    # hash - the Hash outputted by the YAML parser
    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.
    #
    # 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

      _data = @data
      qualifier.each do |path|
        _data = _data[path]
      end
      _data[name] = value

      @dirty = true
    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[name] ||= {}
      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.
    def commit
      return false unless @dirty

      File.open(@path, 'w') do |f|
        f.write YAML.dump({ @name.to_s => @data })
      end
    end

    # Public: Returns a String representation of the Locale.
    def to_s
      @name
    end
  end
end
Something went wrong with that request. Please try again.