eqq 0.0.5 → 0.1.1
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/README.md +32 -8
- data/lib/eqq/buildable.rb +22 -13
- data/lib/eqq/version.rb +1 -1
- data/lib/eqq.rb +15 -6
- data/sig/eqq.rbs +15 -5
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbc7af119664c115468e578edb51c0702530299538f1b067520295b41a150f7d
|
4
|
+
data.tar.gz: e47dbde377b0ca22a2e92a620c3cb5f87b4b09a061833416ebb4ffc88c311521
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c43cd582ac38023b1bc1521abc9dd1f15ec56e7bd2608553e1c7cddccecbf1765f331863c1e1ffabff4a9016698776aec99da16d6bc11ac63758e19af273e9da
|
7
|
+
data.tar.gz: b1f5c8d9868a02bc159b6a7eb18f3f8d7c1894af2f9b0f093867f30e73083add427ab4d79bb707381fe883317f8abb3c9cd8dcef79a36cf2a077ace202a4f295
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Require Ruby 2.6 or later
|
|
12
12
|
Add below code into your Gemfile
|
13
13
|
|
14
14
|
```ruby
|
15
|
-
gem 'eqq', '
|
15
|
+
gem 'eqq', '~> 0.1.1'
|
16
16
|
```
|
17
17
|
|
18
18
|
### Overview
|
@@ -24,7 +24,7 @@ require 'eqq'
|
|
24
24
|
[42, nil, true, false, '', 0].grep(Eqq.BOOLEAN) #=> [true, false]
|
25
25
|
[42, [], {}, 'string', Object.new, nil].grep(Eqq.CAN(:to_h)) #=> [[], {}, nil]
|
26
26
|
|
27
|
-
pattern = Eqq.
|
27
|
+
pattern = Eqq.build do
|
28
28
|
OR(AND(Float, 20..50), Integer)
|
29
29
|
end
|
30
30
|
|
@@ -107,20 +107,44 @@ Reuse as you wish!
|
|
107
107
|
* SAME(object) - Product returns `true` when matched with `#equal?`
|
108
108
|
* SEND(name, pattern) - Basically provided for Enumerable
|
109
109
|
* BOOLEAN() - Product returns `true` when matched to `true` or `false`
|
110
|
+
* NIL() - Product returns `true` when matched to `nil` (Not consider `nil?`)
|
110
111
|
* ANYTHING() - Product returns `true`, always `true`
|
111
112
|
* NEVER() - Product returns `false`, always `false`
|
112
113
|
* XOR(pattern1, pattern2) - Product returns `true` when matched one of the pattern, when matched both returns `false`
|
113
114
|
* NAND(*patterns) - Product is an inverted `AND`
|
114
115
|
* NOR(*patterns) - Product is an inverted `OR`
|
115
116
|
|
116
|
-
###
|
117
|
+
### Best fit for RSpec's `satisfy` matcher too
|
117
118
|
|
118
|
-
|
119
|
+
All builders actually generate a `Proc (lambda)` instance.
|
120
|
+
The signature will fit for RSpec's built-in ["satisfy" matcher](https://relishapp.com/rspec/rspec-expectations/v/3-10/docs/built-in-matchers/satisfy-matcher) too.
|
119
121
|
|
120
|
-
|
121
|
-
|
122
|
+
```ruby
|
123
|
+
RSpec.describe RSpec::Matchers::BuiltIn::Satisfy do
|
124
|
+
let(:product) { Eqq.AND(Integer, 24..42) }
|
125
|
+
|
126
|
+
it 'perfectly works' do
|
127
|
+
expect(23).not_to satisfy(&product)
|
128
|
+
expect(24).to satisfy(&product)
|
129
|
+
expect(24.0).not_to satisfy(&product)
|
130
|
+
expect(42).to satisfy(&product)
|
131
|
+
expect(42.0).not_to satisfy(&product)
|
132
|
+
expect(43).not_to satisfy(&product)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
### Use builders without receiver specifying
|
138
|
+
|
139
|
+
When you felt annoy to write `Eqq` in many place, some ways exist.
|
140
|
+
|
141
|
+
* `Eqq.build(&block)` - In the block scope, all builder methods can be used without receiver
|
142
|
+
* `extend Eqq::Buildable` - In the class/module, all builders can be used as class methods
|
143
|
+
* `include Eqq::Buildable` - In the class/module, all builders can be used as instance methods
|
144
|
+
|
145
|
+
### Signature
|
122
146
|
|
123
|
-
This gem provides [ruby/rbs](https://github.com/ruby/rbs)
|
147
|
+
* This gem provides [ruby/rbs](https://github.com/ruby/rbs) signature file
|
124
148
|
|
125
149
|
## Links
|
126
150
|
|
@@ -129,4 +153,4 @@ This gem provides [ruby/rbs](https://github.com/ruby/rbs) signatures
|
|
129
153
|
|
130
154
|
## NOTE
|
131
155
|
|
132
|
-
* [
|
156
|
+
* ["eqq" is the implementation name of "#===" in CRuby](https://github.com/ruby/ruby/blob/2a685da1fcd928530509e99f5edb4117bc377994/range.c#L1859)
|
data/lib/eqq/buildable.rb
CHANGED
@@ -38,7 +38,7 @@ module Eqq
|
|
38
38
|
# @api private
|
39
39
|
# @return [void]
|
40
40
|
def validate_patterns(*patterns)
|
41
|
-
invalids = patterns.reject { |pattern| Eqq.
|
41
|
+
invalids = patterns.reject { |pattern| Eqq.pattern?(pattern) }
|
42
42
|
invalid_inspections = invalids.map { |invalid| safe_inspect_for(invalid) }.join(', ')
|
43
43
|
raise ArgumentError, "given `#{invalid_inspections}` are invalid as pattern objects" unless invalids.empty?
|
44
44
|
end
|
@@ -256,37 +256,46 @@ module Eqq
|
|
256
256
|
product
|
257
257
|
end
|
258
258
|
|
259
|
-
|
260
|
-
define_inspect_on(
|
261
|
-
private_constant :ANYTHING
|
259
|
+
EQQ_BUILTIN_ANYTHING = ->_v { true }
|
260
|
+
define_inspect_on(EQQ_BUILTIN_ANYTHING, name: 'ANYTHING', arguments: [])
|
262
261
|
|
263
262
|
# Product returns `true`, always `true`
|
264
263
|
#
|
265
264
|
# @return [Proc]
|
266
265
|
def ANYTHING
|
267
|
-
|
266
|
+
EQQ_BUILTIN_ANYTHING
|
268
267
|
end
|
269
268
|
|
270
|
-
|
271
|
-
define_inspect_on(
|
272
|
-
private_constant :NEVER
|
269
|
+
EQQ_BUILTIN_NEVER = ->_v { false }
|
270
|
+
define_inspect_on(EQQ_BUILTIN_NEVER, name: 'NEVER', arguments: [])
|
273
271
|
|
274
272
|
# Product returns `false`, always `false`
|
275
273
|
#
|
276
274
|
# @return [Proc]
|
277
275
|
def NEVER
|
278
|
-
|
276
|
+
EQQ_BUILTIN_NEVER
|
279
277
|
end
|
280
278
|
|
281
|
-
|
282
|
-
define_inspect_on(
|
283
|
-
private_constant :BOOLEAN
|
279
|
+
EQQ_BUILTIN_BOOLEAN = ->v { true.equal?(v) || false.equal?(v) }
|
280
|
+
define_inspect_on(EQQ_BUILTIN_BOOLEAN, name: 'BOOLEAN', arguments: [])
|
284
281
|
|
285
282
|
# Product returns `true` when matched to `true` or `false`
|
286
283
|
#
|
287
284
|
# @return [Proc]
|
288
285
|
def BOOLEAN
|
289
|
-
|
286
|
+
EQQ_BUILTIN_BOOLEAN
|
290
287
|
end
|
288
|
+
|
289
|
+
EQQ_BUILTIN_NIL = ->v { nil.equal?(v) }
|
290
|
+
define_inspect_on(EQQ_BUILTIN_NIL, name: 'NIL', arguments: [])
|
291
|
+
|
292
|
+
# Product returns `true` when matched to `nil` (Not consider `nil?`)
|
293
|
+
#
|
294
|
+
# @return [Proc]
|
295
|
+
def NIL
|
296
|
+
EQQ_BUILTIN_NIL
|
297
|
+
end
|
298
|
+
|
299
|
+
private_constant(:EQQ_BUILTIN_ANYTHING, :EQQ_BUILTIN_NEVER, :EQQ_BUILTIN_BOOLEAN, :EQQ_BUILTIN_NIL)
|
291
300
|
end
|
292
301
|
end
|
data/lib/eqq/version.rb
CHANGED
data/lib/eqq.rb
CHANGED
@@ -15,7 +15,7 @@ module Eqq
|
|
15
15
|
class InvalidProductError < Error; end
|
16
16
|
|
17
17
|
class << self
|
18
|
-
def
|
18
|
+
def pattern?(object)
|
19
19
|
case object
|
20
20
|
when Proc, Method
|
21
21
|
object.arity == 1
|
@@ -28,11 +28,20 @@ module Eqq
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
# @
|
32
|
-
|
33
|
-
|
31
|
+
# @api private
|
32
|
+
def satisfy?(object)
|
33
|
+
(Proc === object) && object.lambda? && (object.arity == 1) && object.respond_to?(:inspect)
|
34
|
+
end
|
35
|
+
|
36
|
+
# In the block scope, all builder methods can be used without receiver
|
37
|
+
#
|
38
|
+
# @return [Proc]
|
39
|
+
# @raise [InvalidProductError] if the return value is not looks to be built with builders
|
40
|
+
def build(&block)
|
41
|
+
raise ArgumentError, 'might be mis used the `Eqq.build` in your code' unless block
|
42
|
+
|
34
43
|
pattern = DSLScope.new.instance_exec(&block)
|
35
|
-
raise InvalidProductError unless
|
44
|
+
raise InvalidProductError, 'might be mis used the `Eqq.build` in your code' unless satisfy?(pattern)
|
36
45
|
|
37
46
|
pattern
|
38
47
|
end
|
@@ -47,5 +56,5 @@ module Eqq
|
|
47
56
|
class DSLScope
|
48
57
|
include Buildable
|
49
58
|
end
|
50
|
-
private_constant
|
59
|
+
private_constant(:DSLScope)
|
51
60
|
end
|
data/sig/eqq.rbs
CHANGED
@@ -17,13 +17,16 @@ module Eqq
|
|
17
17
|
type product = patternable_lambda & _Inspectable
|
18
18
|
|
19
19
|
# A private constant. Should not be used in your code.
|
20
|
-
|
20
|
+
EQQ_BUILTIN_ANYTHING: product
|
21
21
|
|
22
22
|
# A private constant. Should not be used in your code.
|
23
|
-
|
23
|
+
EQQ_BUILTIN_NEVER: product
|
24
24
|
|
25
25
|
# A private constant. Should not be used in your code.
|
26
|
-
|
26
|
+
EQQ_BUILTIN_BOOLEAN: product
|
27
|
+
|
28
|
+
# A private constant. Should not be used in your code.
|
29
|
+
EQQ_BUILTIN_NIL: product
|
27
30
|
|
28
31
|
# A private API. Should not be used in your code.
|
29
32
|
def self.safe_inspect_for: (untyped object)-> String
|
@@ -78,6 +81,9 @@ module Eqq
|
|
78
81
|
|
79
82
|
# Product returns `true` when matched to `true` or `false`
|
80
83
|
def BOOLEAN: () -> product
|
84
|
+
|
85
|
+
# Product returns `true` when matched to `nil` (Not consider `nil?`)
|
86
|
+
def NIL: () -> product
|
81
87
|
end
|
82
88
|
|
83
89
|
extend Buildable
|
@@ -98,8 +104,12 @@ module Eqq
|
|
98
104
|
VERSION: String
|
99
105
|
|
100
106
|
# Returns `true` when given object has patternable signature
|
101
|
-
def self.
|
107
|
+
def self.pattern?: (untyped object) -> bool
|
108
|
+
|
109
|
+
# Returns `true` when given object has correct signature as a product of builders
|
110
|
+
# Basically this is a private API. Should not be used in your code.
|
111
|
+
def self.satisfy?: (untyped object) -> bool
|
102
112
|
|
103
113
|
# In the block scope, all builder methods can be used without receiver
|
104
|
-
def self.
|
114
|
+
def self.build: { () -> _Patternable } -> _Patternable
|
105
115
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eqq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenichi Kamiya
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Pattern objects builder.
|
@@ -38,7 +38,8 @@ metadata:
|
|
38
38
|
homepage_uri: https://github.com/kachick/eqq
|
39
39
|
source_code_uri: https://github.com/kachick/eqq
|
40
40
|
bug_tracker_uri: https://github.com/kachick/eqq/issues
|
41
|
-
|
41
|
+
rubygems_mfa_required: 'true'
|
42
|
+
post_install_message:
|
42
43
|
rdoc_options: []
|
43
44
|
require_paths:
|
44
45
|
- lib
|
@@ -53,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
54
|
- !ruby/object:Gem::Version
|
54
55
|
version: '0'
|
55
56
|
requirements: []
|
56
|
-
rubygems_version: 3.
|
57
|
-
signing_key:
|
57
|
+
rubygems_version: 3.3.7
|
58
|
+
signing_key:
|
58
59
|
specification_version: 4
|
59
60
|
summary: Pattern objects builder
|
60
61
|
test_files: []
|