Sha256: 692fde41c209ede9d19ae6121cb9276c420c27b0bcf23f02f2a6b97703a5497e

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

Contents

module Dentaku
  class FlatHash
    def self.from_hash(h, key = [], acc = {})
      return acc.update(key => h)  unless h.is_a? Hash
      h.each { |k, v| from_hash(v, key + [k], acc) }
      flatten_keys(acc)
    end

    def self.from_hash_with_intermediates(h, key = [], acc = {})
      acc.update(key => h) unless key.empty?
      return unless h.is_a? Hash
      h.each { |k, v| from_hash_with_intermediates(v, key + [k], acc) }
      flatten_keys(acc)
    end

    def self.flatten_keys(hash)
      hash.each_with_object({}) do |(k, v), h|
        h[flatten_key(k)] = v
      end
    end

    def self.flatten_key(segments)
      return segments.first if segments.length == 1
      key = segments.join('.')
      key = key.to_sym if segments.first.is_a?(Symbol)
      key
    end

    def self.expand(hash)
      hash.each_with_object({}) do |(k, v), r|
        hash_levels = k.to_s.split('.')
        hash_levels = hash_levels.map(&:to_sym) if k.is_a?(Symbol)
        child_hash = hash_levels[0...-1].reduce(r) { |h, n| h[n] ||= {} }
        child_hash[hash_levels.last] = v
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
dentaku-3.5.4 lib/dentaku/flat_hash.rb
dentaku-3.5.3 lib/dentaku/flat_hash.rb
dentaku-3.5.2 lib/dentaku/flat_hash.rb
dentaku_zevo-3.5.2 lib/dentaku/flat_hash.rb
dentaku-3.5.1 lib/dentaku/flat_hash.rb
dentaku-3.5.0 lib/dentaku/flat_hash.rb
dentaku-3.4.2 lib/dentaku/flat_hash.rb
dentaku-3.4.1 lib/dentaku/flat_hash.rb
dentaku-3.4.0 lib/dentaku/flat_hash.rb