Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Compress deeply nested hashes to a single level hash
Ruby
Branch: master

README.md

HashSmash

Hash#smash takes a deeply nested hash and compresses it to a single level. The new key names will be the old hierarchy of keys as strings separated by periods. This is useful for when you are going to send a hash to another data format to be used by another language, such as JavaScript for use with the Polyglot library for I18n.

Installation

Add this line to your application's Gemfile:

gem 'hash_smash'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_smash

Usage

Simply require the hash_smash and then send smash or smash! (if you want the original hash mutated) to the hash:

require 'hash_smash'

locales = {
  greeting: {
    hello: 'hello',
    hi: 'hi',
    bye:
      informal: 'later dude'
      formal: 'I bid you farewell, good sir'
    cya: 'cya'
  }
}

smashed_locales = locales.smash
=> {
  'greeting.hello' => 'hello',
  'greeting.hi' => 'hi',
  'greeting.bye.informal' => 'later dude',
  'greeting.bye.formal' => 'I bid you farewell, good sir',
  'greeting.cya' => 'cya'
}

# smash the hash in place
locales.smash!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.