Class: GoogleTranslateDiff::Linearizer

Inherits:
Object
  • Object
show all
Defined in:
lib/google_translate_diff/linearizer.rb

Class Method Summary collapse

Class Method Details

.linearize(struct, array = []) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/google_translate_diff/linearizer.rb', line 3

def linearize(struct, array = [])
  case struct
  when Hash then
    struct.each { |_k, v| linearize(v, array) }
  when Array then
    struct.each { |v| linearize(v, array) }
  else
    array << struct
  end

  array
end

.restore(struct, array) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/google_translate_diff/linearizer.rb', line 16

def restore(struct, array)
  case struct
  when Hash then
    struct.each_with_object({}) { |(k, v), h| h[k] = restore(v, array) }
  when Array then
    struct.map { |v| restore(v, array) }
  else
    array.shift
  end
end