pattern_matchable 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +28 -0
- data/README.md +83 -62
- data/lib/pattern_matchable/version.rb +1 -1
- data/lib/pattern_matchable.rb +29 -11
- data/pattern_matchable.gemspec +2 -2
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e959ed23b722f0cf3e64745093f1cd6952d60265154eebb65357fca9742b8401
|
4
|
+
data.tar.gz: 48e3ec0052c4ec762fa0850f100195ed25146183dc4389813d963e30484d6824
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 818c7f81876bd43f1e7e7f2a6f962cb48397a7b27d722ba7281c923a0ddbe7144aaefee4ee96b8a6909be0adbd49e1a2ad24f76965ee7ccf4bb4f8bc70d384a0
|
7
|
+
data.tar.gz: a61c65e11de5e9c72482b98f60d82b5cc8e76710c1b5c0f15607cb9ee93983d9c30f552656851d40c2f6a9c02ea6996def8536534286b04b0fb83ea1a40ac03e
|
@@ -0,0 +1,28 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
|
8
|
+
pull_request:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
pattern_matchable:
|
12
|
+
name: >-
|
13
|
+
pattern_matchable ${{ matrix.os }} ${{ matrix.ruby }}
|
14
|
+
runs-on: ${{ matrix.os }}
|
15
|
+
strategy:
|
16
|
+
matrix:
|
17
|
+
os: [ ubuntu-latest, macos-latest, windows-latest ]
|
18
|
+
ruby: [ 'head', '3.2', '3.1', '3.0' ]
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v3
|
22
|
+
- name: Set up Ruby
|
23
|
+
uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
26
|
+
bundler-cache: true
|
27
|
+
- name: Run the default task
|
28
|
+
run: bundle exec rake
|
data/README.md
CHANGED
@@ -23,93 +23,83 @@ Or install it yourself as:
|
|
23
23
|
```ruby
|
24
24
|
require "pattern_matchable"
|
25
25
|
|
26
|
-
|
27
|
-
# defined #deconstruct_keys
|
28
|
-
include PatternMatchable
|
29
|
-
end
|
26
|
+
using PatternMatchable Array
|
30
27
|
|
31
28
|
case [1, 2, 3]
|
32
29
|
in first:, last:
|
33
30
|
pp "OK : #{first}, #{last}"
|
34
31
|
# => "OK : 1, 3"
|
35
32
|
end
|
33
|
+
```
|
36
34
|
|
35
|
+
### 1. `using PatternMatchable {class names}`
|
37
36
|
|
38
|
-
class
|
39
|
-
include PatternMatchable
|
37
|
+
Use Refinements to add `{class names}#deconstruct_keys`.
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
in month: (3..5)
|
44
|
-
"spring"
|
45
|
-
in month: (6..8)
|
46
|
-
"summer"
|
47
|
-
in month: (9..11)
|
48
|
-
"autumn"
|
49
|
-
in { month: (1..2) } | { month: 12 }
|
50
|
-
"winter"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
39
|
+
```ruby
|
40
|
+
require "pattern_matchable"
|
54
41
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
p
|
60
|
-
p
|
42
|
+
# define Array#deconstruct_keys
|
43
|
+
using PatternMatchable Array
|
44
|
+
|
45
|
+
[1, 2, 3, 4, 5] => { first:, last: }
|
46
|
+
p first # => 1
|
47
|
+
p last # => 5
|
48
|
+
|
49
|
+
"Homu" in { downcase:, upcase: }
|
50
|
+
# => false
|
61
51
|
```
|
62
52
|
|
63
|
-
###
|
53
|
+
### 2. `using PatternMatchable`
|
64
54
|
|
65
|
-
|
55
|
+
Use Refinements to add `Object#deconstruct_keys`.
|
66
56
|
|
67
57
|
```ruby
|
68
58
|
require "pattern_matchable"
|
69
59
|
|
70
|
-
#
|
71
|
-
|
72
|
-
class Array
|
73
|
-
include PatternMatchable
|
74
|
-
end
|
60
|
+
# defined Object#deconstruct_keys
|
61
|
+
using PatternMatchable
|
75
62
|
|
76
|
-
# OK: assigned `first` `last` variables
|
77
63
|
case [1, 2, 3, 4, 5]
|
78
64
|
in { first:, last: }
|
79
65
|
end
|
80
66
|
p first # => 1
|
81
67
|
p last # => 5
|
82
68
|
|
83
|
-
# error: NoMatchingPatternError
|
84
69
|
case "Homu"
|
85
70
|
in { downcase:, upcase: }
|
86
71
|
end
|
72
|
+
p downcase # => "homu"
|
73
|
+
p upcase # => "HOMU"
|
87
74
|
```
|
88
75
|
|
89
|
-
###
|
76
|
+
### 3. `include PatternMatchable`
|
90
77
|
|
91
|
-
|
78
|
+
`include` and add `#deconstruct_keys` to any class / module.
|
92
79
|
|
93
80
|
```ruby
|
94
81
|
require "pattern_matchable"
|
95
82
|
|
96
|
-
#
|
97
|
-
|
83
|
+
# mixin PatternMatchable to Array
|
84
|
+
# defined Array#deconstruct_keys
|
85
|
+
class Array
|
86
|
+
include PatternMatchable
|
87
|
+
end
|
98
88
|
|
89
|
+
# OK: assigned `first` `last` variables
|
99
90
|
case [1, 2, 3, 4, 5]
|
100
91
|
in { first:, last: }
|
101
92
|
end
|
102
93
|
p first # => 1
|
103
94
|
p last # => 5
|
104
95
|
|
96
|
+
# error: NoMatchingPatternError
|
105
97
|
case "Homu"
|
106
98
|
in { downcase:, upcase: }
|
107
99
|
end
|
108
|
-
p downcase # => "homu"
|
109
|
-
p upcase # => "HOMU"
|
110
100
|
```
|
111
101
|
|
112
|
-
###
|
102
|
+
### 4. `using PatternMatchable.refining klass`
|
113
103
|
|
114
104
|
Add `#deconstruct_keys` to any class / module using Refinements.
|
115
105
|
|
@@ -119,9 +109,7 @@ require "pattern_matchable"
|
|
119
109
|
# define Array#deconstruct_keys
|
120
110
|
using PatternMatchable.refining Array
|
121
111
|
|
122
|
-
|
123
|
-
in { first:, last: }
|
124
|
-
end
|
112
|
+
[1, 2, 3, 4, 5] => { first:, last: }
|
125
113
|
p first # => 1
|
126
114
|
p last # => 5
|
127
115
|
|
@@ -131,7 +119,7 @@ in { downcase:, upcase: }
|
|
131
119
|
end
|
132
120
|
```
|
133
121
|
|
134
|
-
###
|
122
|
+
### 5. `using PatternMatchable::#{class name}`
|
135
123
|
|
136
124
|
Use `Refinements` using `const_missing`.
|
137
125
|
|
@@ -151,38 +139,71 @@ p last # => 5
|
|
151
139
|
# defined Enumerator::Lazy
|
152
140
|
using PatternMatchable::Enumerator::Lazy
|
153
141
|
|
154
|
-
|
155
|
-
in { first:, count: }
|
156
|
-
end
|
142
|
+
(1..10).lazy.map { _1 * 2 } => { first:, count: }
|
157
143
|
p first # => 2
|
158
144
|
p count # => 10
|
159
145
|
|
160
|
-
|
161
|
-
|
162
|
-
in { downcase:, upcase: }
|
163
|
-
end
|
146
|
+
"Homu" in { downcase:, upcase: }
|
147
|
+
# => false
|
164
148
|
```
|
165
149
|
|
166
|
-
|
150
|
+
## NOTE: Classes with `#deconstruct_keys` or `#respond_to?` defined will not work with `using PatternMatchable`.
|
167
151
|
|
168
152
|
```ruby
|
169
153
|
require "pattern_matchable"
|
170
154
|
|
171
|
-
|
172
|
-
|
155
|
+
class X
|
156
|
+
def respond_to?(...)
|
157
|
+
super
|
158
|
+
end
|
159
|
+
end
|
173
160
|
|
174
|
-
|
175
|
-
|
161
|
+
using PatternMatchable
|
162
|
+
|
163
|
+
# NoMatchingPatternKeyError
|
164
|
+
case { name: "homu", age: 14 }
|
165
|
+
in { first:, count: }
|
166
|
+
else
|
176
167
|
end
|
177
|
-
p first # => 1
|
178
|
-
p last # => 5
|
179
168
|
|
180
|
-
|
181
|
-
|
182
|
-
|
169
|
+
|
170
|
+
# NoMatchingPatternKeyError
|
171
|
+
case X.new
|
172
|
+
in { __id__: }
|
173
|
+
else
|
183
174
|
end
|
184
175
|
```
|
185
176
|
|
177
|
+
For example, `ActiveRecord::Base`.
|
178
|
+
In this case, you must use `using PatternMatchable X`.
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
require "pattern_matchable"
|
182
|
+
|
183
|
+
class X
|
184
|
+
def respond_to?(...)
|
185
|
+
super
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
using PatternMatchable Hash
|
190
|
+
using PatternMatchable X
|
191
|
+
|
192
|
+
case { name: "homu", age: 14 }
|
193
|
+
in { first:, count: }
|
194
|
+
pp "ok: #{first}: #{count}"
|
195
|
+
# => "ok: [:name, \"homu\"]: 2"
|
196
|
+
else
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
case X.new
|
201
|
+
in { __id__: }
|
202
|
+
pp "ok: #{__id__}"
|
203
|
+
# => "ok: 880"
|
204
|
+
else
|
205
|
+
end
|
206
|
+
```
|
186
207
|
|
187
208
|
## Development
|
188
209
|
|
data/lib/pattern_matchable.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative "pattern_matchable/version"
|
2
2
|
|
3
3
|
module PatternMatchable
|
4
4
|
def deconstruct_keys(keys)
|
@@ -6,18 +6,36 @@ module PatternMatchable
|
|
6
6
|
end
|
7
7
|
|
8
8
|
refine Object do
|
9
|
-
|
9
|
+
if defined?(import_methods)
|
10
|
+
import_methods PatternMatchable
|
11
|
+
else
|
12
|
+
include PatternMatchable
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
|
-
def self.refining(
|
16
|
+
def self.refining(*klasses)
|
13
17
|
Module.new {
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
klasses.each do |klass|
|
19
|
+
refine klass do
|
20
|
+
def deconstruct_keys(keys)
|
21
|
+
if defined? super
|
22
|
+
super.then { |result|
|
23
|
+
result.merge((keys - result.keys).to_h { [_1, public_send(_1)] })
|
24
|
+
}
|
25
|
+
else
|
26
|
+
keys.to_h { [_1, public_send(_1)] }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def respond_to?(name, ...)
|
31
|
+
name == :deconstruct_keys || super
|
32
|
+
end
|
33
|
+
end
|
17
34
|
|
18
|
-
|
19
|
-
|
20
|
-
|
35
|
+
define_singleton_method(:const_missing) { |nested_name|
|
36
|
+
PatternMatchable.refining Object.const_get("#{klass.name}::#{nested_name}")
|
37
|
+
}
|
38
|
+
end
|
21
39
|
}
|
22
40
|
end
|
23
41
|
|
@@ -26,6 +44,6 @@ module PatternMatchable
|
|
26
44
|
end
|
27
45
|
end
|
28
46
|
|
29
|
-
def PatternMatchable(
|
30
|
-
PatternMatchable.refining
|
47
|
+
def PatternMatchable(*klasses)
|
48
|
+
PatternMatchable.refining *klasses
|
31
49
|
end
|
data/pattern_matchable.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
|
9
9
|
spec.summary = %q{call method with pattern match.}
|
10
10
|
spec.description = %q{call method with pattern match.}
|
11
|
-
spec.homepage = ""
|
12
|
-
spec.required_ruby_version = Gem::Requirement.new(">=
|
11
|
+
spec.homepage = "https://github.com/osyo-manga/gem-pattern_matchable"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
|
13
13
|
|
14
14
|
# Specify which files should be added to the gem when it is released.
|
15
15
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pattern_matchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- manga_osyo
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: call method with pattern match.
|
14
14
|
email:
|
@@ -17,6 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/workflows/main.yml"
|
20
21
|
- ".gitignore"
|
21
22
|
- ".rspec"
|
22
23
|
- ".travis.yml"
|
@@ -28,10 +29,10 @@ files:
|
|
28
29
|
- lib/pattern_matchable.rb
|
29
30
|
- lib/pattern_matchable/version.rb
|
30
31
|
- pattern_matchable.gemspec
|
31
|
-
homepage:
|
32
|
+
homepage: https://github.com/osyo-manga/gem-pattern_matchable
|
32
33
|
licenses: []
|
33
34
|
metadata: {}
|
34
|
-
post_install_message:
|
35
|
+
post_install_message:
|
35
36
|
rdoc_options: []
|
36
37
|
require_paths:
|
37
38
|
- lib
|
@@ -39,15 +40,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
40
|
requirements:
|
40
41
|
- - ">="
|
41
42
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
43
|
+
version: 3.0.0
|
43
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
45
|
requirements:
|
45
46
|
- - ">="
|
46
47
|
- !ruby/object:Gem::Version
|
47
48
|
version: '0'
|
48
49
|
requirements: []
|
49
|
-
rubygems_version: 3.
|
50
|
-
signing_key:
|
50
|
+
rubygems_version: 3.4.6
|
51
|
+
signing_key:
|
51
52
|
specification_version: 4
|
52
53
|
summary: call method with pattern match.
|
53
54
|
test_files: []
|