0.0
Low commit activity in last 3 years
No release in over a year
Nest Hash String and Symbol keys by splitting with String#split
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

ruby-ci

Hash Nestizy

Convert a flat hash representation to a nested one. Specifically useful to return easily to handle rails ActiveModel::Error in API mode.

Splits Symbol and String keys but converting them to stirng and using String#split method.

Tested on Ruby 2.5 and newer but probably works on older versions.

Examples

>>> require 'hash_nestizy'
=> true
>>> errors = { 'name' => 'is required', 'role.name' => 'max be longer than 5' }
=> {"name"=>"is required", "role.name"=>"max be longer than 5"}
>>> HashNestizy.to_nested(errors)
=> {"name"=>"is required", "role"=>{"name"=>"max be longer than 5"}}

There's also a handy method to patch it to the Hash class.

>>> HashNestizy.patch_hash!
=> Hash
>>> { 'name' => 'is required', 'role.name' => 'max be longer than 5' }.nestizy
=> {"name"=>"is required", "role"=>{"name"=>"max be longer than 5"}}

You can use it to render ActiveModel::Error instances on rails APIs as follow:

class ApiController < ApplicationController
  rescue_from ActiveRecord::RecordInvalid, with: :record_invalid

  def record_invalid(ex)
    render json: HashNestizy.to_nested(ex.record.errors.to_hash), status: :bad_request
  end
end

Beware of key collisions

Unexpected behaviour might occur on key conflicts. There's no reasonable way to handle this on a case that fits all use cases.

>>> data = {'role' => 'admin', 'role.name' => 'Administrator'}
=> {"role"=>"admin", "role.name"=>"Administrator"}
>>> HashNestizy.to_nested(data)
=> {"role"=>"admin"}

You can specify an iterator of [key, value] pairs and use the override: bool parameter to handle key conflicts correctly:

>>> data = [['role', 'admin'], ['role.name', 'Administrator']]
=> [["role", "admin"], ["role.name", "Administrator"]]
>>> HashNestizy.to_nested(data, override: true)
=> {"role"=>{"name": "Administrator"}}

Changelog

  • 0.0.3: Initial release.
  • 0.1.0: Handle conflicts correctly.
  • 0.2.0: Cleaner implementaiton.

License

MIT