rails-pattern_matching 0.1.0 → 0.2.0
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/Gemfile.lock +2 -2
- data/lib/rails/pattern_matching/version.rb +1 -1
- data/lib/rails/pattern_matching.rb +41 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e22e2e09c3fcaad2a569d0020da8e60991b836923693e2d49bea810cc8c18cf2
|
4
|
+
data.tar.gz: 6f53bdd3290b045c87db287190b6ba74abb7a083d7fd6578b02fb6358c68c956
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1d8c33df5c0914f84ed9fd73a96265f30d830f62da0b03bca860d8c7ce710ef1446bc54178b55164d9e46750debb3f3750c654d15a54cf60d3458d3c367a59d
|
7
|
+
data.tar.gz: 1d440c27f72b3a4f3dcd9d262938553953162585a3235d68d75a424464b9517881348b9446a2dd6b98918c1959f99a52b2f4b7e6208d68fa0c81b20826f7bdb1
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rails-pattern_matching (0.
|
4
|
+
rails-pattern_matching (0.2.0)
|
5
5
|
activemodel
|
6
6
|
activerecord
|
7
7
|
|
@@ -26,7 +26,7 @@ GEM
|
|
26
26
|
rake (13.0.6)
|
27
27
|
sqlite3 (1.5.4-arm64-darwin)
|
28
28
|
sqlite3 (1.5.4-x86_64-linux)
|
29
|
-
test-unit (3.5.
|
29
|
+
test-unit (3.5.7)
|
30
30
|
power_assert
|
31
31
|
tzinfo (2.0.5)
|
32
32
|
concurrent-ruby (~> 1.0)
|
@@ -47,20 +47,35 @@ class ActiveRecord::Base
|
|
47
47
|
# greeting_for(person) # => "Welcome, stranger!"
|
48
48
|
#
|
49
49
|
def deconstruct_keys(keys)
|
50
|
-
keys
|
51
|
-
|
50
|
+
if keys
|
51
|
+
# If we've been given keys, then we're going to filter down to just the
|
52
|
+
# values that are being requested.
|
53
|
+
keys.each_with_object({}) do |key, deconstructed|
|
54
|
+
method = key.to_s
|
52
55
|
|
53
|
-
|
54
|
-
#
|
55
|
-
#
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
56
|
+
# We don't need to worry about the case where the user provided a key
|
57
|
+
# that doesn't match an attribute or association, because the match
|
58
|
+
# will fail by virtue of there not being a key in the result hash.
|
59
|
+
if attribute_method?(method)
|
60
|
+
# Here we're pattern matching against an attribute method. We're
|
61
|
+
# going to use the [] method so that we either get the value or
|
62
|
+
# raise an error for a missing attribute in case it wasn't loaded.
|
63
|
+
deconstructed[key] = public_send(method)
|
64
|
+
elsif self.class.reflect_on_association(method)
|
65
|
+
# Here we're going to pattern match against an association. We're
|
66
|
+
# going to use the main interface for that association which can
|
67
|
+
# be further pattern matched later.
|
68
|
+
deconstructed[key] = public_send(method)
|
69
|
+
end
|
63
70
|
end
|
71
|
+
else
|
72
|
+
# If we haven't been given keys, then the user wants to grab up all of the
|
73
|
+
# attributes and associations for this record.
|
74
|
+
attributes.transform_keys(&:to_sym).merge!(
|
75
|
+
self.class.reflect_on_all_associations.to_h do |reflection|
|
76
|
+
[reflection.name, public_send(reflection.name)]
|
77
|
+
end
|
78
|
+
)
|
64
79
|
end
|
65
80
|
end
|
66
81
|
end
|
@@ -128,12 +143,22 @@ module ActiveModel::AttributeMethods
|
|
128
143
|
# greeting_for(person) # => "Welcome, stranger!"
|
129
144
|
#
|
130
145
|
def deconstruct_keys(keys)
|
131
|
-
keys
|
132
|
-
|
146
|
+
if keys
|
147
|
+
# If we've been given keys, then we're going to filter down to just the
|
148
|
+
# attributes that were given for this object.
|
149
|
+
keys.each_with_object({}) do |key, deconstructed|
|
150
|
+
string_key = key.to_s
|
133
151
|
|
134
|
-
|
135
|
-
|
152
|
+
# If the user provided a key that doesn't match an attribute, then we
|
153
|
+
# do not add it to the result hash, and the match will fail.
|
154
|
+
if attribute_method?(string_key)
|
155
|
+
deconstructed[key] = public_send(string_key)
|
156
|
+
end
|
136
157
|
end
|
158
|
+
else
|
159
|
+
# If we haven't been given keys, then the user wants to grab up all of the
|
160
|
+
# attributes for this object.
|
161
|
+
attributes.transform_keys(&:to_sym)
|
137
162
|
end
|
138
163
|
end
|
139
164
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-pattern_matching
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Newton
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|