Project

smush

0.0
No commit activity in last 3 years
No release in over 3 years
Convert hashes to (and from) a flattened key-value pair.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
 Project Readme

Smush

Build Status

Smush is a simple gem that converts a hash into a flat array of key-value pairs. You can also unsmush the result back into a hash. Note: if an integer key is encountered, Smush will always assume the target object is an Array.

Unsmushed

{
  name: 'John Doe',
  chores: ['Wash dishes', 'Pay rent']
}

Smushed

[
  {
    key: [:name],
    value: 'John Doe'
  },
  {
    key: [:chores, 0],
    value: 'Wash dishes'
  },
  {
    key: [:chores, 1],
    value: 'Pay rent'
  }
]

How

require 'smush'

hash = {
  name: 'Bob Smith',
  favorite_foods: ['BLT', 'Hot Dog On A Stick'],
  siblings: [
    {
      name: 'Ron Smith'
    }
  ]
}

smushed = Smush.smush(hash)

# smushed = [
#   {
#     key: [:name],
#     value: 'Bob Smith'
#   },
#   {
#     key: [:favorite_foods, 0],
#     value: 'BLT'
#   },
#   ...
# ]

original = Smush.unsmush(smushed)

Smush does not check your inputs for consistency, e.g. check that your smushed hash doesn't have duplicate keys, or that you aren't skipping indices in your arrays.