eqq 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -16
- data/lib/eqq/buildable.rb +33 -22
- data/lib/eqq/version.rb +1 -1
- data/sig/eqq.rbs +41 -5
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d6619f43d14ad7f0519f68caabc8cf3128ebc3898608aaad753adb81c929201
|
4
|
+
data.tar.gz: ef9edac7cfd62cc0ba23f55e86ad9194a5396550ec9b9d1ec17614f36a0f21c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a48164a2c1cea62810b4930fa92ca3ebb0b5a2a1b4877f743f1902de52b4ec429d590333437e54eae41ee917463ee5b3166f5c77394369bf2402ff6122cb4ee9
|
7
|
+
data.tar.gz: 880f51dbce3e27f845b05a520decd8c9a17b7c400837d29cb1ad0597ae00279d888c708632dd64eb5457f7ed9d023c9e71951e5b5c4a28c04252727043852f0f
|
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', '0.0.
|
15
|
+
gem 'eqq', '>= 0.0.5', '< 0.1.0'
|
16
16
|
```
|
17
17
|
|
18
18
|
### Overview
|
@@ -97,26 +97,27 @@ Reuse as you wish!
|
|
97
97
|
|
98
98
|
### Builders
|
99
99
|
|
100
|
-
* OR(*patterns)
|
101
|
-
* AND(*patterns)
|
102
|
-
* NOT(pattern)
|
103
|
-
* CAN(*method_names)
|
104
|
-
* RESCUE(exception_class/module, pattern)
|
105
|
-
* QUIET(*patterns)
|
106
|
-
* EQ(object)
|
107
|
-
* SAME(object)
|
108
|
-
* SEND(name, pattern)
|
109
|
-
* BOOLEAN()
|
110
|
-
* ANYTHING()
|
111
|
-
*
|
112
|
-
*
|
113
|
-
*
|
100
|
+
* OR(*patterns) - Product returns `true` when matched even one pattern
|
101
|
+
* AND(*patterns) - Product returns `true` when matched all patterns
|
102
|
+
* NOT(pattern) - Product returns `true` when not matched the pattern
|
103
|
+
* CAN(*method_names) - Product returns `true` when it has all of the methods (checked with `respond_to?`)
|
104
|
+
* RESCUE(exception_class/module, pattern) - Product returns `true` when the pattern raises the exception
|
105
|
+
* QUIET(*patterns) - Product returns `true` when all patterns did not raise any exception
|
106
|
+
* EQ(object) - Product returns `true` when matched with `#==`
|
107
|
+
* SAME(object) - Product returns `true` when matched with `#equal?`
|
108
|
+
* SEND(name, pattern) - Basically provided for Enumerable
|
109
|
+
* BOOLEAN() - Product returns `true` when matched to `true` or `false`
|
110
|
+
* ANYTHING() - Product returns `true`, always `true`
|
111
|
+
* NEVER() - Product returns `false`, always `false`
|
112
|
+
* XOR(pattern1, pattern2) - Product returns `true` when matched one of the pattern, when matched both returns `false`
|
113
|
+
* NAND(*patterns) - Product is an inverted `AND`
|
114
|
+
* NOR(*patterns) - Product is an inverted `OR`
|
114
115
|
|
115
116
|
### Additional information
|
116
117
|
|
117
118
|
When you feel annoy to write `Eqq` in many place, 2 ways exist.
|
118
119
|
|
119
|
-
* `Eqq.define` - In the block scope, all builder methods can be used without receiver
|
120
|
+
* `Eqq.define(&block)` - In the block scope, all builder methods can be used without receiver
|
120
121
|
* `include Eqq::Buildable` - In the class/module, all builders can be used as own method
|
121
122
|
|
122
123
|
This gem provides [ruby/rbs](https://github.com/ruby/rbs) signatures
|
data/lib/eqq/buildable.rb
CHANGED
@@ -11,7 +11,7 @@ module Eqq
|
|
11
11
|
|
12
12
|
# @api private
|
13
13
|
# @return [String]
|
14
|
-
def
|
14
|
+
def safe_inspect_for(object)
|
15
15
|
String.try_convert(object.inspect) || INSPECTION_FALLBACK
|
16
16
|
rescue Exception
|
17
17
|
# This implementation used `RSpec::Support::ObjectFormatter::UninspectableObjectInspector` as a reference, thank you!
|
@@ -28,8 +28,8 @@ module Eqq
|
|
28
28
|
|
29
29
|
# @api private
|
30
30
|
# @return [void]
|
31
|
-
def
|
32
|
-
inspect = "#{name}(#{arguments.map { |argument|
|
31
|
+
def define_inspect_on(product, name:, arguments:)
|
32
|
+
inspect = "#{name}(#{arguments.map { |argument| safe_inspect_for(argument) }.join(', ')})".freeze
|
33
33
|
product.define_singleton_method(:inspect) do
|
34
34
|
inspect
|
35
35
|
end
|
@@ -39,7 +39,7 @@ module Eqq
|
|
39
39
|
# @return [void]
|
40
40
|
def validate_patterns(*patterns)
|
41
41
|
invalids = patterns.reject { |pattern| Eqq.valid?(pattern) }
|
42
|
-
invalid_inspections = invalids.map { |invalid|
|
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
|
45
45
|
end
|
@@ -58,12 +58,12 @@ module Eqq
|
|
58
58
|
patterns.all? { |pattern| pattern === v }
|
59
59
|
}
|
60
60
|
|
61
|
-
Buildable.
|
61
|
+
Buildable.define_inspect_on(product, name: 'AND', arguments: patterns)
|
62
62
|
|
63
63
|
product
|
64
64
|
end
|
65
65
|
|
66
|
-
# Product is inverted {#AND}
|
66
|
+
# Product is an inverted {#AND}
|
67
67
|
#
|
68
68
|
# @param pattern1 [Proc, Method, #===]
|
69
69
|
# @param pattern2 [Proc, Method, #===]
|
@@ -86,12 +86,12 @@ module Eqq
|
|
86
86
|
product = ->v {
|
87
87
|
patterns.any? { |pattern| pattern === v }
|
88
88
|
}
|
89
|
-
Buildable.
|
89
|
+
Buildable.define_inspect_on(product, name: 'OR', arguments: patterns)
|
90
90
|
|
91
91
|
product
|
92
92
|
end
|
93
93
|
|
94
|
-
# Product is inverted {#OR}
|
94
|
+
# Product is an inverted {#OR}
|
95
95
|
#
|
96
96
|
# @param pattern1 [Proc, Method, #===]
|
97
97
|
# @param pattern2 [Proc, Method, #===]
|
@@ -113,7 +113,7 @@ module Eqq
|
|
113
113
|
product = ->v {
|
114
114
|
patterns.one? { |pattern| pattern === v }
|
115
115
|
}
|
116
|
-
Buildable.
|
116
|
+
Buildable.define_inspect_on(product, name: 'XOR', arguments: patterns)
|
117
117
|
|
118
118
|
product
|
119
119
|
end
|
@@ -127,7 +127,7 @@ module Eqq
|
|
127
127
|
|
128
128
|
product = ->v { !(pattern === v) }
|
129
129
|
|
130
|
-
Buildable.
|
130
|
+
Buildable.define_inspect_on(product, name: 'NOT', arguments: [pattern])
|
131
131
|
|
132
132
|
product
|
133
133
|
end
|
@@ -137,9 +137,9 @@ module Eqq
|
|
137
137
|
# @param obj [#==]
|
138
138
|
# @return [Proc]
|
139
139
|
def EQ(obj)
|
140
|
-
->v { obj == v }
|
141
|
-
|
142
|
-
|
140
|
+
product = ->v { obj == v }
|
141
|
+
Buildable.define_inspect_on(product, name: 'EQ', arguments: [obj])
|
142
|
+
product
|
143
143
|
end
|
144
144
|
|
145
145
|
# Product returns `true` when matched with `#equal?`
|
@@ -147,9 +147,9 @@ module Eqq
|
|
147
147
|
# @param obj [#equal?]
|
148
148
|
# @return [Proc]
|
149
149
|
def SAME(obj)
|
150
|
-
->v { obj.equal?(v) }
|
151
|
-
|
152
|
-
|
150
|
+
product = ->v { obj.equal?(v) }
|
151
|
+
Buildable.define_inspect_on(product, name: 'SAME', arguments: [obj])
|
152
|
+
product
|
153
153
|
end
|
154
154
|
|
155
155
|
# Product returns `true` when it has all of the methods (checked with `respond_to?`)
|
@@ -176,7 +176,7 @@ module Eqq
|
|
176
176
|
}
|
177
177
|
}
|
178
178
|
|
179
|
-
Buildable.
|
179
|
+
Buildable.define_inspect_on(product, name: 'CAN', arguments: messages)
|
180
180
|
|
181
181
|
product
|
182
182
|
end
|
@@ -202,7 +202,7 @@ module Eqq
|
|
202
202
|
}
|
203
203
|
}
|
204
204
|
|
205
|
-
Buildable.
|
205
|
+
Buildable.define_inspect_on(product, name: 'QUIET', arguments: patterns)
|
206
206
|
|
207
207
|
product
|
208
208
|
end
|
@@ -227,7 +227,7 @@ module Eqq
|
|
227
227
|
end
|
228
228
|
}
|
229
229
|
|
230
|
-
Buildable.
|
230
|
+
Buildable.define_inspect_on(product, name: 'RESCUE', arguments: [mod, pattern])
|
231
231
|
|
232
232
|
product
|
233
233
|
end
|
@@ -251,13 +251,13 @@ module Eqq
|
|
251
251
|
v.__send__(name, pattern)
|
252
252
|
}
|
253
253
|
|
254
|
-
Buildable.
|
254
|
+
Buildable.define_inspect_on(product, name: 'SEND', arguments: [name, pattern])
|
255
255
|
|
256
256
|
product
|
257
257
|
end
|
258
258
|
|
259
259
|
ANYTHING = ->_v { true }
|
260
|
-
|
260
|
+
define_inspect_on(ANYTHING, name: 'ANYTHING', arguments: [])
|
261
261
|
private_constant :ANYTHING
|
262
262
|
|
263
263
|
# Product returns `true`, always `true`
|
@@ -267,8 +267,19 @@ module Eqq
|
|
267
267
|
ANYTHING
|
268
268
|
end
|
269
269
|
|
270
|
+
NEVER = ->_v { false }
|
271
|
+
define_inspect_on(NEVER, name: 'NEVER', arguments: [])
|
272
|
+
private_constant :NEVER
|
273
|
+
|
274
|
+
# Product returns `false`, always `false`
|
275
|
+
#
|
276
|
+
# @return [Proc]
|
277
|
+
def NEVER
|
278
|
+
NEVER
|
279
|
+
end
|
280
|
+
|
270
281
|
BOOLEAN = ->v { true.equal?(v) || false.equal?(v) }
|
271
|
-
|
282
|
+
define_inspect_on(BOOLEAN, name: 'BOOLEAN', arguments: [])
|
272
283
|
private_constant :BOOLEAN
|
273
284
|
|
274
285
|
# Product returns `true` when matched to `true` or `false`
|
data/lib/eqq/version.rb
CHANGED
data/sig/eqq.rbs
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# Pattern objects builder
|
1
2
|
module Eqq
|
2
3
|
interface _Patternable
|
3
4
|
def ===: (untyped object) -> bool
|
@@ -18,39 +19,74 @@ module Eqq
|
|
18
19
|
# A private constant. Should not be used in your code.
|
19
20
|
ANYTHING: product
|
20
21
|
|
22
|
+
# A private constant. Should not be used in your code.
|
23
|
+
NEVER: product
|
24
|
+
|
21
25
|
# A private constant. Should not be used in your code.
|
22
26
|
BOOLEAN: product
|
23
27
|
|
24
28
|
# A private API. Should not be used in your code.
|
25
|
-
def self.
|
29
|
+
def self.safe_inspect_for: (untyped object)-> String
|
26
30
|
|
27
31
|
# A private API. Should not be used in your code.
|
28
|
-
def self.
|
32
|
+
def self.define_inspect_on: (patternable_lambda product, name: String, arguments: Array[untyped])-> void
|
29
33
|
|
30
34
|
# A private API. Should not be used in your code.
|
31
35
|
def self.validate_patterns: (*untyped) -> void
|
32
36
|
|
37
|
+
# Product returns `true` when matched even one pattern
|
33
38
|
def OR: (_Patternable, _Patternable, *_Patternable) -> product
|
39
|
+
|
40
|
+
# Product returns `true` when matched all patterns
|
34
41
|
def AND: (_Patternable, _Patternable, *_Patternable) -> product
|
42
|
+
|
43
|
+
# Product is an inverted `AND`
|
35
44
|
def NAND: (_Patternable, _Patternable, *_Patternable) -> product
|
45
|
+
|
46
|
+
# Product is an inverted `OR`
|
36
47
|
def NOR: (_Patternable, _Patternable, *_Patternable) -> product
|
48
|
+
|
49
|
+
# Product returns `true` when matched one of the pattern, when matched both returns `false`
|
37
50
|
def XOR: (_Patternable, _Patternable) -> product
|
51
|
+
|
52
|
+
# Product returns `true` when not matched the pattern
|
38
53
|
def NOT: (_Patternable) -> product
|
54
|
+
|
55
|
+
# Product returns `true` when matched with `#==`
|
39
56
|
def EQ: (untyped object) -> product
|
57
|
+
|
58
|
+
# Product returns `true` when matched with `#equal?`
|
40
59
|
def SAME: (untyped object) -> product
|
60
|
+
|
61
|
+
# Product returns `true` when it has all of the methods (checked with `respond_to?`)
|
41
62
|
def CAN: (_ToSym, *_ToSym) -> product
|
63
|
+
|
64
|
+
# Product returns `true` when the pattern raises the exception
|
42
65
|
def RESCUE: (Module, _Patternable) -> product
|
66
|
+
|
67
|
+
# Product returns `true` when all patterns did not raise any exception
|
43
68
|
def QUIET: (_Patternable, *_Patternable) -> product
|
69
|
+
|
70
|
+
# Basically provided for Enumerable
|
44
71
|
def SEND: (Symbol | String name, _Patternable) -> product
|
72
|
+
|
73
|
+
# Product returns `true`, always `true`
|
45
74
|
def ANYTHING: () -> product
|
75
|
+
|
76
|
+
# Product returns `false`, always `false`
|
77
|
+
def NEVER: () -> product
|
78
|
+
|
79
|
+
# Product returns `true` when matched to `true` or `false`
|
46
80
|
def BOOLEAN: () -> product
|
47
81
|
end
|
48
82
|
|
49
83
|
extend Buildable
|
50
84
|
|
85
|
+
# Base error of this library
|
51
86
|
class Error < StandardError
|
52
87
|
end
|
53
88
|
|
89
|
+
# Raised when found some products are invalid as a pattern object
|
54
90
|
class InvalidProductError < Error
|
55
91
|
end
|
56
92
|
|
@@ -61,9 +97,9 @@ module Eqq
|
|
61
97
|
|
62
98
|
VERSION: String
|
63
99
|
|
64
|
-
#
|
65
|
-
INSPECTION_FALLBACK: String
|
66
|
-
|
100
|
+
# Returns `true` when given object has patternable signature
|
67
101
|
def self.valid?: (untyped object) -> bool
|
102
|
+
|
103
|
+
# In the block scope, all builder methods can be used without receiver
|
68
104
|
def self.define: { () -> _Patternable } -> _Patternable
|
69
105
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eqq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenichi Kamiya
|
@@ -10,7 +10,14 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2021-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: |2
|
14
|
+
Pattern objects builder.
|
15
|
+
|
16
|
+
All products can be used as `pattern === something`.
|
17
|
+
|
18
|
+
All products can be mixed with other products as a parts.
|
19
|
+
|
20
|
+
Reuse as you wish!
|
14
21
|
email:
|
15
22
|
- kachick1+ruby@gmail.com
|
16
23
|
executables: []
|
@@ -49,5 +56,5 @@ requirements: []
|
|
49
56
|
rubygems_version: 3.2.15
|
50
57
|
signing_key:
|
51
58
|
specification_version: 4
|
52
|
-
summary: Pattern objects builder
|
59
|
+
summary: Pattern objects builder
|
53
60
|
test_files: []
|