pattern_matchable 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/.github/workflows/main.yml +28 -0
- data/README.md +107 -47
- data/lib/pattern_matchable/version.rb +1 -1
- data/lib/pattern_matchable.rb +31 -9
- data/pattern_matchable.gemspec +2 -2
- metadata +6 -5
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,62 +23,31 @@ 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
|
36
|
-
|
37
|
-
|
38
|
-
class Time
|
39
|
-
include PatternMatchable
|
40
|
-
|
41
|
-
def to_four_seasons
|
42
|
-
case self
|
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
|
54
|
-
|
55
|
-
p Time.local(2019, 1, 1).to_four_seasons # => "winter"
|
56
|
-
p Time.local(2019, 3, 1).to_four_seasons # => "spring"
|
57
|
-
p Time.local(2019, 5, 1).to_four_seasons # => "spring"
|
58
|
-
p Time.local(2019, 8, 1).to_four_seasons # => "summer"
|
59
|
-
p Time.local(2019, 10, 1).to_four_seasons # => "autumn"
|
60
|
-
p Time.local(2019, 12, 1).to_four_seasons # => "winter"
|
61
33
|
```
|
62
34
|
|
63
|
-
### 1. `
|
35
|
+
### 1. `using PatternMatchable {class names}`
|
64
36
|
|
65
|
-
|
37
|
+
Use Refinements to add `{class names}#deconstruct_keys`.
|
66
38
|
|
67
39
|
```ruby
|
68
40
|
require "pattern_matchable"
|
69
41
|
|
70
|
-
#
|
71
|
-
|
72
|
-
class Array
|
73
|
-
include PatternMatchable
|
74
|
-
end
|
42
|
+
# define Array#deconstruct_keys
|
43
|
+
using PatternMatchable Array
|
75
44
|
|
76
|
-
[1, 2, 3, 4, 5]
|
45
|
+
[1, 2, 3, 4, 5] => { first:, last: }
|
77
46
|
p first # => 1
|
78
47
|
p last # => 5
|
79
48
|
|
80
|
-
# error: NoMatchingPatternError
|
81
49
|
"Homu" in { downcase:, upcase: }
|
50
|
+
# => false
|
82
51
|
```
|
83
52
|
|
84
53
|
### 2. `using PatternMatchable`
|
@@ -91,16 +60,46 @@ require "pattern_matchable"
|
|
91
60
|
# defined Object#deconstruct_keys
|
92
61
|
using PatternMatchable
|
93
62
|
|
94
|
-
[1, 2, 3, 4, 5]
|
63
|
+
case [1, 2, 3, 4, 5]
|
64
|
+
in { first:, last: }
|
65
|
+
end
|
95
66
|
p first # => 1
|
96
67
|
p last # => 5
|
97
68
|
|
98
|
-
"Homu"
|
69
|
+
case "Homu"
|
70
|
+
in { downcase:, upcase: }
|
71
|
+
end
|
99
72
|
p downcase # => "homu"
|
100
73
|
p upcase # => "HOMU"
|
101
74
|
```
|
102
75
|
|
103
|
-
### 3. `
|
76
|
+
### 3. `include PatternMatchable`
|
77
|
+
|
78
|
+
`include` and add `#deconstruct_keys` to any class / module.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require "pattern_matchable"
|
82
|
+
|
83
|
+
# mixin PatternMatchable to Array
|
84
|
+
# defined Array#deconstruct_keys
|
85
|
+
class Array
|
86
|
+
include PatternMatchable
|
87
|
+
end
|
88
|
+
|
89
|
+
# OK: assigned `first` `last` variables
|
90
|
+
case [1, 2, 3, 4, 5]
|
91
|
+
in { first:, last: }
|
92
|
+
end
|
93
|
+
p first # => 1
|
94
|
+
p last # => 5
|
95
|
+
|
96
|
+
# error: NoMatchingPatternError
|
97
|
+
case "Homu"
|
98
|
+
in { downcase:, upcase: }
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
### 4. `using PatternMatchable.refining klass`
|
104
103
|
|
105
104
|
Add `#deconstruct_keys` to any class / module using Refinements.
|
106
105
|
|
@@ -110,15 +109,17 @@ require "pattern_matchable"
|
|
110
109
|
# define Array#deconstruct_keys
|
111
110
|
using PatternMatchable.refining Array
|
112
111
|
|
113
|
-
[1, 2, 3, 4, 5]
|
112
|
+
[1, 2, 3, 4, 5] => { first:, last: }
|
114
113
|
p first # => 1
|
115
114
|
p last # => 5
|
116
115
|
|
117
116
|
# error: NoMatchingPatternError
|
118
|
-
"Homu"
|
117
|
+
case "Homu"
|
118
|
+
in { downcase:, upcase: }
|
119
|
+
end
|
119
120
|
```
|
120
121
|
|
121
|
-
###
|
122
|
+
### 5. `using PatternMatchable::#{class name}`
|
122
123
|
|
123
124
|
Use `Refinements` using `const_missing`.
|
124
125
|
|
@@ -128,7 +129,9 @@ require "pattern_matchable"
|
|
128
129
|
# define Array#deconstruct_keys
|
129
130
|
using PatternMatchable::Array
|
130
131
|
|
131
|
-
[1, 2, 3, 4, 5]
|
132
|
+
case [1, 2, 3, 4, 5]
|
133
|
+
in { first:, last: }
|
134
|
+
end
|
132
135
|
p first # => 1
|
133
136
|
p last # => 5
|
134
137
|
|
@@ -136,14 +139,71 @@ p last # => 5
|
|
136
139
|
# defined Enumerator::Lazy
|
137
140
|
using PatternMatchable::Enumerator::Lazy
|
138
141
|
|
139
|
-
(1..10).lazy.map { _1 * 2 }
|
142
|
+
(1..10).lazy.map { _1 * 2 } => { first:, count: }
|
140
143
|
p first # => 2
|
141
144
|
p count # => 10
|
142
145
|
|
143
|
-
# error: NoMatchingPatternError
|
144
146
|
"Homu" in { downcase:, upcase: }
|
147
|
+
# => false
|
148
|
+
```
|
149
|
+
|
150
|
+
## NOTE: Classes with `#deconstruct_keys` or `#respond_to?` defined will not work with `using PatternMatchable`.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
require "pattern_matchable"
|
154
|
+
|
155
|
+
class X
|
156
|
+
def respond_to?(...)
|
157
|
+
super
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
using PatternMatchable
|
162
|
+
|
163
|
+
# NoMatchingPatternKeyError
|
164
|
+
case { name: "homu", age: 14 }
|
165
|
+
in { first:, count: }
|
166
|
+
else
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
# NoMatchingPatternKeyError
|
171
|
+
case X.new
|
172
|
+
in { __id__: }
|
173
|
+
else
|
174
|
+
end
|
145
175
|
```
|
146
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
|
+
```
|
147
207
|
|
148
208
|
## Development
|
149
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
|
|
@@ -25,3 +43,7 @@ module PatternMatchable
|
|
25
43
|
self.refining(Object.const_get(klass_name))
|
26
44
|
end
|
27
45
|
end
|
46
|
+
|
47
|
+
def PatternMatchable(*klasses)
|
48
|
+
PatternMatchable.refining *klasses
|
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
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,7 +29,7 @@ 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
35
|
post_install_message:
|
@@ -39,14 +40,14 @@ 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
|
+
rubygems_version: 3.4.6
|
50
51
|
signing_key:
|
51
52
|
specification_version: 4
|
52
53
|
summary: call method with pattern match.
|