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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d0f0b6be09429d51fcbb4683ff41d54f52001860252f97dd9d92713250b5ccd
4
- data.tar.gz: 53400bf0accac128b063573955666e57dcb03930e280a5a8e7d4729711763a6c
3
+ metadata.gz: e22e2e09c3fcaad2a569d0020da8e60991b836923693e2d49bea810cc8c18cf2
4
+ data.tar.gz: 6f53bdd3290b045c87db287190b6ba74abb7a083d7fd6578b02fb6358c68c956
5
5
  SHA512:
6
- metadata.gz: 11654a4b132a304a188c94e6f96903904190c4b15f459ef0c9b276e00e6a57e7fcd59cf1c341526285f5131d2239a07fc6a3337c00cb5c9999f28b70ab4767ba
7
- data.tar.gz: 7536eb9b739cabf05c38ef85cb975d961d0629bf4b44be17a39221985f972fdf9025b2de10dec0261de370f5be2c8fca39c6d5910bbb244cf33fe27ed7da4e39
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.1.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.5)
29
+ test-unit (3.5.7)
30
30
  power_assert
31
31
  tzinfo (2.0.5)
32
32
  concurrent-ruby (~> 1.0)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rails
4
4
  module PatternMatching
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -47,20 +47,35 @@ class ActiveRecord::Base
47
47
  # greeting_for(person) # => "Welcome, stranger!"
48
48
  #
49
49
  def deconstruct_keys(keys)
50
- keys.each_with_object({}) do |key, deconstructed|
51
- method = key.to_s
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
- if attribute_method?(method)
54
- # Here we're pattern matching against an attribute method. We're
55
- # going to use the [] method so that we either get the value or
56
- # raise an error for a missing attribute in case it wasn't loaded.
57
- deconstructed[key] = public_send(method)
58
- elsif self.class.reflect_on_association(method)
59
- # Here we're going to pattern match against an association. We're
60
- # going to use the main interface for that association which can
61
- # be further pattern matched later.
62
- deconstructed[key] = public_send(method)
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.each_with_object({}) do |key, deconstructed|
132
- string_key = key.to_s
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
- if attribute_method?(string_key)
135
- deconstructed[key] = public_send(string_key)
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.1.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-07 00:00:00.000000000 Z
11
+ date: 2022-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel