Sha256: a14153919de686bc843a5dcadc44b5c55cd24804a2f060aef301e2ee2fab1496

Contents?: true

Size: 608 Bytes

Versions: 32

Compression:

Stored size: 608 Bytes

Contents

# -*- encoding : utf-8 -*-

module Phrase
  module HashFlattener
    FLATTEN_SEPARATOR = "."
    SEPARATOR_ESCAPE_CHAR = "\001"

    def self.escape_default_separator(key)
      key.to_s.tr(FLATTEN_SEPARATOR, SEPARATOR_ESCAPE_CHAR)
    end

    def self.flatten(hash, escape, previous_key=nil, &block)
      hash.each_pair do |key, value|
        key = escape_default_separator(key) if escape
        current_key = [previous_key, key].compact.join(FLATTEN_SEPARATOR).to_sym
        yield current_key, value
        flatten(value, escape, current_key, &block) if value.is_a?(Hash)
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
phrase-0.4.33 lib/phrase/hash_flattener.rb
phrase-0.4.32 lib/phrase/hash_flattener.rb
phrase-0.4.31 lib/phrase/hash_flattener.rb
phrase-0.4.30 lib/phrase/hash_flattener.rb
phrase-0.4.29 lib/phrase/hash_flattener.rb
phrase-0.4.28 lib/phrase/hash_flattener.rb
phrase-0.4.27 lib/phrase/hash_flattener.rb
phrase-0.4.26 lib/phrase/hash_flattener.rb
phrase-0.4.25 lib/phrase/hash_flattener.rb
phrase-0.4.24 lib/phrase/hash_flattener.rb
phrase-0.4.23 lib/phrase/hash_flattener.rb
phrase-0.4.22 lib/phrase/hash_flattener.rb
phrase-0.4.21 lib/phrase/hash_flattener.rb
phrase-0.4.20 lib/phrase/hash_flattener.rb
phrase-0.4.19 lib/phrase/hash_flattener.rb
phrase-0.4.18 lib/phrase/hash_flattener.rb
phrase-0.4.17 lib/phrase/hash_flattener.rb
phrase-0.4.16 lib/phrase/hash_flattener.rb
phrase-0.4.15 lib/phrase/hash_flattener.rb
phrase-0.4.14 lib/phrase/hash_flattener.rb