HashFlatten Build Status

Flatten nested Hash to one level depth with keys joined with a dot and vice versa.

Content:

  1. About
  2. How to
    1. Installation
    2. Usage
    3. squish_levels
    4. stretch_to_levels
    5. Notes
  3. License

About

This gem includes Refinement which adds #squish_levels and #stretch_to_levels to Hash. With them you can transform this hash: { a: { b: 'b', c: 'c' } } to this: { 'a.b' => 'b', 'a.c' => 'c' } and vice versa. I use these to transform Rails locales to somehow readable csv for external translators or showing XML structure(transformed to a hash) in a table.

How to

Installation

Add this line to your application's Gemfile:

gem 'hash_flatten'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_flatten

Usage

Use as a Refinement:

class MyClass
  using HashFlatten
end

now you can call #squish_levels and #stretch_to_levels on hashes in MyClass(lexically).

Here is an excellent blog post on Refinements if you didn't use them before: http://interblah.net/why-is-nobody-using-refinements

#squish_levels

hash = { a: 'a',
         b: { a: 'b' },
         c: { b: { a: 'c' } } }

hash.squish_levels =>

  { 'a' => 'a',
    'b.a' => 'b',
    'c.b.a' => 'c' }

#stretch_to_levels

hash = { 'a' => 'a',
         'b.a' => 'b',
         'c.b.a' => 'c' }

hash.stretch_to_levels =>

{ 'a' => 'a',
  'b' => { 'a' => 'b' },
  'c' => { 'b' => { 'a' => 'c' } } }

Notes

Some things to know:

  • whenever you call #squish_levels or #stretch_to_levels returned hash has keys transformed to strings
  • squishes or stretches only sub Hashes, other hash-like structure aren't touched(#to_h isn't called on values..)

License

The gem is available as open source under the terms of the MIT License.