eqq 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE.txt +21 -0
  3. data/README.md +38 -0
  4. data/lib/eqq.rb +239 -0
  5. data/lib/eqq/version.rb +6 -0
  6. metadata +265 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4bca6982f43fb57004acdca8facb594d0d178999bdcb323a41663138d9f00325
4
+ data.tar.gz: 31f2e909e814925d47cf64824ac1267597fc2b85d3a4704ed9612704f6b41071
5
+ SHA512:
6
+ metadata.gz: e30c7e8c661ffb9764e9c604503454e3bd29de40075f6e8296540d0b3a41a482c1221178d155e2c090b1b075ea4ec4d6f63306d5eaeba3b69e505e981abd0712
7
+ data.tar.gz: '0885388e12a098f0c26415b252f0bc552c39e526df9ffbed2beffcc046ffe778eee99a71120e23e6ea80c61c1aee67b5769208196fee9d422646a6d90e4e92d4'
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2011 Kenichi Kamiya
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # eqq
2
+
3
+ ![Build Status](https://github.com/kachick/eqq/actions/workflows/test_behaviors.yml/badge.svg?branch=main)
4
+ [![Gem Version](https://badge.fury.io/rb/eqq.png)](http://badge.fury.io/rb/eqq)
5
+
6
+ Pattern objects builder.
7
+
8
+ `eqq` means `#===`
9
+
10
+ ## Usage
11
+
12
+ Require Ruby 2.5 or later
13
+
14
+ Add below code into your Gemfile
15
+
16
+ ```ruby
17
+ gem 'eqq', '>= 0.0.1', '< 0.1.0'
18
+ ```
19
+
20
+ ### Overview
21
+
22
+ ```ruby
23
+ require 'eqq'
24
+
25
+ [4.2, 42, 42.0, 420].grep(Eqq.AND(Integer, 20..50)) #=> [42]
26
+ [42, nil, true, false, '', 0].grep(Eqq.BOOLEAN) #=> [true, false]
27
+ [42, [], {}, 'string', Object.new, nil].grep(Eqq.CAN(:to_h)) #=> [[], {}, nil]
28
+
29
+ pattern = Eqq.define do
30
+ OR(AND(Float, 20..50), Integer)
31
+ end
32
+ [4.2, 42, 42.0, 420].grep(pattern) #=> [42, 42.0, 420]
33
+ ```
34
+
35
+ ## Links
36
+
37
+ * [Repository](https://github.com/kachick/eqq)
38
+ * [API documents](https://kachick.github.io/eqq)
data/lib/eqq.rb ADDED
@@ -0,0 +1,239 @@
1
+ # coding: us-ascii
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright (c) 2011 Kenichi Kamiya
5
+ # Forked from https://github.com/kachick/eqq at 2021
6
+
7
+ module Eqq
8
+ def self.conditionable?(object)
9
+ case object
10
+ when Proc, Method
11
+ object.arity == 1
12
+ else
13
+ begin
14
+ object.respond_to?(:===)
15
+ rescue NoMethodError
16
+ false
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.define(&block)
22
+ module_exec(&block)
23
+ end
24
+
25
+ module_function
26
+
27
+ # A condition builder.
28
+ # @param condition1 [Proc, Method, #===]
29
+ # @param condition2 [Proc, Method, #===]
30
+ # @param conditions [Array<Proc, Method, #===>]
31
+ # @return [Proc]
32
+ # this lambda return true if match all conditions
33
+ def AND(condition1, condition2, *conditions)
34
+ ->v {
35
+ [condition1, condition2, *conditions].all? { |condition| condition === v }
36
+ }
37
+ end
38
+
39
+ # A condition builder.
40
+ # @param condition1 [Proc, Method, #===]
41
+ # @param condition2 [Proc, Method, #===]
42
+ # @param conditions [Array<Proc, Method, #===>]
43
+ # @return [Proc]
44
+ def NAND(condition1, condition2, *conditions)
45
+ NOT(AND(condition1, condition2, *conditions))
46
+ end
47
+
48
+ # A condition builder.
49
+ # @param condition1 [Proc, Method, #===]
50
+ # @param condition2 [Proc, Method, #===]
51
+ # @param conditions [Array<Proc, Method, #===>]
52
+ # @return [Proc]
53
+ # this lambda return true if match a any condition
54
+ def OR(condition1, condition2, *conditions)
55
+ ->v {
56
+ [condition1, condition2, *conditions].any? { |condition| condition === v }
57
+ }
58
+ end
59
+
60
+ # A condition builder.
61
+ # @param condition1 [Proc, Method, #===]
62
+ # @param condition2 [Proc, Method, #===]
63
+ # @param conditions [Array<Proc, Method, #===>]
64
+ # @return [Proc]
65
+ def NOR(condition1, condition2, *conditions)
66
+ NOT(OR(condition1, condition2, *conditions))
67
+ end
68
+
69
+ # A condition builder.
70
+ # @param condition1 [Proc, Method, #===]
71
+ # @param condition2 [Proc, Method, #===]
72
+ # @param conditions [Array<Proc, Method, #===>]
73
+ # @return [Proc]
74
+ def XOR(condition1, condition2, *conditions)
75
+ ->v {
76
+ [condition1, condition2, *conditions].one? { |condition| condition === v }
77
+ }
78
+ end
79
+
80
+ # A condition builder.
81
+ # @param condition1 [Proc, Method, #===]
82
+ # @param condition2 [Proc, Method, #===]
83
+ # @param conditions [Array<Proc, Method, #===>]
84
+ # @return [Proc]
85
+ def XNOR(condition1, condition2, *conditions)
86
+ NOT(XOR(condition1, condition2, *conditions))
87
+ end
88
+
89
+ # A condition builder.
90
+ # @param condition [Proc, Method, #===]
91
+ # @return [Proc] A condition invert the original condition.
92
+ def NOT(condition)
93
+ unless Eqq.conditionable?(condition)
94
+ raise TypeError, 'wrong object for condition'
95
+ end
96
+
97
+ ->v { !(condition === v) }
98
+ end
99
+
100
+ # A condition builder.
101
+ # @param obj [#==]
102
+ # @return [Proc]
103
+ # this lambda return true if a argument match under #== method
104
+ def EQ(obj)
105
+ ->v { obj == v }
106
+ end
107
+
108
+ # A condition builder.
109
+ # @param obj [#equal?]
110
+ # @return [Proc]
111
+ # this lambda return true if a argument match under #equal? method
112
+ def SAME(obj)
113
+ ->v { obj.equal?(v) }
114
+ end
115
+
116
+ # A condition builder.
117
+ # @param message1 [Symbol, String]
118
+ # @param messages [Array<Symbol, String>]
119
+ # @return [Proc]
120
+ # this lambda return true if a argument respond to all messages
121
+ def CAN(message1, *messages)
122
+ messages = [message1, *messages].map(&:to_sym)
123
+
124
+ ->v {
125
+ messages.all? { |message| v.respond_to?(message) }
126
+ }
127
+ end
128
+
129
+ # A condition builder.
130
+ # @param condition1 [Proc, Method, #===]
131
+ # @param conditions [Array<Proc, Method, #===>]
132
+ # @return [Proc]
133
+ # this lambda return true
134
+ # if face no exception when a argument checking under all conditions
135
+ def QUIET(condition1, *conditions)
136
+ conditions = [condition1, *conditions]
137
+ unless conditions.all? { |c| Eqq.conditionable?(c) }
138
+ raise TypeError, 'wrong object for condition'
139
+ end
140
+
141
+ ->v {
142
+ conditions.all? { |condition|
143
+ begin
144
+ condition === v
145
+ rescue Exception
146
+ false
147
+ else
148
+ true
149
+ end
150
+ }
151
+ }
152
+ end
153
+
154
+ # A condition builder.
155
+ # @param exception [Exception]
156
+ # @param exceptions [Array<Exception>]
157
+ # @return [Proc]
158
+ # this lambda return true
159
+ # if catch any kindly exceptions when a argument checking in a block parameter
160
+ def RESCUE(exception, *exceptions, &condition)
161
+ exceptions = [exception, *exceptions]
162
+ raise ArgumentError unless Eqq.conditionable?(condition)
163
+ raise ArgumentError unless exceptions.all?(Exception)
164
+
165
+ ->v {
166
+ begin
167
+ condition.call(v)
168
+ false
169
+ rescue *exceptions
170
+ true
171
+ rescue Exception
172
+ false
173
+ end
174
+ }
175
+ end
176
+
177
+ # A condition builder.
178
+ # @param exception [Exception]
179
+ # @return [Proc]
180
+ # this lambda return true
181
+ # if catch a specific exception when a argument checking in a block parameter
182
+ def CATCH(exception, &condition)
183
+ raise ArgumentError unless Eqq.conditionable?(condition)
184
+ raise ArgumentError unless exceptions.all?(Exception)
185
+
186
+ ->v {
187
+ begin
188
+ condition.call(v)
189
+ rescue Exception => err
190
+ err.instance_of?(exception)
191
+ else
192
+ false
193
+ end
194
+ }
195
+ end
196
+
197
+ # A condition builder.
198
+ # @param condition1 [Proc, Method, #===]
199
+ # @param condition2 [Proc, Method, #===]
200
+ # @param conditions [Array<Proc, Method, #===>]
201
+ # @return [Proc]
202
+ # this lambda return true
203
+ # if all included objects match all conditions
204
+ def ALL(condition1, condition2, *conditions)
205
+ condition = Eqq.AND(condition1, condition2, *conditions)
206
+
207
+ ->list {
208
+ enum = (
209
+ case
210
+ when list.respond_to?(:each_value)
211
+ list.each_value
212
+ when list.respond_to?(:all?)
213
+ list
214
+ when list.respond_to?(:each)
215
+ list.each
216
+ else
217
+ return false
218
+ end
219
+ )
220
+
221
+ enum.all?(condition)
222
+ }
223
+ end
224
+
225
+ def ANYTHING
226
+ # BasicObject.=== always passing
227
+ BasicObject
228
+ end
229
+
230
+ BOOLEAN = OR(SAME(true), SAME(false))
231
+
232
+ # A getter for a useful condition.
233
+ # @return [BOOLEAN] "true or false"
234
+ def BOOLEAN
235
+ BOOLEAN
236
+ end
237
+ end
238
+
239
+ require_relative 'eqq/version'
@@ -0,0 +1,6 @@
1
+ # coding: us-ascii
2
+ # frozen_string_literal: true
3
+
4
+ module Eqq
5
+ VERSION = '0.0.1'
6
+ end
metadata ADDED
@@ -0,0 +1,265 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eqq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kenichi Kamiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: test-unit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.4.1
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 3.4.1
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: irb
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.3.5
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.0'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.3.5
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: irb-power_assert
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '='
58
+ - !ruby/object:Gem::Version
59
+ version: 0.0.2
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '='
65
+ - !ruby/object:Gem::Version
66
+ version: 0.0.2
67
+ - !ruby/object:Gem::Dependency
68
+ name: warning
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 1.2.0
74
+ - - "<"
75
+ - !ruby/object:Gem::Version
76
+ version: '2.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.2.0
84
+ - - "<"
85
+ - !ruby/object:Gem::Version
86
+ version: '2.0'
87
+ - !ruby/object:Gem::Dependency
88
+ name: rake
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 13.0.3
94
+ - - "<"
95
+ - !ruby/object:Gem::Version
96
+ version: '20.0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 13.0.3
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '20.0'
107
+ - !ruby/object:Gem::Dependency
108
+ name: yard
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 0.9.26
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: '2'
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 0.9.26
124
+ - - "<"
125
+ - !ruby/object:Gem::Version
126
+ version: '2'
127
+ - !ruby/object:Gem::Dependency
128
+ name: rubocop
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 1.15.0
134
+ - - "<"
135
+ - !ruby/object:Gem::Version
136
+ version: 1.16.0
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: 1.15.0
144
+ - - "<"
145
+ - !ruby/object:Gem::Version
146
+ version: 1.16.0
147
+ - !ruby/object:Gem::Dependency
148
+ name: rubocop-rake
149
+ requirement: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: 0.5.1
154
+ - - "<"
155
+ - !ruby/object:Gem::Version
156
+ version: 0.6.0
157
+ type: :development
158
+ prerelease: false
159
+ version_requirements: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: 0.5.1
164
+ - - "<"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.6.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-performance
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: 1.11.3
174
+ - - "<"
175
+ - !ruby/object:Gem::Version
176
+ version: 1.12.0
177
+ type: :development
178
+ prerelease: false
179
+ version_requirements: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: 1.11.3
184
+ - - "<"
185
+ - !ruby/object:Gem::Version
186
+ version: 1.12.0
187
+ - !ruby/object:Gem::Dependency
188
+ name: rubocop-rubycw
189
+ requirement: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: 0.1.6
194
+ - - "<"
195
+ - !ruby/object:Gem::Version
196
+ version: 0.2.0
197
+ type: :development
198
+ prerelease: false
199
+ version_requirements: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: 0.1.6
204
+ - - "<"
205
+ - !ruby/object:Gem::Version
206
+ version: 0.2.0
207
+ - !ruby/object:Gem::Dependency
208
+ name: rubocop-md
209
+ requirement: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: 1.0.1
214
+ - - "<"
215
+ - !ruby/object:Gem::Version
216
+ version: 2.0.0
217
+ type: :development
218
+ prerelease: false
219
+ version_requirements: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: 1.0.1
224
+ - - "<"
225
+ - !ruby/object:Gem::Version
226
+ version: 2.0.0
227
+ description: " [4.2, 42, 42.0, 420].grep(Eqq.AND(Integer, 20..50)) #=> [42]\n"
228
+ email:
229
+ - kachick1+ruby@gmail.com
230
+ executables: []
231
+ extensions: []
232
+ extra_rdoc_files: []
233
+ files:
234
+ - MIT-LICENSE.txt
235
+ - README.md
236
+ - lib/eqq.rb
237
+ - lib/eqq/version.rb
238
+ homepage: https://github.com/kachick/eqq
239
+ licenses:
240
+ - MIT
241
+ metadata:
242
+ documentation_uri: https://kachick.github.io/eqq
243
+ homepage_uri: https://github.com/kachick/eqq
244
+ source_code_uri: https://github.com/kachick/eqq
245
+ bug_tracker_uri: https://github.com/kachick/eqq/issues
246
+ post_install_message:
247
+ rdoc_options: []
248
+ require_paths:
249
+ - lib
250
+ required_ruby_version: !ruby/object:Gem::Requirement
251
+ requirements:
252
+ - - ">="
253
+ - !ruby/object:Gem::Version
254
+ version: 2.5.0
255
+ required_rubygems_version: !ruby/object:Gem::Requirement
256
+ requirements:
257
+ - - ">="
258
+ - !ruby/object:Gem::Version
259
+ version: '0'
260
+ requirements: []
261
+ rubygems_version: 3.2.15
262
+ signing_key:
263
+ specification_version: 4
264
+ summary: Pattern objects builder. `eqq` means `#===`
265
+ test_files: []