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

#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
# File 'lib/i18n_kit/document.rb', line 68

def to_flat_hash
  # On demand flattening
  populate_flat_hash(@hash) if @flat_hash.size == 0 and @hash.size > 1
  @flat_hash
end

#to_flat_yamlObject



74
75
76
# File 'lib/i18n_kit/document.rb', line 74

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



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

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