rails-pattern_matching 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d0f0b6be09429d51fcbb4683ff41d54f52001860252f97dd9d92713250b5ccd
4
+ data.tar.gz: 53400bf0accac128b063573955666e57dcb03930e280a5a8e7d4729711763a6c
5
+ SHA512:
6
+ metadata.gz: 11654a4b132a304a188c94e6f96903904190c4b15f459ef0c9b276e00e6a57e7fcd59cf1c341526285f5131d2239a07fc6a3337c00cb5c9999f28b70ab4767ba
7
+ data.tar.gz: 7536eb9b739cabf05c38ef85cb975d961d0629bf4b44be17a39221985f972fdf9025b2de10dec0261de370f5be2c8fca39c6d5910bbb244cf33fe27ed7da4e39
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,22 @@
1
+ name: Dependabot auto-merge
2
+ on: pull_request
3
+
4
+ permissions:
5
+ contents: write
6
+ pull-requests: write
7
+
8
+ jobs:
9
+ dependabot:
10
+ runs-on: ubuntu-latest
11
+ if: ${{ github.actor == 'dependabot[bot]' }}
12
+ steps:
13
+ - name: Dependabot metadata
14
+ id: metadata
15
+ uses: dependabot/fetch-metadata@v1.3.3
16
+ with:
17
+ github-token: "${{ secrets.GITHUB_TOKEN }}"
18
+ - name: Enable auto-merge for Dependabot PRs
19
+ run: gh pr merge --auto --merge "$PR_URL"
20
+ env:
21
+ PR_URL: ${{github.event.pull_request.html_url}}
22
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
@@ -0,0 +1,25 @@
1
+ name: Main
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - main
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ ci:
16
+ name: CI
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: actions/checkout@master
20
+ - uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: '3.1'
23
+ bundler-cache: true
24
+ - name: Test
25
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails-pattern_matching (0.1.0)
5
+ activemodel
6
+ activerecord
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (7.0.4)
12
+ activesupport (= 7.0.4)
13
+ activerecord (7.0.4)
14
+ activemodel (= 7.0.4)
15
+ activesupport (= 7.0.4)
16
+ activesupport (7.0.4)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ concurrent-ruby (1.1.10)
22
+ i18n (1.12.0)
23
+ concurrent-ruby (~> 1.0)
24
+ minitest (5.16.3)
25
+ power_assert (2.0.2)
26
+ rake (13.0.6)
27
+ sqlite3 (1.5.4-arm64-darwin)
28
+ sqlite3 (1.5.4-x86_64-linux)
29
+ test-unit (3.5.5)
30
+ power_assert
31
+ tzinfo (2.0.5)
32
+ concurrent-ruby (~> 1.0)
33
+
34
+ PLATFORMS
35
+ arm64-darwin-21
36
+ x86_64-linux
37
+
38
+ DEPENDENCIES
39
+ bundler
40
+ rails-pattern_matching!
41
+ rake
42
+ sqlite3
43
+ test-unit
44
+
45
+ BUNDLED WITH
46
+ 2.3.24
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present Kevin Newton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # rails-pattern_matching
2
+
3
+ This gem provides the pattern matching interface for `ActiveModel::AttributeMethods`, `ActiveRecord::Base`, and `ActiveRecord::Relation`.
4
+
5
+ That means it allows you to write code using the pattern matching against your Rails models like the following example:
6
+
7
+ ```ruby
8
+ # app/models/post.rb
9
+ class Post < ApplicationRecord
10
+ has_many :comments
11
+ end
12
+
13
+ # app/models/comment.rb
14
+ class Comment < ApplicationRecord
15
+ belongs_to :post
16
+ end
17
+
18
+ # app/helpers/posts_helper.rb
19
+ module PostsHelper
20
+ def comment_header_for(post)
21
+ case post
22
+ # Here we're matching against an attribute on the post that is an
23
+ # association. It will go through the association and then match against the
24
+ # records in the relation.
25
+ in { comments: [] }
26
+ "No comments yet"
27
+ # Here we're searching for a comment that has the same user_id as the post.
28
+ # We can do this with the "find" pattern. This syntax is all baked into
29
+ # Ruby, so we don't have to do anything other than define the requisite
30
+ # deconstruct methods that are used by the pattern matching.
31
+ in { comments: [*, { user_id: ^(post.user_id ) }, *] }
32
+ "Host replied"
33
+ # Here we're extracting the first comment out of the comments association.
34
+ in { comments: [comment] }
35
+ "One comment"
36
+ # Here we provide a default in case none of the above match. Since we have
37
+ # already matched against an empty array of comments and a single element
38
+ # array, we know that there are at least two comments. We can get the length
39
+ # of the comments association by capturing the comments association in the
40
+ # pattern itself and then using it.
41
+ in { comments: }
42
+ "#{comments.length} comments"
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ ## Installation
49
+
50
+ Add this line to your application's Gemfile:
51
+
52
+ ```ruby
53
+ gem "rails-pattern_matching"
54
+ ```
55
+
56
+ And then execute:
57
+
58
+ $ bundle
59
+
60
+ Or install it yourself as:
61
+
62
+ $ gem install rails-pattern_matching
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kddnewton/rails-pattern_matching.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList["test/*_test.rb"]
9
+ t.verbose = true
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/pattern_matching"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module PatternMatching
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model"
4
+ require "active_record"
5
+ require "rails/pattern_matching/version"
6
+
7
+ module Rails
8
+ module PatternMatching
9
+ class Error < StandardError
10
+ def initialize(context)
11
+ super(<<~MSG)
12
+ Pattern matching appears to already be defined in #{context}. In this
13
+ case the rails-pattern_matching gem should not be used because it
14
+ would override the pattern matching behavior given by #{context}.
15
+ MSG
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class ActiveRecord::Base
22
+ if method_defined?(:deconstruct_keys)
23
+ raise Rails::PatternMatching::Error, "ActiveRecord::Base"
24
+ end
25
+
26
+ # Returns a hash of attributes for the given keys. Provides the pattern
27
+ # matching interface for matching against hash patterns. For example:
28
+ #
29
+ # class Person < ActiveRecord::Base
30
+ # end
31
+ #
32
+ # def greeting_for(person)
33
+ # case person
34
+ # in { name: "Mary" }
35
+ # "Welcome back, Mary!"
36
+ # in { name: }
37
+ # "Welcome, stranger!"
38
+ # end
39
+ # end
40
+ #
41
+ # person = Person.new
42
+ # person.name = "Mary"
43
+ # greeting_for(person) # => "Welcome back, Mary!"
44
+ #
45
+ # person = Person.new
46
+ # person.name = "Bob"
47
+ # greeting_for(person) # => "Welcome, stranger!"
48
+ #
49
+ def deconstruct_keys(keys)
50
+ keys.each_with_object({}) do |key, deconstructed|
51
+ method = key.to_s
52
+
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)
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ class ActiveRecord::Relation
69
+ if method_defined?(:deconstruct)
70
+ raise Rails::PatternMatching::Error, "ActiveRecord::Relation"
71
+ end
72
+
73
+ # Provides the pattern matching interface for matching against array
74
+ # patterns. For example:
75
+ #
76
+ # class Person < ActiveRecord::Base
77
+ # end
78
+ #
79
+ # case Person.all
80
+ # in []
81
+ # "No one is here"
82
+ # in [{ name: "Mary" }]
83
+ # "Only Mary is here"
84
+ # in [_]
85
+ # "Only one person is here"
86
+ # in [_, _, *]
87
+ # "More than one person is here"
88
+ # end
89
+ #
90
+ # Be wary when using this method with a large number of records, as it
91
+ # will load everything into memory.
92
+ #
93
+ def deconstruct
94
+ records
95
+ end
96
+ end
97
+
98
+ module ActiveModel::AttributeMethods
99
+ if method_defined?(:deconstruct_keys)
100
+ raise Rails::PatternMatching::Error, "ActiveModel::AttributeMethods"
101
+ end
102
+
103
+ # Returns a hash of attributes for the given keys. Provides the pattern
104
+ # matching interface for matching against hash patterns. For example:
105
+ #
106
+ # class Person
107
+ # include ActiveModel::AttributeMethods
108
+ #
109
+ # attr_accessor :name
110
+ # define_attribute_method :name
111
+ # end
112
+ #
113
+ # def greeting_for(person)
114
+ # case person
115
+ # in { name: "Mary" }
116
+ # "Welcome back, Mary!"
117
+ # in { name: }
118
+ # "Welcome, stranger!"
119
+ # end
120
+ # end
121
+ #
122
+ # person = Person.new
123
+ # person.name = "Mary"
124
+ # greeting_for(person) # => "Welcome back, Mary!"
125
+ #
126
+ # person = Person.new
127
+ # person.name = "Bob"
128
+ # greeting_for(person) # => "Welcome, stranger!"
129
+ #
130
+ def deconstruct_keys(keys)
131
+ keys.each_with_object({}) do |key, deconstructed|
132
+ string_key = key.to_s
133
+
134
+ if attribute_method?(string_key)
135
+ deconstructed[key] = public_send(string_key)
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "rails/pattern_matching/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "rails-pattern_matching"
9
+ spec.version = Rails::PatternMatching::VERSION
10
+ spec.authors = ["Kevin Newton"]
11
+ spec.email = ["kddnewton@gmail.com"]
12
+
13
+ spec.summary = "Pattern matching for Rails applications"
14
+ spec.homepage = "https://github.com/kddnewton/rails-pattern_matching"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activemodel"
25
+ spec.add_dependency "activerecord"
26
+
27
+ spec.add_development_dependency "bundler"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "sqlite3"
30
+ spec.add_development_dependency "test-unit"
31
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-pattern_matching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Newton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - kddnewton@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/dependabot.yml"
105
+ - ".github/workflows/auto-merge.yml"
106
+ - ".github/workflows/main.yml"
107
+ - ".gitignore"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - lib/rails/pattern_matching.rb
115
+ - lib/rails/pattern_matching/version.rb
116
+ - rails-pattern_matching.gemspec
117
+ homepage: https://github.com/kddnewton/rails-pattern_matching
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 3.3.21
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Pattern matching for Rails applications
140
+ test_files: []