{"id":14969856,"url":"https://github.com/igorkasyanchuk/hasharay_ext","last_synced_at":"2025-10-08T18:11:35.045Z","repository":{"id":56875920,"uuid":"455241525","full_name":"igorkasyanchuk/hasharay_ext","owner":"igorkasyanchuk","description":"Painless work with complex Ruby hashes/arrays.","archived":false,"fork":false,"pushed_at":"2022-05-21T18:01:49.000Z","size":30,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-04T09:23:55.670Z","etag":null,"topics":["ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"https://www.railsjazz.com/","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/igorkasyanchuk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2022-02-03T16:29:30.000Z","updated_at":"2022-11-16T21:11:08.000Z","dependencies_parsed_at":"2022-08-20T11:30:53.530Z","dependency_job_id":null,"html_url":"https://github.com/igorkasyanchuk/hasharay_ext","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Fhasharay_ext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Fhasharay_ext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Fhasharay_ext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/igorkasyanchuk%2Fhasharay_ext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/igorkasyanchuk","download_url":"https://codeload.github.com/igorkasyanchuk/hasharay_ext/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233042736,"owners_count":18616064,"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":["ruby","ruby-on-rails"],"created_at":"2024-09-24T13:42:30.504Z","updated_at":"2025-09-17T16:32:18.275Z","avatar_url":"https://github.com/igorkasyanchuk.png","language":"Ruby","funding_links":["https://www.patreon.com/igorkasyanchuk","https://buymeacoffee.com/igorkasyanchuk"],"categories":["Ruby"],"sub_categories":[],"readme":"# Hash/Array Extenstion\n\n[![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)\n[![https://www.patreon.com/igorkasyanchuk](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/patron.svg?raw=true)](https://www.patreon.com/igorkasyanchuk)\n\n[![\"Buy Me A Coffee\"](https://github.com/igorkasyanchuk/get-smart/blob/main/docs/snapshot-bmc-button-small.png?raw=true)](https://buymeacoffee.com/igorkasyanchuk)\n\nThe idea of this gem is to simplify fetching records from the Hash/Array.\nI had a need to read values from the complex hashes, and I was tired of doing lots of \"dig/fetch\". Code was just ugly.\n\nSo, initial idea was to create a way to \"query\" Hash/Array similiar how we are querying CSS, but later idea was changed a little.\n\nRight now I've extracted my code into this gem and now can do the following:\n\nInstead of:\n```ruby\nh.fetch(:projects, []).map{|e| e[:name]}\n```\nI can just write:\n```ruby\nh.fpath('projects.name')\n```\n\nEven with a such simple example you can see that the code is much readable.\nIt was a nice win for my project and at least I'm very happy with it :)\n\nCheck more examples below to see if it can be useful for you too.\n\n## More Complex Example\n\n```ruby\n    # -----\n    # INITIAL DATA (see usage below)\n    # -----\n    hash = {\n      name: \"john\",\n      dob: Date.today,\n      projects: [ {name: \"A\", locations: [\"Kyiv\"]}, {name: \"B\", locations: [\"Paris\", \"Berlin\"]} ],\n      position: {\n        company: {\n          team: \"position1\",\n          office: \"position2\",\n          other: {\n            status: \"unknown\",\n            notes: [\"note a\", \"note b\"],\n            summaries: [\n              {worker: \"John\", level: \"middle\"},\n              {worker: \"Bob\", level: \"senior\"}\n            ]\n          }\n        }\n      },\n      locations: [ { city: \"Kyiv\", country: \"Ukraine\" }, { city: \"Odessa\", country: \"Ukraine\"}]\n    }\n\n    # -----\n    # USAGE with HASH\n    # -----\n    hash.fpath(\"name\") # =\u003e  \"john\"\n    hash.fetch_path(\"projects.name\") # =\u003e  [\"A\", \"B\"]\n    hash.fpath(\"projects.locations\") # =\u003e [[\"Kyiv\"], [\"Paris\", \"Berlin\"]]\n    hash.fpath!(\"position.company.other.summaries\") # =\u003e  [{\"level\" =\u003e \"middle\", \"worker\" =\u003e \"John\"}, {\"level\" =\u003e \"senior\", \"worker\" =\u003e \"Bob\"}]\n    hash.fetch_path!(\"position.company.other.status\") # =\u003e  \"unknown\"\n    hash.fpath(\"position.company.team+office\") # =\u003e  {\"team\" =\u003e \"position1\", \"office\" =\u003e \"position2\"}\n    hash.fpath(\"not_exising_name\") # =\u003e nil\n    hash.fpath(\"not_exising_projects.not_exising_name\") # =\u003e nil\n    hash.fpath(\"not_exising_projects.not_exising_name\", default: 42) # =\u003e 42\n\n    # USAGE with ARRAY\n    array = [{name: \"igor\"}, {name: \"john\"}]\n    array.fpath(\"name\") # =\u003e [\"igor\", \"john\"]\n\n    array = [{user: {first_name: \"john\"}}, {user: {first_name: \"bob\"}}]\n    array.fpath(\"user.first_name\") # =\u003e [\"john\", \"bob\"]\n```\n\n### Methods \u0026 Options\n\n`fpath` or `fetch_path` will return vakue based on path to the value. If nothing - nil.\n`fpath!` or `fetch_path!` working same way as `fpath` but raise error is some key is not available.\n\nAvailable options: `def fpath(key, strict: false, separator: \".\", default: nil)`.\n\nPay attention that if you have in your keys dots, you need to change separator. On my project I've only one work textual keys.\n\nYou can also return multiple values, pay attention to the `hash.fpath(\"position.company.team+office\")`. Note that \"+\" works only for the last key in the queried \"path\".\n\nNote that keys in the returned hash are stringified.\n\nMore examples available in the specs.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'hasharay_ext'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install hasharay_ext\n\n## Ideas\n\n- Add support for \"*\" operator to search in hash/array for needed values.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/igorkasyanchuk/hasharay_ext.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n[\u003cimg src=\"https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/more_gems.png?raw=true\"\n/\u003e](https://www.railsjazz.com/?utm_source=github\u0026utm_medium=bottom\u0026utm_campaign=hasharay_ext)\n\n[![\"Buy Me A Coffee\"](https://github.com/igorkasyanchuk/get-smart/blob/main/docs/snapshot-bmc-button.png?raw=true)](https://buymeacoffee.com/igorkasyanchuk)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figorkasyanchuk%2Fhasharay_ext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figorkasyanchuk%2Fhasharay_ext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figorkasyanchuk%2Fhasharay_ext/lists"}