https://github.com/aaronlasseigne/tash
A hash that allows for transformation of its keys.
https://github.com/aaronlasseigne/tash
Last synced: about 1 month ago
JSON representation
A hash that allows for transformation of its keys.
- Host: GitHub
- URL: https://github.com/aaronlasseigne/tash
- Owner: AaronLasseigne
- License: mit
- Created: 2022-05-29T03:04:25.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-30T22:44:35.000Z (over 2 years ago)
- Last Synced: 2025-03-14T17:44:45.761Z (about 1 month ago)
- Language: Ruby
- Size: 90.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# [Tash][]
Tash is a hash that allows for transformation of its keys.
A transformation block is given to change the key.
Keys can be looked up with any value that transforms into the same key.
This means a hash can be string/symbol insensitive, case insensitive, can convert camel case JSON keys to snake case Ruby keys, or anything else based on the block you provide.[](https://rubygems.org/gems/tash)
[](https://github.com/AaronLasseigne/tash/actions?query=workflow%3ATest)---
## Installation
Add it to your Gemfile:
``` rb
gem 'tash', '~> 1.0'
```Or install it manually:
``` sh
$ gem install tash --version '~> 1.0'
```This project uses [Semantic Versioning][].
Check out [GitHub releases][] for a detailed list of changes.## Usage
Let's say that you wanted to have a hash where the keys are accessible as strings or symbols (i.e. `ActiveSupport::HashWithIndifferentAccess`).
``` rb
t = Tash[one: 1, two: 2, &:to_s]
# => {"one"=>1, "two"=>2}t[:one]
# => 1t['one']
# => 1t[:three] = 9 # oops
# => 9t['three'] = 3
# => 3t[:three]
# => 3t['three']
# => 3
```Lets say that you recieve a series of camel case JSON keys from an API call but want to access the information with Rubys typical snake case style and symbolized.
``` rb
json = { "firstName" => "Adam", "lastName" => "DeCobray" }t = Tash[json] do |key|
key
.to_s
.gsub(/(? "Adam"t['firstName']
# => "Adam"
```This also works with pattern matching:
``` rb
t = Tash[ONE: 1, MORE: 200, &:downcase]case t
in { One: 1, More: more }
more
else
nil
end
# => 200
```Tash implements `to_hash` for implicit hash conversion making it usable nearly everywhere you use a hash.
Tash has every instance method Hash has except for `transform_keys` and `transform_keys!`.
[API Documentation][]
## Contributing
If you want to contribute to Tash, please read [our contribution guidelines][].
A [complete list of contributors][] is available on GitHub.## License
Tash is licensed under [the MIT License][].
[Tash]: https://github.com/AaronLasseigne/tash
[semantic versioning]: http://semver.org/spec/v2.0.0.html
[GitHub releases]: https://github.com/AaronLasseigne/tash/releases
[API Documentation]: http://rubydoc.info/github/AaronLasseigne/tash
[our contribution guidelines]: CONTRIBUTING.md
[complete list of contributors]: https://github.com/AaronLasseigne/tash/graphs/contributors
[the mit license]: LICENSE.txt