Sha256: 7979fdbe64b4fdf22c30aa6b3e087b78409a4e6fdaa7709237995e6e87a64dee

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

module I18n::Tasks::DataTraversal
  # translation of the key found in the passed hash or nil
  # @return [String,nil]
  def t(hash = data[base_locale], key)
    if hash.is_a?(String)
      # has is a locale
      raise ArgumentError.new("invalid locale: #{hash}") if hash =~ /[^A-z\d-]/
      hash = data[hash]
    end
    key.split('.').inject(hash) { |r, seg| r[seg] if r }
  end

  # traverse => map if yield(k, v)
  def traverse_map_if(hash)
    list = []
    traverse hash do |k, v|
      mapped = yield(k, v)
      list << mapped if mapped
    end
    list
  end

  # traverse hash, yielding with full key and value
  # @param hash [Hash{String => String,Hash}] translation data to traverse
  # @yield [full_key, value] yields full key and value for every translation in #hash
  # @return [nil]
  def traverse(path = '', hash)
    q = [[path, hash]]
    until q.empty?
      path, value = q.pop
      if value.is_a?(Hash)
        value.each { |k, v| q << ["#{path}.#{k}", v] }
      else
        yield path[1..-1], value
      end
    end
  end

  # [[key, value], ...] list to tree
  def list_to_tree(list)
    list = list.sort
    tree = {}
    list.each do |key, value|
      key_segments            = key.to_s.split('.')
      node                    = key_segments[0..-2].inject(tree) do |r, segment|
        r[segment] ||= {}
      end
      node[key_segments.last] = value
    end
    tree
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
i18n-tasks-0.2.19 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.18 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.17 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.15 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.14 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.13 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.12 lib/i18n/tasks/data_traversal.rb
i18n-tasks-0.2.11 lib/i18n/tasks/data_traversal.rb