activerecord-hashlike_access 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -9
- data/.yardopts +4 -3
- data/CHANGELOG.md +22 -3
- data/Gemfile.lock +2 -2
- data/README.md +4 -1
- data/activerecord-hashlike_access.gemspec +29 -23
- data/docs/ActiveRecord/HashlikeAccess.html +6 -11
- data/docs/ActiveRecord.html +1 -3
- data/docs/_index.html +1 -29
- data/docs/class_list.html +1 -1
- data/docs/file.README.html +13 -11
- data/docs/index.html +13 -11
- data/docs/top-level-namespace.html +1 -11
- data/lib/active_record/hashlike_access/auto_extend.rb +5 -2
- data/lib/active_record/hashlike_access/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e835fde2ee7fd198f65b0bf3afc769cb5b2c1327352a0334e002aca1293f48b7
|
|
4
|
+
data.tar.gz: 9587bfc94990b6687a4e70d14b2e3d76daf26e436bd5ae3eb506c6dd9d299771
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 81a321912a7140faabf6bcc4de8a95f77b94cbced66f539e2bc8a1828b2924551cdc5b652d9f46f42dbcfacd3551762fa2eacba05cb2e6929ac90abf33546a6b
|
|
7
|
+
data.tar.gz: de8dd3bab32184feed01b560b52e5b54d20d7e4c396cb3421b903d2e3197dd98b7d8c102957f3b41ab88c05c0e2afbf76484fc3a6bd3e86278c71ecca619e16d
|
data/.rubocop.yml
CHANGED
|
@@ -35,16 +35,9 @@ Metrics/MethodLength:
|
|
|
35
35
|
|
|
36
36
|
Naming/VariableNumber:
|
|
37
37
|
# Use the configured style when numbering symbols, methods and variables.
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
Exclude:
|
|
39
|
+
- spec/**/*
|
|
40
40
|
|
|
41
|
-
Style/AsciiComments:
|
|
42
|
-
# Use only ASCII symbols in comments.
|
|
43
|
-
#
|
|
44
|
-
# NLC: We're allowing non-breaking spaces (' '), as they're needed to prevent
|
|
45
|
-
# browsers from collapsing multiple spaces in documentation code blocks.
|
|
46
|
-
AllowedChars:
|
|
47
|
-
- ' '
|
|
48
41
|
|
|
49
42
|
Style/FormatString:
|
|
50
43
|
# Enforce the use of Kernel#sprintf, Kernel#format or String#%.
|
data/.yardopts
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## v0.2.2 (2026-09-29)
|
|
4
|
+
- **Chores**:
|
|
5
|
+
- Corrected Gemspec *changelog* URI. 🤦
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## v0.2.1 (2026-09-29)
|
|
8
|
+
- **Documentation**:
|
|
9
|
+
- Removed `ActiveRecord::Base` from the class list.
|
|
10
|
+
- Added gem version badge to README.
|
|
11
|
+
- Added full documentation link to README.
|
|
12
|
+
- **Chores**:
|
|
13
|
+
- Corrected typos in Gemspec URIs.
|
|
14
|
+
|
|
15
|
+
## v0.2.0 (2026-08-18)
|
|
16
|
+
- **Features**: Added a `require` entrypoint that provides hash-like access to all models.
|
|
17
|
+
|
|
18
|
+
## v0.1.1 (2026-08-18)
|
|
19
|
+
- **Chores**: Patch-bump to correct release version.
|
|
20
|
+
|
|
21
|
+
## v0.1.0 (2026-08-18)
|
|
22
|
+
(Note this was incorrectly released as version `0.0.0`.)
|
|
23
|
+
- **Features**: Initial "hash-like access" implementation.
|
|
24
|
+
- **Testing**: Proper testing of all expected behaviors.
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
activerecord-hashlike_access (0.
|
|
4
|
+
activerecord-hashlike_access (0.2.1)
|
|
5
5
|
activerecord (>= 6)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
@@ -145,7 +145,7 @@ DEPENDENCIES
|
|
|
145
145
|
CHECKSUMS
|
|
146
146
|
activemodel (8.1.3.1) sha256=99cc02ce2faec371d14440949d85787ebd23a907c9baef0a9d4bcd4d21888f88
|
|
147
147
|
activerecord (8.1.3.1) sha256=0a2fb6c28f4938f6b013a3a549bec0a7e37d535f3dc8990e804bcc3258c0403b
|
|
148
|
-
activerecord-hashlike_access (0.
|
|
148
|
+
activerecord-hashlike_access (0.2.1)
|
|
149
149
|
activesupport (8.1.3.1) sha256=85458765f25ea48b9019c46b6bb3fa5683197bf4280d9f06710a6e8d7a831376
|
|
150
150
|
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
|
|
151
151
|
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
|
data/README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
[](https://rubygems.org/gems/activerecord-hashlike_access)
|
|
1
2
|
[](https://tldrlegal.com/license/mit-license)
|
|
2
3
|
|
|
3
4
|
|
|
@@ -42,6 +43,8 @@ Role[name] = new_display_text
|
|
|
42
43
|
|
|
43
44
|
Importantly, note that **this gem works on the model, not its relations**. ActiveRecord's existing bracket constructs for referencing _relations_ (i.e. resultsets) in an `Array`-like fashion will continue to work as they always have.
|
|
44
45
|
|
|
46
|
+
[Full documentation is available here](https://nestor-custodio.github.io/activerecord-hashlike_access/ActiveRecord/HashlikeAccess.html), but do read below for a crash course on availble featues!
|
|
47
|
+
|
|
45
48
|
|
|
46
49
|
## Installation
|
|
47
50
|
|
|
@@ -228,7 +231,7 @@ Every instance of a hash-like lookup (`[some_key]`) is backed by a `find_by`/`fi
|
|
|
228
231
|
|
|
229
232
|
## Potential Gotchas
|
|
230
233
|
|
|
231
|
-
As with anything ActiveRecord-related, there are
|
|
234
|
+
As with anything ActiveRecord-related, there are ways to shoot yourself in the foot if you're not careful. Here are a few items to keep in mind and hopefully prevent this:
|
|
232
235
|
|
|
233
236
|
- The hash-like access construct is essentially syntax sugar around ActiveRecord's `find_by` mechanism. For many (most?) database engines, selecting (or _including_, in the case of a multi-field key) an unindexed field for your lookups will likely result in a full-table scan with every hash-like access. Be mindful of what field(s) you're using as your key(s).
|
|
234
237
|
|
|
@@ -1,33 +1,39 @@
|
|
|
1
1
|
require_relative 'lib/active_record/hashlike_access/version'
|
|
2
2
|
|
|
3
3
|
Gem::Specification.new do |spec|
|
|
4
|
-
spec.name = 'activerecord-hashlike_access'
|
|
5
|
-
spec.version = ActiveRecord::HashlikeAccess::VERSION
|
|
6
|
-
spec.authors = ['Nestor Custodio']
|
|
7
|
-
spec.email = ['nestor@custodio.org']
|
|
8
|
-
|
|
9
|
-
spec.summary = 'Provides Hash-like access to ActiveRecord models.'
|
|
10
|
-
spec.homepage = 'https://github.com/nestor-custodio/activerecord-hashlike_acces'
|
|
11
|
-
spec.license = 'MIT'
|
|
12
4
|
spec.required_ruby_version = '>= 3.4'
|
|
13
5
|
|
|
14
|
-
spec.
|
|
15
|
-
|
|
16
|
-
spec.
|
|
17
|
-
spec.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
spec.bindir = 'exe'
|
|
28
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
6
|
+
spec.name = 'activerecord-hashlike_access'
|
|
7
|
+
spec.version = ActiveRecord::HashlikeAccess::VERSION
|
|
8
|
+
spec.authors = ['Nestor Custodio']
|
|
9
|
+
spec.email = ['nestor@custodio.org']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'Provides Hash-like access to ActiveRecord models.'
|
|
12
|
+
spec.homepage = 'https://github.com/nestor-custodio/activerecord-hashlike_access'
|
|
13
|
+
spec.license = 'MIT'
|
|
14
|
+
|
|
15
|
+
spec.files = `git ls-files -z`.split("\x0") - Dir['.[!.]*/**/*', 'bin/**/*', 'spec/**/*', '.git*']
|
|
16
|
+
spec.executables = []
|
|
29
17
|
spec.require_paths = ['lib']
|
|
30
18
|
|
|
19
|
+
# ---
|
|
20
|
+
|
|
21
|
+
spec.metadata = {
|
|
22
|
+
# See: https://guides.rubygems.org/specification-reference/#metadata
|
|
23
|
+
|
|
24
|
+
# RubyGems.org Security
|
|
25
|
+
#
|
|
26
|
+
'allowed_push_host' => 'https://rubygems.org',
|
|
27
|
+
'rubygems_mfa_required' => 'true',
|
|
28
|
+
|
|
29
|
+
# Metadata-Provided URIs
|
|
30
|
+
#
|
|
31
|
+
'homepage_uri' => spec.homepage,
|
|
32
|
+
'changelog_uri' => "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
# ---
|
|
36
|
+
|
|
31
37
|
# This of course requires ActiveRecord.
|
|
32
38
|
#
|
|
33
39
|
spec.add_dependency 'activerecord', '>= 6'
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
<div id="menu">
|
|
40
40
|
|
|
41
41
|
<a href="../_index.html">Index (H)</a> »
|
|
42
|
-
<span class='title'><span class='object_link'
|
|
42
|
+
<span class='title'><span class='object_link'>ActiveRecord</span></span>
|
|
43
43
|
»
|
|
44
44
|
<span class="title">HashlikeAccess</span>
|
|
45
45
|
|
|
@@ -76,11 +76,6 @@
|
|
|
76
76
|
|
|
77
77
|
|
|
78
78
|
|
|
79
|
-
<dl>
|
|
80
|
-
<dt>Included in:</dt>
|
|
81
|
-
<dd><span class='object_link'><a href="Base.html" title="ActiveRecord::Base (class)">Base</a></span></dd>
|
|
82
|
-
</dl>
|
|
83
|
-
|
|
84
79
|
|
|
85
80
|
|
|
86
81
|
<dl>
|
|
@@ -115,7 +110,7 @@
|
|
|
115
110
|
<dt id="VERSION-constant" class="">VERSION =
|
|
116
111
|
|
|
117
112
|
</dt>
|
|
118
|
-
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>0.2.
|
|
113
|
+
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>0.2.2</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
|
|
119
114
|
|
|
120
115
|
</dl>
|
|
121
116
|
|
|
@@ -706,7 +701,7 @@ All but the last of these is passed through, unaltered, to <span class='object_l
|
|
|
706
701
|
<div class="method_details ">
|
|
707
702
|
<h3 class="signature " id="hashlike_access_lookup-instance_method">
|
|
708
703
|
|
|
709
|
-
#<strong>hashlike_access_lookup</strong>(*lookup_values) ⇒ <tt><span class='object_link'
|
|
704
|
+
#<strong>hashlike_access_lookup</strong>(*lookup_values) ⇒ <tt><span class='object_link'>ActiveRecord::Base</span></tt><sup>?</sup> <span class="extras">(private)</span>
|
|
710
705
|
|
|
711
706
|
|
|
712
707
|
|
|
@@ -744,7 +739,7 @@ All but the last of these is passed through, unaltered, to <span class='object_l
|
|
|
744
739
|
<li>
|
|
745
740
|
|
|
746
741
|
|
|
747
|
-
<span class='type'>(<tt><span class='object_link'
|
|
742
|
+
<span class='type'>(<tt><span class='object_link'>ActiveRecord::Base</span></tt>, <tt>nil</tt>)</span>
|
|
748
743
|
|
|
749
744
|
|
|
750
745
|
|
|
@@ -786,7 +781,7 @@ All but the last of these is passed through, unaltered, to <span class='object_l
|
|
|
786
781
|
|
|
787
782
|
<span class='kw'>def</span> <span class='id identifier rubyid_hashlike_access_lookup'>hashlike_access_lookup</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_lookup_values'>lookup_values</span><span class='rparen'>)</span>
|
|
788
783
|
<span class='id identifier rubyid_record'>record</span> <span class='op'>=</span> <span class='id identifier rubyid_find_by'>find_by</span> <span class='id identifier rubyid_hashlike_access_config'>hashlike_access_config</span><span class='lbracket'>[</span><span class='symbol'>:lookup_fields</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_zip'>zip</span><span class='lparen'>(</span><span class='id identifier rubyid_lookup_values'>lookup_values</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_h'>to_h</span>
|
|
789
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'
|
|
784
|
+
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'>RecordNotFound</span> <span class='kw'>if</span> <span class='id identifier rubyid_record'>record</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>&&</span> <span class='id identifier rubyid_hashlike_access_config'>hashlike_access_config</span><span class='lbracket'>[</span><span class='symbol'>:raise_if_not_found</span><span class='rbracket'>]</span>
|
|
790
785
|
|
|
791
786
|
<span class='id identifier rubyid_record'>record</span>
|
|
792
787
|
<span class='kw'>end</span></pre>
|
|
@@ -800,7 +795,7 @@ All but the last of these is passed through, unaltered, to <span class='object_l
|
|
|
800
795
|
</div>
|
|
801
796
|
|
|
802
797
|
<div id="footer">
|
|
803
|
-
Generated on Tue
|
|
798
|
+
Generated on Tue Sep 29 01:20:55 2026 by
|
|
804
799
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
805
800
|
0.9.45 (ruby-3.4.8).
|
|
806
801
|
</div>
|
data/docs/ActiveRecord.html
CHANGED
|
@@ -96,8 +96,6 @@
|
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
|
|
99
|
-
<strong class="classes">Classes:</strong> <span class='object_link'><a href="ActiveRecord/Base.html" title="ActiveRecord::Base (class)">Base</a></span>
|
|
100
|
-
|
|
101
99
|
|
|
102
100
|
</p>
|
|
103
101
|
|
|
@@ -112,7 +110,7 @@
|
|
|
112
110
|
</div>
|
|
113
111
|
|
|
114
112
|
<div id="footer">
|
|
115
|
-
Generated on Tue Aug 18
|
|
113
|
+
Generated on Tue Aug 18 19:02:36 2026 by
|
|
116
114
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
117
115
|
0.9.45 (ruby-3.4.8).
|
|
118
116
|
</div>
|
data/docs/_index.html
CHANGED
|
@@ -78,34 +78,6 @@
|
|
|
78
78
|
<td valign='top' width="33%">
|
|
79
79
|
|
|
80
80
|
|
|
81
|
-
<ul id="alpha_A" class="alpha">
|
|
82
|
-
<li class="letter">A</li>
|
|
83
|
-
<ul>
|
|
84
|
-
|
|
85
|
-
<li>
|
|
86
|
-
<span class='object_link'><a href="ActiveRecord.html" title="ActiveRecord (module)">ActiveRecord</a></span>
|
|
87
|
-
|
|
88
|
-
</li>
|
|
89
|
-
|
|
90
|
-
</ul>
|
|
91
|
-
</ul>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
<ul id="alpha_B" class="alpha">
|
|
95
|
-
<li class="letter">B</li>
|
|
96
|
-
<ul>
|
|
97
|
-
|
|
98
|
-
<li>
|
|
99
|
-
<span class='object_link'><a href="ActiveRecord/Base.html" title="ActiveRecord::Base (class)">Base</a></span>
|
|
100
|
-
|
|
101
|
-
<small>(ActiveRecord)</small>
|
|
102
|
-
|
|
103
|
-
</li>
|
|
104
|
-
|
|
105
|
-
</ul>
|
|
106
|
-
</ul>
|
|
107
|
-
|
|
108
|
-
|
|
109
81
|
<ul id="alpha_H" class="alpha">
|
|
110
82
|
<li class="letter">H</li>
|
|
111
83
|
<ul>
|
|
@@ -129,7 +101,7 @@
|
|
|
129
101
|
</div>
|
|
130
102
|
|
|
131
103
|
<div id="footer">
|
|
132
|
-
Generated on Tue
|
|
104
|
+
Generated on Tue Sep 29 01:20:55 2026 by
|
|
133
105
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
134
106
|
0.9.45 (ruby-3.4.8).
|
|
135
107
|
</div>
|
data/docs/class_list.html
CHANGED
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
|
|
47
47
|
<ul id="full_list" class="class">
|
|
48
48
|
<li id="object_" class="odd"><div class="item" style="padding-left:30px"><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></div></li>
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
|
|
51
51
|
</ul>
|
|
52
52
|
</div>
|
data/docs/file.README.html
CHANGED
|
@@ -59,7 +59,8 @@
|
|
|
59
59
|
<div class="clear"></div>
|
|
60
60
|
</div>
|
|
61
61
|
|
|
62
|
-
<div id="content"><div id='filecontents'><p><a href="https://
|
|
62
|
+
<div id="content"><div id='filecontents'><p><a href="https://rubygems.org/gems/activerecord-hashlike_access"><img src="https://img.shields.io/github/v/release/nestor-custodio/activerecord-hashlike_access?color=green&label=gem%20version" alt="Gem Version" /></a>
|
|
63
|
+
<a href="https://tldrlegal.com/license/mit-license"><img src="https://img.shields.io/github/license/nestor-custodio/activerecord-hashlike_access" alt="MIT License" /></a></p>
|
|
63
64
|
<h1 id="ActiveRecord__HashlikeAccess">ActiveRecord::HashlikeAccess</h1>
|
|
64
65
|
<p>Pulling this module into your ActiveRecord models gives you Hash-like getters (<code>[]</code>) and setters (<code>[]=</code>) for <em>individual records/values</em>. This is not only slightly cleaner to read/write, but also encourages fetching items by either their <code>id</code> or a canonical "lookup field", minimizing the likelihood of pulling records by an unindexed field.</p>
|
|
65
66
|
<p>In simplest terms:</p>
|
|
@@ -70,7 +71,7 @@
|
|
|
70
71
|
<span class='comment'># Hash-Like Access to Records:
|
|
71
72
|
</span>
|
|
72
73
|
<span class='kw'>class</span> <span class='const'>Role</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
73
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
74
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
74
75
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>by:</span> <span class='symbol'>:name</span>
|
|
75
76
|
<span class='kw'>end</span>
|
|
76
77
|
|
|
@@ -82,7 +83,7 @@
|
|
|
82
83
|
<span class='comment'># Hash-Like Access to Values:
|
|
83
84
|
</span>
|
|
84
85
|
<span class='kw'>class</span> <span class='const'>Role</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
85
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
86
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
86
87
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>to:</span> <span class='symbol'>:display_text</span><span class='comma'>,</span> <span class='label'>by:</span> <span class='symbol'>:name</span>
|
|
87
88
|
<span class='kw'>end</span>
|
|
88
89
|
|
|
@@ -96,6 +97,7 @@
|
|
|
96
97
|
|
|
97
98
|
</code></pre>
|
|
98
99
|
<p>Importantly, note that <strong>this gem works on the model, not its relations</strong>. ActiveRecord's existing bracket constructs for referencing <em>relations</em> (i.e. resultsets) in an <code>Array</code>-like fashion will continue to work as they always have.</p>
|
|
100
|
+
<p><a href="https://nestor-custodio.github.io/activerecord-hashlike_access/ActiveRecord/HashlikeAccess.html">Full documentation is available here</a>, but do read below for a crash course on availble featues!</p>
|
|
99
101
|
<h2 id="Installation">Installation</h2>
|
|
100
102
|
<ul>
|
|
101
103
|
<li>
|
|
@@ -136,7 +138,7 @@
|
|
|
136
138
|
<p>If your model pulls from <code>ActiveRecord::HashlikeAccess</code> but does not explicitly call one of these methods, the default behaviour will grant you hash-like access to records by the model's primary key.</p>
|
|
137
139
|
<h3 id="Hash_Like_Access_to__Records_">Hash-Like Access to <em>Records</em></h3>
|
|
138
140
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
139
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
141
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
140
142
|
|
|
141
143
|
<span class='comment'># For implicit access to records BY the primary key,
|
|
142
144
|
</span> <span class='comment'># you can omit the `hashlike_access` call altogether.
|
|
@@ -165,7 +167,7 @@
|
|
|
165
167
|
</ul>
|
|
166
168
|
<h3 id="Hash_Like_Access_to__Values_">Hash-Like Access to <em>Values</em></h3>
|
|
167
169
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
168
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
170
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
169
171
|
|
|
170
172
|
<span class='comment'># For access TO a specific value for records matched BY the primary key,
|
|
171
173
|
</span> <span class='comment'># `hashlike_access` needs the target name via 'to', but the 'by' is optional:
|
|
@@ -189,7 +191,7 @@
|
|
|
189
191
|
<li>
|
|
190
192
|
<p>You have hash-like access to a field and the model is not read-only:</p>
|
|
191
193
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
192
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
194
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
193
195
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>to:</span> <span class='symbol'>:field_x</span>
|
|
194
196
|
<span class='kw'>end</span>
|
|
195
197
|
|
|
@@ -203,7 +205,7 @@
|
|
|
203
205
|
<li>
|
|
204
206
|
<p>You have hash-like access to a method and there is <em>also</em> a corresponding <code>=</code> method for it:</p>
|
|
205
207
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
206
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
208
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
207
209
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>to:</span> <span class='symbol'>:method_x</span>
|
|
208
210
|
|
|
209
211
|
<span class='kw'>def</span> <span class='id identifier rubyid_method_x'>method_x</span>
|
|
@@ -233,7 +235,7 @@
|
|
|
233
235
|
<h3 id="Using_Non_Scalar_Keys">Using Non-Scalar Keys</h3>
|
|
234
236
|
<p>If your model uses a composite primary key, hash-like access happens by passing all the needed values:</p>
|
|
235
237
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
236
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
238
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
237
239
|
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_primary_key'>primary_key</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='symbol'>:primary_key_1</span><span class='comma'>,</span> <span class='symbol'>:primary_key_2</span><span class='rbracket'>]</span>
|
|
238
240
|
<span class='kw'>end</span>
|
|
239
241
|
|
|
@@ -247,7 +249,7 @@
|
|
|
247
249
|
</code></pre>
|
|
248
250
|
<p>You can also do this if you want to use multiple non-primary-key lookup fields by passing an array to the <code>by</code> option:</p>
|
|
249
251
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
250
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
252
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
251
253
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>by:</span> <span class='lbracket'>[</span><span class='symbol'>:lookup_field_1</span><span class='comma'>,</span> <span class='symbol'>:lookup_field_2</span><span class='rbracket'>]</span>
|
|
252
254
|
<span class='kw'>end</span>
|
|
253
255
|
|
|
@@ -262,7 +264,7 @@
|
|
|
262
264
|
<h2 id="Performance_Considerations">Performance Considerations</h2>
|
|
263
265
|
<p>Every instance of a hash-like lookup (<code>[some_key]</code>) is backed by a <code>find_by</code>/<code>find_by!</code> call, but ActiveRecord's query cache goes <em>a long way</em> to reducing the performance penalty from repeated calls for the same record within a single worker.</p>
|
|
264
266
|
<h2 id="Potential_Gotchas">Potential Gotchas</h2>
|
|
265
|
-
<p>As with anything ActiveRecord-related, there are
|
|
267
|
+
<p>As with anything ActiveRecord-related, there are ways to shoot yourself in the foot if you're not careful. Here are a few items to keep in mind and hopefully prevent this:</p>
|
|
266
268
|
<ul>
|
|
267
269
|
<li>
|
|
268
270
|
<p>The hash-like access construct is essentially syntax sugar around ActiveRecord's <code>find_by</code> mechanism. For many (most?) database engines, selecting (or <em>including</em>, in the case of a multi-field key) an unindexed field for your lookups will likely result in a full-table scan with every hash-like access. Be mindful of what field(s) you're using as your key(s).</p>
|
|
@@ -279,7 +281,7 @@
|
|
|
279
281
|
<p><code>ActiveRecord::HashlikeAccess</code> is available as open source under the terms of the <a href="https://tldrlegal.com/license/mit-license">MIT License</a>.</p></div></div>
|
|
280
282
|
|
|
281
283
|
<div id="footer">
|
|
282
|
-
Generated on Tue
|
|
284
|
+
Generated on Tue Sep 29 01:20:55 2026 by
|
|
283
285
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
284
286
|
0.9.45 (ruby-3.4.8).
|
|
285
287
|
</div>
|
data/docs/index.html
CHANGED
|
@@ -59,7 +59,8 @@
|
|
|
59
59
|
<div class="clear"></div>
|
|
60
60
|
</div>
|
|
61
61
|
|
|
62
|
-
<div id="content"><div id='filecontents'><p><a href="https://
|
|
62
|
+
<div id="content"><div id='filecontents'><p><a href="https://rubygems.org/gems/activerecord-hashlike_access"><img src="https://img.shields.io/github/v/release/nestor-custodio/activerecord-hashlike_access?color=green&label=gem%20version" alt="Gem Version" /></a>
|
|
63
|
+
<a href="https://tldrlegal.com/license/mit-license"><img src="https://img.shields.io/github/license/nestor-custodio/activerecord-hashlike_access" alt="MIT License" /></a></p>
|
|
63
64
|
<h1 id="ActiveRecord__HashlikeAccess">ActiveRecord::HashlikeAccess</h1>
|
|
64
65
|
<p>Pulling this module into your ActiveRecord models gives you Hash-like getters (<code>[]</code>) and setters (<code>[]=</code>) for <em>individual records/values</em>. This is not only slightly cleaner to read/write, but also encourages fetching items by either their <code>id</code> or a canonical "lookup field", minimizing the likelihood of pulling records by an unindexed field.</p>
|
|
65
66
|
<p>In simplest terms:</p>
|
|
@@ -70,7 +71,7 @@
|
|
|
70
71
|
<span class='comment'># Hash-Like Access to Records:
|
|
71
72
|
</span>
|
|
72
73
|
<span class='kw'>class</span> <span class='const'>Role</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
73
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
74
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
74
75
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>by:</span> <span class='symbol'>:name</span>
|
|
75
76
|
<span class='kw'>end</span>
|
|
76
77
|
|
|
@@ -82,7 +83,7 @@
|
|
|
82
83
|
<span class='comment'># Hash-Like Access to Values:
|
|
83
84
|
</span>
|
|
84
85
|
<span class='kw'>class</span> <span class='const'>Role</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
85
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
86
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
86
87
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>to:</span> <span class='symbol'>:display_text</span><span class='comma'>,</span> <span class='label'>by:</span> <span class='symbol'>:name</span>
|
|
87
88
|
<span class='kw'>end</span>
|
|
88
89
|
|
|
@@ -96,6 +97,7 @@
|
|
|
96
97
|
|
|
97
98
|
</code></pre>
|
|
98
99
|
<p>Importantly, note that <strong>this gem works on the model, not its relations</strong>. ActiveRecord's existing bracket constructs for referencing <em>relations</em> (i.e. resultsets) in an <code>Array</code>-like fashion will continue to work as they always have.</p>
|
|
100
|
+
<p><a href="https://nestor-custodio.github.io/activerecord-hashlike_access/ActiveRecord/HashlikeAccess.html">Full documentation is available here</a>, but do read below for a crash course on availble featues!</p>
|
|
99
101
|
<h2 id="Installation">Installation</h2>
|
|
100
102
|
<ul>
|
|
101
103
|
<li>
|
|
@@ -136,7 +138,7 @@
|
|
|
136
138
|
<p>If your model pulls from <code>ActiveRecord::HashlikeAccess</code> but does not explicitly call one of these methods, the default behaviour will grant you hash-like access to records by the model's primary key.</p>
|
|
137
139
|
<h3 id="Hash_Like_Access_to__Records_">Hash-Like Access to <em>Records</em></h3>
|
|
138
140
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
139
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
141
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
140
142
|
|
|
141
143
|
<span class='comment'># For implicit access to records BY the primary key,
|
|
142
144
|
</span> <span class='comment'># you can omit the `hashlike_access` call altogether.
|
|
@@ -165,7 +167,7 @@
|
|
|
165
167
|
</ul>
|
|
166
168
|
<h3 id="Hash_Like_Access_to__Values_">Hash-Like Access to <em>Values</em></h3>
|
|
167
169
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
168
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
170
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
169
171
|
|
|
170
172
|
<span class='comment'># For access TO a specific value for records matched BY the primary key,
|
|
171
173
|
</span> <span class='comment'># `hashlike_access` needs the target name via 'to', but the 'by' is optional:
|
|
@@ -189,7 +191,7 @@
|
|
|
189
191
|
<li>
|
|
190
192
|
<p>You have hash-like access to a field and the model is not read-only:</p>
|
|
191
193
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
192
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
194
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
193
195
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>to:</span> <span class='symbol'>:field_x</span>
|
|
194
196
|
<span class='kw'>end</span>
|
|
195
197
|
|
|
@@ -203,7 +205,7 @@
|
|
|
203
205
|
<li>
|
|
204
206
|
<p>You have hash-like access to a method and there is <em>also</em> a corresponding <code>=</code> method for it:</p>
|
|
205
207
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
206
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
208
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
207
209
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>to:</span> <span class='symbol'>:method_x</span>
|
|
208
210
|
|
|
209
211
|
<span class='kw'>def</span> <span class='id identifier rubyid_method_x'>method_x</span>
|
|
@@ -233,7 +235,7 @@
|
|
|
233
235
|
<h3 id="Using_Non_Scalar_Keys">Using Non-Scalar Keys</h3>
|
|
234
236
|
<p>If your model uses a composite primary key, hash-like access happens by passing all the needed values:</p>
|
|
235
237
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
236
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
238
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
237
239
|
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_primary_key'>primary_key</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='symbol'>:primary_key_1</span><span class='comma'>,</span> <span class='symbol'>:primary_key_2</span><span class='rbracket'>]</span>
|
|
238
240
|
<span class='kw'>end</span>
|
|
239
241
|
|
|
@@ -247,7 +249,7 @@
|
|
|
247
249
|
</code></pre>
|
|
248
250
|
<p>You can also do this if you want to use multiple non-primary-key lookup fields by passing an array to the <code>by</code> option:</p>
|
|
249
251
|
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>YourModel</span> <span class='op'><</span> <span class='const'>ApplicationRecord</span>
|
|
250
|
-
<span class='id identifier rubyid_extend'>extend</span> <span class='const'
|
|
252
|
+
<span class='id identifier rubyid_extend'>extend</span> <span class='const'>ActiveRecord</span><span class='op'>::</span><span class='const'><span class='object_link'><a href="ActiveRecord/HashlikeAccess.html" title="ActiveRecord::HashlikeAccess (module)">HashlikeAccess</a></span></span>
|
|
251
253
|
<span class='id identifier rubyid_hashlike_access'>hashlike_access</span> <span class='label'>by:</span> <span class='lbracket'>[</span><span class='symbol'>:lookup_field_1</span><span class='comma'>,</span> <span class='symbol'>:lookup_field_2</span><span class='rbracket'>]</span>
|
|
252
254
|
<span class='kw'>end</span>
|
|
253
255
|
|
|
@@ -262,7 +264,7 @@
|
|
|
262
264
|
<h2 id="Performance_Considerations">Performance Considerations</h2>
|
|
263
265
|
<p>Every instance of a hash-like lookup (<code>[some_key]</code>) is backed by a <code>find_by</code>/<code>find_by!</code> call, but ActiveRecord's query cache goes <em>a long way</em> to reducing the performance penalty from repeated calls for the same record within a single worker.</p>
|
|
264
266
|
<h2 id="Potential_Gotchas">Potential Gotchas</h2>
|
|
265
|
-
<p>As with anything ActiveRecord-related, there are
|
|
267
|
+
<p>As with anything ActiveRecord-related, there are ways to shoot yourself in the foot if you're not careful. Here are a few items to keep in mind and hopefully prevent this:</p>
|
|
266
268
|
<ul>
|
|
267
269
|
<li>
|
|
268
270
|
<p>The hash-like access construct is essentially syntax sugar around ActiveRecord's <code>find_by</code> mechanism. For many (most?) database engines, selecting (or <em>including</em>, in the case of a multi-field key) an unindexed field for your lookups will likely result in a full-table scan with every hash-like access. Be mindful of what field(s) you're using as your key(s).</p>
|
|
@@ -279,7 +281,7 @@
|
|
|
279
281
|
<p><code>ActiveRecord::HashlikeAccess</code> is available as open source under the terms of the <a href="https://tldrlegal.com/license/mit-license">MIT License</a>.</p></div></div>
|
|
280
282
|
|
|
281
283
|
<div id="footer">
|
|
282
|
-
Generated on Tue
|
|
284
|
+
Generated on Tue Sep 29 01:20:55 2026 by
|
|
283
285
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
284
286
|
0.9.45 (ruby-3.4.8).
|
|
285
287
|
</div>
|
|
@@ -80,16 +80,6 @@
|
|
|
80
80
|
|
|
81
81
|
</div>
|
|
82
82
|
|
|
83
|
-
<h2>Defined Under Namespace</h2>
|
|
84
|
-
<p class="children">
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<strong class="modules">Modules:</strong> <span class='object_link'><a href="ActiveRecord.html" title="ActiveRecord (module)">ActiveRecord</a></span>
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
</p>
|
|
93
83
|
|
|
94
84
|
|
|
95
85
|
|
|
@@ -102,7 +92,7 @@
|
|
|
102
92
|
</div>
|
|
103
93
|
|
|
104
94
|
<div id="footer">
|
|
105
|
-
Generated on Tue
|
|
95
|
+
Generated on Tue Sep 29 01:20:55 2026 by
|
|
106
96
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
107
97
|
0.9.45 (ruby-3.4.8).
|
|
108
98
|
</div>
|
metadata
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activerecord-hashlike_access
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nestor Custodio
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
@@ -59,14 +59,14 @@ files:
|
|
|
59
59
|
- lib/active_record/hashlike_access.rb
|
|
60
60
|
- lib/active_record/hashlike_access/auto_extend.rb
|
|
61
61
|
- lib/active_record/hashlike_access/version.rb
|
|
62
|
-
homepage: https://github.com/nestor-custodio/activerecord-
|
|
62
|
+
homepage: https://github.com/nestor-custodio/activerecord-hashlike_access
|
|
63
63
|
licenses:
|
|
64
64
|
- MIT
|
|
65
65
|
metadata:
|
|
66
66
|
allowed_push_host: https://rubygems.org
|
|
67
|
-
homepage_uri: https://github.com/nestor-custodio/activerecord-hashlike_acces
|
|
68
|
-
changelog_uri: https://github.com/nestor-custodio/activerecord-hashlike_acces/CHANGELOG.md
|
|
69
67
|
rubygems_mfa_required: 'true'
|
|
68
|
+
homepage_uri: https://github.com/nestor-custodio/activerecord-hashlike_access
|
|
69
|
+
changelog_uri: https://github.com/nestor-custodio/activerecord-hashlike_access/blob/main/CHANGELOG.md
|
|
70
70
|
rdoc_options: []
|
|
71
71
|
require_paths:
|
|
72
72
|
- lib
|