Sha256: 05d460f87e9ba2b8a394141dc7fd5fb08b2e1013aea244d1270fa4f89819b535
Contents?: true
Size: 854 Bytes
Versions: 4
Compression:
Stored size: 854 Bytes
Contents
Hash.class_eval do # Flattens a hash with delimiters and key prefix/postfix. # # hash = { # "errors" => { # "something" => { # "somethingElse" => { # "password" => ["VALIDATION_STATE_PASSWORD"] # } # } # } # } # # Could be turned into a flattened hash using this: # # hash.string_from_hash prefix: "registration[", delimiter: "][", postfix: "]" # # And the end result would look like this: # # { "registration[errors][something][somethingElse][password]" => ["VALIDATION_STATE_PASSWORD"] } def flatten_keys(prefix: "", postfix: "", delimiter: "") each_with_object({}) do |(k, v), ret| key = k.to_s if v.is_a? Hash ret.merge! v.flatten_keys(prefix: prefix + key + delimiter, postfix: postfix, delimiter: delimiter) if v.present? else ret[prefix + key + postfix] = v end end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
hash-pipe-0.5.0 | lib/hash_pipe/flattening.rb |
hash-pipe-0.4.1 | lib/hash_pipe/flattening.rb |
hash-pipe-0.4.0 | lib/hash_pipe/flattening.rb |
hash-pipe-0.3.0 | lib/hash_pipe/flattening.rb |