https://github.com/ruby-oauth/snaky_hash
๐ A Hashie::Mash joint to improve #snake_life for everyone
https://github.com/ruby-oauth/snaky_hash
Keywords
hash hashie ruby rubygem underscore
Keywords from Contributors
version feature-flag
Last synced: about 22 hours ago
JSON representation
Repository metadata
๐ A Hashie::Mash joint to improve #snake_life for everyone
- Host: GitHub
- URL: https://github.com/ruby-oauth/snaky_hash
- Owner: ruby-oauth
- License: mit
- Created: 2025-05-18T18:27:16.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-12-02T03:37:29.000Z (about 1 month ago)
- Last Synced: 2025-12-09T20:47:26.382Z (about 1 month ago)
- Topics: hash, hashie, ruby, rubygem, underscore
- Language: Ruby
- Homepage: https://snaky-hash.galtzo.com/
- Size: 330 KB
- Stars: 6
- Watchers: 4
- Forks: 0
- Open Issues: 0
- Releases: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Security: SECURITY.md
README.md
๐ SnakyHash
This library is similar in purpose to the HashWithIndifferentAccess that is famously used in Rails, but does a lot more.
This gem is used by oauth and oauth2 gems to normalize hash keys to snake_case and lookups,
and provide a nice psuedo-object interface.
It can be thought of as a mashup of:
Rash(specifically therash_altflavor), which is a specialMash, made popular by thehashiegem, andserialized_hashiegem by krystal, rewritten, with some behavior changes
Classes that include SnakyHash::Snake.new should inherit from Hashie::Mash.
New for v2.0.2: Serialization Support
The serialization support is set to false by default, for backwards compatibility, but may be switched to true in the next major release, which will be v3. Example:
# This class has `dump` and `load` abilities!
class MyStringKeyedHash < Hashie::Mash
include SnakyHash::Snake.new(
key_type: :string,
serializer: true,
)
end
โจ Also new dump & load plugin extensions to control the way your data is dumped and loaded.
Note for use with oauth2 gem
The serializer is being introduced as a disabled option for backwards compatibility.
In snaky_hash v3 it will default to true.
If you want to start using the serializer immediately, reopen the SnakyHash::StringKeyed class and add the SnakyHash::Serializer module like this:
SnakyHash::StringKeyed.class_eval do
extend SnakyHash::Serializer
end
or you can create a custom class
class MyHash < Hashie::Mash
include SnakyHash::Snake.new(key_type: :string, serializer: true)
# Which is the same as:
# include SnakyHash::Snake.new(key_type: :string)
# extend SnakyHash::Serializer
end
You can then add serialization extensions as needed. See serialization and extensions for more.
| Federated DVCS Repository | Status | Issues | PRs | Wiki | CI | Discussions |
|---|---|---|---|---|---|---|
| ๐งช oauth-xx/snaky_hash on GitLab | The Truth | ๐ | ๐ | ๐ | ๐ Tiny Matrix | โ |
| ๐ง oauth-xx/snaky_hash on CodeBerg | An Ethical Mirror (Donate) | โ | ๐ | โ | โญ๏ธ No Matrix | โ |
| ๐ oauth-xx/snaky_hash on GitHub | A Dirty Mirror | ๐ | ๐ | โ | ๐ฏ Full Matrix | โ |
| ๐คผ OAuth Ruby Google Group | "Active" | โ | โ | โ | โ | ๐ |
| ๐ฎ๏ธ Discord Server | Let's | talk | about | this | library! |
Upgrading Runtime Gem Dependencies
Due to oauth and oauth2 gems depending on this gem,
this project sits underneath a large portion of the authorization systems on the internet.
That means it is painful for the Ruby community when this gem forces updates to its runtime dependencies.
As a result, great care, and a lot of time, have been invested to ensure this gem is working with all the
leading versions per each minor version of Ruby of all the runtime dependencies it can install with.
What does that mean specifically for the runtime dependencies?
We have 100% test coverage of lines and branches, and this test suite runs across a large matrix
covering the latest patch for each of the following minor versions:
- MRI Ruby @ v2.3, v2.4, v2.5, v2.6, v2.7, v3.0, v3.1, v3.2, v3.3, v3.4, HEAD
- NOTE: This gem will still install on ruby v2.2, but vanilla GitHub Actions no longer supports testing against it, so YMMV.
- JRuby @ v9.2, v9.3, v9.4, v10.0, HEAD
- TruffleRuby @ v23.1, v23.2, HEAD
- gem
hashie@ v0, v1, v2, v3, v4, v5, HEAD โฉ๏ธ hashie/hashie - gem
version_gem- @v1, HEAD โฉ๏ธ oauth-xx/version_gem
NOTE: version_gem, and this library, were both extracted from the ouaht2 gem. They are part of the oauth-xx org,
and are developed in tight collaboration with the oauth and oauth2 gems.
You should upgrade this gem with confidence*.
- This gem follows a strict & correct (according to the maintainer of SemVer; more info) interpretation of SemVer.
- Dropping support for any of the runtime dependency versions above will be a major version bump.
- If you aren't on one of the minor versions above, make getting there a priority.
- You should upgrade the dependencies of this gem with confidence*.
- Please do upgrade, and then, when it goes smooth as butter please sponsor me. Thanks!
* MIT license; I am unable to make guarantees.
| ๐ Test matrix brought to you by | ๐ appraisal++ |
|---|---|
| Adds back support for old Rubies | โจ appraisal PR #250 |
Adds support for eval_gemfile |
โจ appraisal PR #248 |
| Please review | my PRs! |
๐ก Info you can shake a stick at
| Tokens to Remember | |
|---|---|
| Works with JRuby | |
| Works with Truffle Ruby | |
| Works with MRI Ruby 3 | |
| Works with MRI Ruby 2 | |
| Source | |
| Documentation | |
| Compliance | |
| Style | |
| Support | |
| Enterprise Support | @Pointy Haired Boss: An enterprise support subscription is "never gonna let you down", and supports open source maintainers! |
| Comrade BDFL ๐๏ธ | |
... ๐ |
โจ Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add snaky_hash
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install snaky_hash
๐ Secure Installation
snaky_hash is cryptographically signed, and has verifiable SHA-256 and SHA-512 checksums by
stone_checksums. Be sure the gem you install hasnโt been tampered with
by following the instructions below.
Add my public key (if you havenโt already, expires 2045-04-29) as a trusted certificate:
gem cert --add <(curl -Ls https://raw.github.com/oauth-xx/snaky_hash/main/certs/pboling.pem)
You only need to do that once. Then proceed to install with:
gem install snaky_hash -P MediumSecurity
The MediumSecurity trust profile will verify signed gems, but allow the installation of unsigned dependencies.
This is necessary because not all of snaky_hashโs dependencies are signed, so we cannot use HighSecurity.
If you want to up your security game full-time:
bundle config set --global trust-policy MediumSecurity
NOTE: Be prepared to track down certs for signed gems and add them the same way you added mine.
๐ง Basic Usage
class MySnakedHash < Hashie::Mash
include SnakyHash::Snake.new(key_type: :string) # or :symbol
end
snake = MySnakedHash.new(:a => "a", "b" => "b", 2 => 2, "VeryFineHat" => "Feathers")
snake.a # => 'a'
snake.b # => 'b'
snake[2] # => 2
snake["2"] # => nil, note that this gem only affects string / symbol keys.
snake.very_fine_hat # => 'Feathers'
snake[:very_fine_hat] # => 'Feathers'
snake["very_fine_hat"] # => 'Feathers'
Note above that you can access the values via the string, or symbol.
The key_type determines how the key is actually stored, but the hash acts as "indifferent".
Note also that keys which do not respond to to_sym, because they don't have a natural conversion to a Symbol,
are left as-is.
Serialization
class MySerializedSnakedHash < Hashie::Mash
include SnakyHash::Snake.new(
key_type: :symbol, # default :string
serializer: true, # default: false
)
end
snake = MySerializedSnakedHash.new(:a => "a", "b" => "b", 2 => 2, "VeryFineHat" => "Feathers") # => {a: "a", b: "b", 2 => 2, very_fine_hat: "Feathers"}
dump = MySerializedSnakedHash.dump(snake) # => "{\"a\":\"a\",\"b\":\"b\",\"2\":2,\"very_fine_hat\":\"Feathers\"}"
hydrated = MySerializedSnakedHash.load(dump) # => {a: "a", b: "b", "2": 2, very_fine_hat: "Feathers"}
hydrated.class # => MySerializedSnakedHash
hydrated.a # => 'a'
hydrated.b # => 'b'
hydrated[2] # => nil # NOTE: this is the opposite of snake[2] => 2
hydrated["2"] # => 2 # NOTE: this is the opposite of snake["2"] => nil
hydrated.very_fine_hat # => 'Feathers'
hydrated[:very_fine_hat] # => 'Feathers'
hydrated["very_fine_hat"] # => 'Feathers'
Note that the key VeryFineHat changed to very_fine_hat.
That is indeed the point of this library, so not a bug.
Note that the key 2 changed to "2" (because JSON keys are strings).
When the JSON dump was reloaded it did not know to restore it as 2 instead of "2".
This is also not a bug, though if you need different behavior, there is a solution in the next section.
Extensions
You can write your own arbitrary extensions:
- "Hash Load" extensions operate on the hash and nested hashes
- use
::load_hash_extensions.add(:extension_name) { |hash| } - since v2.0.2, bugs fixed in v2.0.3
- use
- "Value Load" extensions operate on the values, and nested hashes' values, if any
- use
::load_value_extensions.add(:extension_name) { |value| } - since v2.0.2, bugs fixed in v2.0.3
- use
- "Hash Dump" extensions operate on the hash and nested hashes
- use
::dump_hash_extensions.add(:extension_name) { |value| } - since v2.0.3
- use
- "Value Dump" extensions operate on the values, and nested hashes' values, if any
- use
::dump_value_extensions.add(:extension_name) { |value| } - since v2.0.2, bugs fixed in v2.0.3
- use
Example
Let's say I want to really smash up my hash and make it more food-like.
class MyExtSnakedHash < Hashie::Mash
include SnakyHash::Snake.new(
key_type: :symbol, # default :string
serializer: true, # default: false
)
end
# We could swap all values with indexed apples (obliteraating nested data!)
MyExtSnakedHash.dump_hash_extensions.add(:to_apple) do |value|
num = 0
value.transform_values do |_key|
key = "apple-#{num}"
num += 1
key
end
end
# And then when loading the dump we could convert the yum to pear
MyExtSnakedHash.load_hash_extensions.add(:apple_to_pear) do |value|
value.transform_keys do |key|
key.to_s.sub("yum", "pear")
end
end
# We could swap all index numbers "beet-<number>"
MyExtSnakedHash.dump_value_extensions.add(:to_beet) do |value|
value.to_s.sub(/(\d+)/) { |match| "beet-#{match[0]}" }
end
# And then when loading the dump we could convert beet to corn
MyExtSnakedHash.load_value_extensions.add(:beet_to_corn) do |value|
value.to_s.sub("beet", "corn")
end
snake = MyExtSnakedHash.new({"YumBread" => "b", "YumCake" => {"b" => "b"}, "YumBoba" => [1, 2, 3]})
snake # => {yum_bread: "b", yum_cake: {b: "b"}, yum_boba: [1, 2, 3]}
snake.yum_bread # => "b"
snake.yum_cake # => {b: "b"}
snake.yum_boba # => [1, 2, 3]
dump = snake.dump
dump # => "{\"yum_bread\":\"apple-beet-0\",\"yum_cake\":\"apple-beet-1\",\"yum_boba\":\"apple-beet-2\"}"
hydrated = MyExtSnakedHash.load(dump)
hydrated # => {pear_bread: "apple-corn-0", pear_cake: "apple-corn-1", pear_boba: "apple-corn-2"}
See the specs for more examples.
Bad Ideas
I don't recommend using these features... but they exist (for now).
You can still access the original un-snaked camel keys.
And through them you can even use un-snaked camel methods.
But don't.
snake = SnakyHash::StringKeyed["VeryFineHat" => "Feathers"]
snake.key?("VeryFineHat") # => true
snake["VeryFineHat"] # => 'Feathers'
snake.VeryFineHat # => 'Feathers', PLEASE don't do this!!!
snake["VeryFineHat"] = "pop" # Please don't do this... you'll get a warning, and it works (for now), but no guarantees.
# WARN -- : You are setting a key that conflicts with a built-in method MySnakedHash#VeryFineHat defined in MySnakedHash. This can cause unexpected behavior when accessing the key as a property. You can still access the key via the #[] method.
# => "pop"
Since you are reading this, here's what to do instead.
snake.very_fine_hat = "pop" # => 'pop', do this instead!!!
snake.very_fine_hat # => 'pop'
snake[:very_fine_hat] = "moose" # => 'moose', or do this instead!!!
snake.very_fine_hat # => 'moose'
snake["very_fine_hat"] = "cheese" # => 'cheese', or do this instead!!!
snake.very_fine_hat # => 'cheese'
๐ Release Instructions
See CONTRIBUTING.md.
๐ Security
See SECURITY.md.
๐ค Contributing
If you need some ideas of where to help, you could work on adding more code coverage,
or if it is already ๐ฏ (see below) check issues, or PRs,
or use the gem and think about how it could be better.
We so if you make changes, remember to update it.
See CONTRIBUTING.md for more detailed instructions.
Code Coverage
๐ช Code of Conduct
Everyone interacting with this project's codebases, issue trackers,
chat rooms and mailing lists agrees to follow the .
๐ Contributors
Made with contributors-img.
Also see GitLab Contributors: https://gitlab.com/oauth-xx/snaky_hash/-/graphs/main
โญ๏ธ Star History
๐ Versioning
This Library adheres to .
Violations of this scheme should be reported as bugs.
Specifically, if a minor or patch version is released that breaks backward compatibility,
a new version should be immediately released that restores compatibility.
Breaking changes to the public API will only be introduced with new major versions.
๐ Is "Platform Support" part of the public API?
Yes. But I'm obligated to include notes...
SemVer should, but doesn't explicitly, say that dropping support for specific Platforms
is a breaking change to an API.
It is obvious to many, but not all, and since the spec is silent, the bike shedding is endless.
dropping support for a platform is both obviously and objectively a breaking change
- Jordan Harband (@ljharb, maintainer of SemVer) in SemVer issue 716
To get a better understanding of how SemVer is intended to work over a project's lifetime,
read this article from the creator of SemVer:
As a result of this policy, and the interpretive lens used by the maintainer,
you can (and should) specify a dependency on these libraries using
the Pessimistic Version Constraint with two digits of precision.
For example:
spec.add_dependency("snaky_hash", "~> 2.0")
See CHANGELOG.md for list of releases.
๐ License
The gem is available as open source under the terms of
the MIT License .
See LICENSE.txt for the official Copyright Notice.
ยฉ Copyright
๐ค One more thing
You made it to the bottom of the page,
so perhaps you'll indulge me for another 20 seconds.
I maintain many dozens of gems, including this one,
because I want Ruby to be a great place for people to solve problems, big and small.
Please consider supporting my efforts via the giant yellow link below,
or one of the others at the head of this README.
CodeCov currently fails to parse the coverage upload.
Citation (CITATION.cff)
cff-version: 1.2.0
title: snaky_hash
message: >-
If you use this work and you want to cite it,
then you can use the metadata from this file.
type: software
authors:
- given-names: Peter Hurn
family-names: Boling
email: peter@railsbling.com
affiliation: railsbling.com
orcid: 'https://orcid.org/0009-0008-8519-441X'
- given-names: Aboling0
email: aboling@railsbling.com
affiliation: railsbling.com
identifiers:
- type: url
value: 'https://github.com/oauth-xx/snaky_hash'
description: snaky_hash
repository-code: 'https://github.com/oauth-xx/snaky_hash'
abstract: >-
snaky_hash
license: See license file
Owner metadata
- Name: Ruby OAuth
- Login: ruby-oauth
- Email: floss@galtzo.com
- Kind: organization
- Description: ๐ Ruby Gems for building with OAuth v1.0a, 2.0, 2.1 and OIDC
- Website: https://opencollective.com/ruby-oauth
- Location: United States of America
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/40009?v=4
- Repositories: 7
- Last ynced at: 2025-12-09T21:39:40.258Z
- Profile URL: https://github.com/ruby-oauth
GitHub Events
Total
- Watch event: 1
- Delete event: 1
- Push event: 1
- Pull request event: 1
- Create event: 1
Last Year
- Watch event: 1
- Delete event: 1
- Push event: 1
- Pull request event: 1
- Create event: 1
Committers metadata
Last synced: 2 days ago
Total Commits: 76
Total Committers: 2
Avg Commits per committer: 38.0
Development Distribution Score (DDS): 0.039
Commits in past year: 51
Committers in past year: 2
Avg Commits per committer in past year: 25.5
Development Distribution Score (DDS) in past year: 0.059
| Name | Commits | |
|---|---|---|
| Peter H. Boling | p****g@g****m | 73 |
| dependabot[bot] | 4****] | 3 |
Issue and Pull Request metadata
Last synced: about 1 month ago
Total issues: 0
Total pull requests: 3
Average time to close issues: N/A
Average time to close pull requests: 23 days
Total issue authors: 0
Total pull request authors: 1
Average comments per issue: 0
Average comments per pull request: 0.0
Merged pull request: 3
Bot issues: 0
Bot pull requests: 3
Past year issues: 0
Past year pull requests: 3
Past year average time to close issues: N/A
Past year average time to close pull requests: 23 days
Past year issue authors: 0
Past year pull request authors: 1
Past year average comments per issue: 0
Past year average comments per pull request: 0.0
Past year merged pull request: 3
Past year bot issues: 0
Past year bot pull requests: 3
Top Issue Authors
Top Pull Request Authors
- dependabot[bot] (3)
Top Issue Labels
Top Pull Request Labels
- dependencies (3)
- github_actions (3)
Dependencies
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- github/codeql-action/analyze v3 composite
- github/codeql-action/autobuild v3 composite
- github/codeql-action/init v3 composite
- actions/checkout v4 composite
- codecov/codecov-action v5 composite
- coverallsapp/github-action master composite
- irongut/CodeCoverageSummary v1.3.0 composite
- marocchino/sticky-pull-request-comment v2 composite
- qltysh/qlty-action/coverage main composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- actions/dependency-review-action v4 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- actions/checkout v4 composite
- ruby/setup-ruby v1 composite
- appraisal >= 0
- debug >= 1.0.0
- gem_bench ~> 2.0, >= 2.0.5
- ansi 1.5.0
- appraisal 3.0.0.rc1
- ast 2.4.3
- backports 3.25.1
- benchmark 0.4.0
- bigdecimal 3.1.9
- bundler 2.6.9
- bundler-audit 0.9.2
- concurrent-ruby 1.3.5
- date 3.4.1
- debug 1.10.0
- diff-lcs 1.6.2
- diffy 3.4.3
- docile 1.4.1
- dry-configurable 1.3.0
- dry-core 1.1.0
- dry-inflector 1.2.0
- dry-initializer 3.2.0
- dry-logic 1.6.0
- dry-schema 1.14.1
- dry-types 1.8.2
- erb 5.0.1
- gem_bench 2.0.5
- hashie 5.0.0
- io-console 0.8.0
- irb 1.15.2
- json 2.12.0
- kettle-soup-cover 1.0.9
- kramdown 2.5.1
- kramdown-parser-gfm 1.1.0
- language_server-protocol 3.17.0.5
- lint_roller 1.1.0
- logger 1.7.0
- nokogiri 1.18.8
- ostruct 0.6.1
- parallel 1.27.0
- parser 3.3.8.0
- pp 0.6.2
- prettyprint 0.2.0
- prism 1.4.0
- psych 5.2.6
- racc 1.8.1
- rainbow 3.1.1
- rake 13.2.1
- rdoc 6.14.0
- reek 6.5.0
- regexp_parser 2.10.0
- reline 0.6.1
- rexml 3.4.1
- rspec 3.13.0
- rspec-block_is_expected 1.0.6
- rspec-core 3.13.3
- rspec-expectations 3.13.4
- rspec-mocks 3.13.4
- rspec-support 3.13.3
- rubocop 1.75.6
- rubocop-ast 1.44.1
- rubocop-gradual 0.3.6
- rubocop-lts 8.1.1
- rubocop-md 1.2.4
- rubocop-packaging 0.6.0
- rubocop-performance 1.25.0
- rubocop-rake 0.7.1
- rubocop-rspec 3.6.0
- rubocop-ruby2_2 2.0.5
- rubocop-shopify 2.17.0
- rubocop-thread_safety 0.7.2
- ruby-progressbar 1.13.0
- simplecov 0.22.0
- simplecov-cobertura 2.1.0
- simplecov-console 0.9.3
- simplecov-html 0.13.1
- simplecov-lcov 0.8.0
- simplecov-rcov 0.3.7
- simplecov_json_formatter 0.1.4
- snaky_hash 2.0.1
- standard 1.50.0
- standard-custom 1.0.2
- standard-performance 1.8.0
- standard-rubocop-lts 1.0.10
- stone_checksums 1.0.0
- stringio 3.1.7
- terminal-table 4.0.0
- thor 1.3.2
- unicode-display_width 3.1.4
- unicode-emoji 4.0.4
- version_gem 1.1.8
- yard 0.9.37
- yard-junk 0.0.10
- yard-relative_markdown_links 0.5.0
- zeitwerk 2.7.3
- rake ~> 13.0 development
- rspec ~> 3.13 development
- rspec-block_is_expected ~> 1.0, >= 1.0.6 development
- stone_checksums ~> 1.0 development
- hashie >= 0
- version_gem >= 1.1.8, < 3
Score: 2.4849066497880004