Module: PolyglotCli::Helper::Nest

Included in:
Command::Pull, Command::Push
Defined in:
lib/polyglot_cli/helpers/nest.rb

Instance Method Summary collapse

Instance Method Details

#denest(nested_hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/polyglot_cli/helpers/nest.rb', line 18

def denest(nested_hash)
  nested_hash.each_with_object({}) do |(key, value), hash|
    if value.is_a? Hash
      denest(value).map do |h_key, h_value|
        hash["#{key}.#{h_key}"] = h_value
      end
    else
      hash[key] = value
    end
  end
end

#nest(translation_key, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/polyglot_cli/helpers/nest.rb', line 4

def nest(translation_key, value)
  # Try get first nested key
  key = translation_key.slice!(/^.+?\./) # Regex: select all chars until first dot

  if key.nil?
    # If no nested key return key/value hash
    { translation_key => value }
  else
    # Recursively try to get next nested key
    key.delete!('.')
    { key => nest(translation_key, value) }
  end
end