0.0
The project is in a healthy, maintained state
HashHelper is a collection of advanced helper methods for Ruby hashes. It provides tools for deep inversion, normalization, percentage calculations, recursive operations, sorting, validation, linear regression, and much more. HashHelper is designed to simplify complex hash manipulations with clean, reusable methods. Extend the power of the Hash class seamlessly!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

HashHelper Gem

HashHelper is a versatile Ruby gem providing powerful utility modules for deeply nested hashes. It simplifies common operations such as deep inversion, normalization, summation, percentage calculations, and converting arrays into nested hash structures. This gem automatically extends Hash and Array with the provided methods, allowing you to seamlessly manipulate nested data.

Installation

Add this line to your application's Gemfile:

gem 'hash_helper'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install hash_helper

Features

1. DeepInvert

Deeply inverts a hash, reversing the nesting structure.

Example:

hash = { a: { b: { c: 1 } }, d: { e: 2 } }
inverted = hash.deep_invert
# Result: { c: { b: { a: 1 } }, e: { d: 2 } }

2. DeepNormalize

Recursively normalizes a hash by ensuring all layers in the nested structure contain the same keys, merging it with a default structure to fill in any missing keys or values.

Example:

hash = { a: { x: 1, y: 2 }, b: { z: 3 } }
normalized = hash.deep_normalize(default_value: 0)
# Result: { a: { x: 1, y: 2, z: 0 }, b: { x: 0, y: 0, z: 3 } }

3. DeepSum

Calculates the sum of all numeric values in a nested hash or array.

Example:

hash = {
  a: 1,
  b: { c: 2, d: { e: 3, f: 4 } },
  g: [5, { h: 6, i: { j: 7 } }]
}
result = hash.deep_sum
# Result: 28

4. Percentage

Transforms numeric values in a hash into percentages relative to a layer or the entire structure.

Example:

hash = { a: 50, b: { c: 30, d: 20 }, e: 100 }

# Global percentages
percentages = hash.deep_transform_values_to_percentages(relative: false)
# Result: { a: 25.0, b: { c: 15.0, d: 10.0 }, e: 50.0 }

# Relative percentages
relative_percentages = hash.deep_transform_values_to_percentages(relative: true)
# Result: { a: 25.0, b: { c: 60.0, d: 40.0 }, e: 50.0 }

5. ToNestedH

Converts a nested array into a nested hash with a default value at leaf nodes.

Example:

nested_array = [[:a, :b], [:x, :y], [:m, :n]]
nested_hash = nested_array.to_nested_h(value: "leaf")
# Result:
# {
#   a: { x: { m: "leaf", n: "leaf" }, y: { m: "leaf", n: "leaf" } },
#   b: { x: { m: "leaf", n: "leaf" }, y: { m: "leaf", n: "leaf" } }
# }

Usage

The gem automatically extends Hash and Array with the provided methods. You can use them directly:

require 'hash_helper'

hash = { a: { b: { c: 1 } } }
puts hash.deep_invert

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Synsbasen/hash_helper.

License

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