Class: I18nKit::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_kit/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Document

Returns a new instance of Document.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/i18n_kit/document.rb', line 8

def initialize(opts={})
  @name = opts.fetch(:name, :undefined)
  # Each Document can only have one locale
  @locale = opts.fetch(:locale, :undefined)
  # This is the hierarchical format that is commonly used in standard I18n
  # Note that the root node (i.e. 'en') is *not* supposed to be included
  @hash = opts.fetch(:hash, {})
  # This is a flattened version of the hierarchical hash
  # There is again no locale at the beginning of the keys (i.e. 'one.two' instead of 'en.one.two')
  @flat_hash = if opts.has_key?(:flat_hash)
    opts[:flat_hash]
  else
    {}
  end
  self
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



6
7
8
# File 'lib/i18n_kit/document.rb', line 6

def locale
  @locale
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/i18n_kit/document.rb', line 6

def name
  @name
end

Instance Method Details

#[](chain) ⇒ Object



32
33
34
# File 'lib/i18n_kit/document.rb', line 32

def [](chain)
  to_flat_hash[chain]
end

#[]=(key, value) ⇒ Object



25
26
27
28
29
30
# File 'lib/i18n_kit/document.rb', line 25

def []=(key, value)
  yamlator = YAMLator.new(@hash)
  yamlator[key] = value
  @hash = yamlator.hash
  @flat_hash = {}
end

#each_pair(hash, prev_key = nil, &block) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/i18n_kit/document.rb', line 86

def each_pair(hash, prev_key=nil, &block)
  hash.each do |key, value|
    curr_key = [prev_key, key].compact.join('.').to_sym
    yield curr_key.to_s, value unless value.is_a?(Hash)
    each_pair(value, curr_key, &block) if value.is_a?(Hash)
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/i18n_kit/document.rb', line 36

def has_key?(key)
  to_flat_hash.has_key?(key)
end

#keysObject



40
41
42
# File 'lib/i18n_kit/document.rb', line 40

def keys
  to_flat_hash.keys
end

#to_flat_hashObject



68
69
70
71
72
73
74
75
76
# File 'lib/i18n_kit/document.rb', line 68

def to_flat_hash
  if @flat_hash.size == 0 and @hash.size > 1
    # On demand flattening
    each_pair(@hash) do |key, value|
      @flat_hash[key] = value
    end
  end
  @flat_hash
end

#to_flat_yamlObject



78
79
80
# File 'lib/i18n_kit/document.rb', line 78

def to_flat_yaml
  to_flat_hash.ya2yaml
end

#to_hierarchical_hashObject

Without locale name as root



49
50
51
# File 'lib/i18n_kit/document.rb', line 49

def to_hierarchical_hash
  @hash
end

#to_i18n_hashObject

With locale name as root



54
55
56
# File 'lib/i18n_kit/document.rb', line 54

def to_i18n_hash
  { @locale => @hash }
end

#to_i18n_yamlObject



58
59
60
# File 'lib/i18n_kit/document.rb', line 58

def to_i18n_yaml
  to_i18n_hash.ya2yaml
end

#to_sObject



82
83
84
# File 'lib/i18n_kit/document.rb', line 82

def to_s
  @name
end

#valuesObject



44
45
46
# File 'lib/i18n_kit/document.rb', line 44

def values
  to_flat_hash.values
end

#write_i18n_file(path) ⇒ Object



62
63
64
65
66
# File 'lib/i18n_kit/document.rb', line 62

def write_i18n_file(path)
  File.open(path, 'w') do |file|
    file.write to_i18n_yaml
  end
end