module PolyglotCli::Helper::Nest

Public Instance Methods

denest(nested_hash) click to toggle source
# 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) click to toggle source
# 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