{"id":17159244,"url":"https://github.com/akuzko/rehash","last_synced_at":"2026-02-26T16:14:08.536Z","repository":{"id":56891654,"uuid":"177024154","full_name":"akuzko/rehash","owner":"akuzko","description":"gem for hash values mapping and transformation","archived":false,"fork":false,"pushed_at":"2022-02-17T12:25:47.000Z","size":20,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T04:51:17.643Z","etag":null,"topics":["hash","mapping","ruby","ruby-gem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akuzko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-21T21:09:07.000Z","updated_at":"2022-02-17T12:25:50.000Z","dependencies_parsed_at":"2022-08-21T00:50:31.103Z","dependency_job_id":null,"html_url":"https://github.com/akuzko/rehash","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akuzko%2Frehash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akuzko%2Frehash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akuzko%2Frehash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akuzko%2Frehash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akuzko","download_url":"https://codeload.github.com/akuzko/rehash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248722011,"owners_count":21151189,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hash","mapping","ruby","ruby-gem"],"created_at":"2024-10-14T22:13:45.966Z","updated_at":"2026-02-26T16:14:03.493Z","avatar_url":"https://github.com/akuzko.png","language":"Ruby","readme":"# Rehash\n\nThis gem allows you to transform a hash from one structure to another. For instance,\nto extract deeply nested values from it to a more convenient form with a simple and\neasy-to use mapping. Inspired by [hash_mapper](https://github.com/ismasan/hash_mapper),\nbut has a more DRY and robust API.\n\nDo not be confused with Ruby's core `Hash#rehash` method. This gem has nothing to\ndo with it and is used solely for mapping values from source hash into another one.\n\n[![build status](https://secure.travis-ci.org/akuzko/rehash.png)](http://travis-ci.org/akuzko/rehash)\n[![github release](https://img.shields.io/github/release/akuzko/rehash.svg)](https://github.com/akuzko/rehash/releases)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 're-hash', require: 'rehash'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install re-hash\n\n## Usage\n\nConsidering we have a following hash:\n\n```rb\nhash = {\n  foo: {\n    bar: {\n      baz: 1,\n      bak: 2\n    }\n  },\n  foos: [\n    { bar: { baz: '3-1' } },\n    { bar: { baz: '3-2' } }\n  ],\n  big_foo: {\n    nested: {\n      bar1: { baz: '4-1' },\n      bar2: { baz: '4-2' },\n      bar3: { baz: '4-3' }\n    }\n  },\n  config: [\n    {name: 'important_value', value: 'yes'},\n    {name: 'secondary_value', value: 'no'}\n  ]\n}\n```\n\n### Simple mapping\n\nSimple mapping, provided as a mapping hash, allows to quickly map source hash\nvalues to new structure:\n\n```rb\nRehash.map(hash,\n  '/foo/bar/baz'             =\u003e '/faz',\n  '/big_foo/nested/bar1/baz' =\u003e '/baz1'\n)\n# =\u003e {:faz =\u003e 1, :baz1 =\u003e '4-1'}\n```\n\n### Block usage\n\n`Rehash.map` method yield a `Rehash::Mapper` instance that allows you to apply multiple\nmappings, as well as transform mapped values themselves:\n\n```rb\nRehash.map(hash) do |m|\n  m.(\n    '/foo/bar/baz' =\u003e '/faz',\n    '/foo/bar/bak' =\u003e '/fak'\n  )\n  m.('/big_foo/nested/bar1/baz' =\u003e '/baz1') do |value|\n    value.to_i\n  end\n  m.('/foos' =\u003e '/foos') do |foos|\n    foos.map{ |item| Rehash.map(item, '/bar/baz' =\u003e '/value') }\n  end\nend\n# =\u003e {faz: 1, fak: 2, :baz1 =\u003e 4, :foos =\u003e [{:value =\u003e '3-1'}, {:value =\u003e '3-2'}]}\n```\n\nIn case if you need to do any additional manipulations over resulting returned hash,\nyou may access it via `Mapper#result` method (i.e. `m.result` in example above)\n\n### Accessing array items\n\n#### By index\n\nIt is very easy to map values from items within array by accessing them by index:\n\n```rb\nRehash.map(hash, '/foos[0]/bar/baz' =\u003e '/first_faz')\n# =\u003e {:first_faz =\u003e '3-1'}\n```\n\n#### By property lookup\n\nIt is also possible to access item within array by one of it's properties:\n\n```rb\nRehash.map(hash, '/config[name:important_value]/value' =\u003e '/important')\n# =\u003e {:important =\u003e 'yes'}\n```\n\n### Refinement (recommended usage)\n\n`Rehash` also implements a `Hash` class refinement, using which is actually\n**a recommended way** of using `re-hash`. Considering that `#map` and `#rehash`\nmethods are part of Ruby's Hash core functionality, `Rehash` allows to use\n`#map_with` method for hash mappings.\n\n```rb\nusing Rehash\n\nhash.map_with('/foo/bar/baz' =\u003e '/faz') # =\u003e {:faz =\u003e 1}\n# OR:\nhash.map_with do |m|\n  m.('/foo/bar/bak' =\u003e '/fak') { |v| v * 2 }\nend\n# =\u003e {:fak =\u003e 4}\n```\n\n### HashExtension\n\nIn case if you don't want to use refinement and want to have `#map_with` method\nglobally available, you can extend `Hash` itself with core extension:\n\n```rb\nHash.send(:include, HashExtension)\n\n{foo: 'baz'}.map_with('/foo' =\u003e '/bar') # =\u003e {bar: 'baz'}\n```\n\n### Options\n\n`Rehash` uses `'/'` as path delimiter by default, as well as it symbolizes resulting\nkeys. To use other options on a distinct `map` or `map_with` calls you have to use block form:\n\n```rb\nRehash.map(hash, delimiter: '.', symbolize_keys: false) do |m|\n  m.('foo.bar.baz' =\u003e 'foo.baz')\n)\n# =\u003e {\"foo\" =\u003e {\"baz\" =\u003e 1}}\n```\n\nOr you can set default options globally:\n\n```rb\nRehash.default_options(delimiter: '.', symbolize_keys: false)\nRehash.map(hash, 'foo.bar.baz' =\u003e 'foo.baz') # =\u003e {\"foo\" =\u003e {\"baz\" =\u003e 1}}\n```\n\n### Default value\n\nOn mapping, for convenience, you can use `default` option that will be assigned\nto value that is missing at the specified path in the source hash or is `nil`\n*before* it is yielded to the block (if block is given):\n\n```rb\nRehash.map(hash) do |m|\n  m.('/foo/bar/baz' =\u003e '/faz', '/missing' =\u003e '/bak', default: 5) do |value|\n    value * 2\n  end\nend\n# =\u003e {:faz =\u003e 2, :bar =\u003e 10}\n```\n\n### Helper methods\n\n`Mapper` instance that is yielded to the block also has a couple of small helper\nmethods for dealing with arrays and deeply nested values to make things even more DRY.\n\n- `map_array(from =\u003e to, \u0026block)` - used to map an array of items at path `from` to a path `to`,\n  yielding a `Mapper` instance for each item:\n\n```rb\nRehash.map(hash) do |m|\n  m.map_array('/foos' =\u003e '/foos') do |im|\n    im.('/bar/baz' =\u003e '/value')\n  end\n  # is the same as:\n  m.('/foos' =\u003e '/foos') do |value|\n    value.map do |item|\n      Rehash.map(item, '/bar/baz' =\u003e '/value')\n    end\n  end\nend\n```\n\n- `map_hash(from =\u003e to, \u0026block)` - yields a `Mapper` instance for a hash located at\n  the path `from` and puts result of mapping to the path defined by `to`:\n\n```rb\nRehash.map(hash) do |m|\n  m.map_hash('/big_foo/nested' =\u003e '/') do |hm|\n    hm.(\n      '/bar1/baz' =\u003e '/big_baz1',\n      '/bar2/baz' =\u003e '/big_baz2',\n      '/bar3/baz' =\u003e '/big_baz3'\n    )\n  end\n  # is the same as:\n  m.(\n    '/big_foo/nested/bar1/baz' =\u003e '/big_baz1',\n    '/big_foo/nested/bar2/baz' =\u003e '/big_baz2',\n    '/big_foo/nested/bar3/baz' =\u003e '/big_baz3'\n  )\nend\n```\n\n- `[](path)` and `[]=(path, value)` - small helper methods that can be called on mapper\n  object when hash is mapped with a block form. `[](path)` can be used to get the value\n  at the `path` **in source hash**, and `[]=(path, value)` can be used to put the `value`\n  at the `path` **in the resulting hash**:\n\n```rb\nRehash.map(hash) do |m|\n  m['/faz'] = m['/foo/bar/baz']\n  # is essentially the same as:\n  m.('/foo/bar/baz' =\u003e '/faz')\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec`\nto run the tests. You can also run `bin/console` for an interactive prompt that will allow\nyou to experiment.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/akuzko/rehash.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakuzko%2Frehash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakuzko%2Frehash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakuzko%2Frehash/lists"}