Class: I18nliner::Extractors::TranslationHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/i18nliner/extractors/translation_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TranslationHash

Returns a new instance of TranslationHash.



13
14
15
16
# File 'lib/i18nliner/extractors/translation_hash.rb', line 13

def initialize(*args)
  super
  @total_size = 0
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



4
5
6
# File 'lib/i18nliner/extractors/translation_hash.rb', line 4

def line
  @line
end

Class Method Details

.new(hash = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/i18nliner/extractors/translation_hash.rb', line 6

def self.new(hash = {})
  hash.inject(super()) do |result, (key, value)|
    result.store(key.to_s, value.is_a?(Hash) ? new(value) : value)
    result
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



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
# File 'lib/i18nliner/extractors/translation_hash.rb', line 18

def []=(key, value)
  parts = key.split('.')
  leaf = parts.pop
  hash = self
  while part = parts.shift
    if hash[part]
      unless hash[part].is_a?(Hash)
        intermediate_key = key.sub((parts + [leaf]).join('.'), '')
        raise KeyAsScopeError, intermediate_key
      end
    else
      hash.store(part, {})
    end
    hash = hash[part]
  end
  if hash[leaf]
    if hash[leaf] != value
      if hash[leaf].is_a?(Hash)
        raise KeyAsScopeError.new(@line, key)
      else
        raise KeyInUseError.new(@line, key)
      end
    end
  else
    @total_size += 1
    hash.store(leaf, value)
  end
end