Skip to content

Commit 808651b

Browse files
committed
Update Readme and Changelog
1 parent 2619313 commit 808651b

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog
2+
3+
## [2.0.0] - 2024-04-20
4+
5+
- Completely rewrite internals and remove Active Support as a dependency
6+
(this means HashWithDotAccess::Hash supports string, symbol, and dot access with zero dependencies)
7+
- Dot access now writes accessors to the class, improving performance (`method_missing` only runs once per key/accessor, essentially)
8+
9+
## [1.2.0] - 2021-12-16
10+
11+
- Support Rails 7

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Hash with Dot Access
22

3-
Rails' ActiveSupport gem provides `HashWithIndifferentAccess` which allows you to access hash keys with either strings or symbols. This gem provides `HashWithDotAccess` which subclasses `HashWithIndifferentAccess` and allows you to access or set those keys using dot access (aka methods). It utilizes `method_missing` to access key data if available, and you can also set data using `keyname=`.
3+
This gem provides a Ruby `Hash` subclass which lets you access hash values with either string or symbol keys, as well as via methods (aka dot access). It utilizes `method_missing` to access key data if available, and you can also set data using `keyname=`. Our goal is on providing good performance and if anything offering a _subset_ of standard Hash functionality (it's a non-goal to add all-new Hash-related functionality to this class).
4+
5+
Performance is improved over long-running processes (such as the build process of the [Bridgetown](https://www.bridgetownrb.com) framework) by automatically defining accessors on the class so that `method_missing` is only called once per key/accessor pair.
46

57
## Example
68

@@ -26,13 +28,17 @@ hsh[:d]
2628
hsh["d"]
2729
# => "Indeed!"
2830

29-
hsh2 = {test: "dot access"}.with_dot_access
31+
# You can use the `as_dots` method on Hash by loading in our refinement.
32+
33+
using HashWithDotAccess::Refinements
34+
35+
hsh2 = {test: "dot access"}.as_dots
3036
hsh2.test
3137
# => "dot access"
3238

3339
## Nested hashes work too! Pairs nicely with lonely operator: &.
3440

35-
nested = {a: 1, b: {c: 3}}.with_dot_access
41+
nested = {a: 1, b: {c: 3}}.as_dots
3642
nested.b.c
3743
# => 3
3844

@@ -41,7 +47,7 @@ nested&.d&.e&.f
4147

4248
## You can also set default return values when key is missing
4349

44-
hsh = {a: 1, b: 2}.with_dot_access
50+
hsh = {a: 1, b: 2}.as_dots
4551
hsh.default = 0
4652
hsh.a
4753
# => 1
@@ -63,12 +69,22 @@ Then simply require `hash_with_dot_access`:
6369
require "hash_with_dot_access"
6470
```
6571

72+
> [!IMPORTANT]
73+
> If you're upgrading from an earlier version, and you don't want to modify your code away from using `with_dot_access`, you can add a monkey-patch to `Hash`:
74+
> ```ruby
75+
> class Hash
76+
> def with_dot_access
77+
> HashWithDotAccess::Hash.new(self)
78+
> end
79+
> end
80+
> ```
81+
6682
## Caveats
6783
6884
As with any Ruby object which provides arbitrary data through dynamic method calls, you may encounter collisions between your key names and existing `Hash` methods. For example:
6985
7086
```ruby
71-
hsh = {each: "this won't work!"}.with_dot_access
87+
hsh = {each: "this won't work!"}.as_dots
7288
hsh.each
7389
# => #<Enumerator: {"each"=>"this won't work!"}:each>
7490
#
@@ -78,7 +94,7 @@ hsh.each
7894
Of course, the easy fix is to simply use standard ways of accessing hash data in these cases:
7995

8096
```ruby
81-
hsh = {each: "this will work!"}.with_dot_access
97+
hsh = {each: "this will work!"}.as_dots
8298
hsh[:each]
8399
# => "this will work!"
84100
```

hash_with_dot_access.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ Gem::Specification.new do |spec|
66
spec.author = "Bridgetown Team"
77
spec.email = "[email protected]"
88

9-
spec.summary = %q{Subclass of HashWithIndifferentAccess which provides dot access via method_missing}
9+
spec.summary = %q{Performance-oriented subclass of Hash which provides symbolized keys and method access}
1010
spec.homepage = "https://github.com/bridgetownrb/hash_with_dot_access"
1111
spec.license = "MIT"
1212

13+
spec.required_ruby_version = ">= 3.1.0"
14+
1315
# Specify which files should be added to the gem when it is released.
1416
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
1517
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do

0 commit comments

Comments
 (0)