-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
predicates.rb
275 lines (214 loc) · 5.62 KB
/
predicates.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# frozen_string_literal: true
require "bigdecimal"
require "bigdecimal/util"
require "date"
module Dry
module Logic
module Predicates
# rubocop:disable Metrics/ModuleLength
module Methods
def self.uuid_format(version)
::Regexp.new(<<~FORMAT.chomp, ::Regexp::IGNORECASE)
\\A[0-9A-F]{8}-[0-9A-F]{4}-#{version}[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\\z
FORMAT
end
UUIDv1 = uuid_format(1)
UUIDv2 = uuid_format(2)
UUIDv3 = uuid_format(3)
UUIDv4 = uuid_format(4)
UUIDv5 = uuid_format(5)
def [](name)
method(name)
end
def type?(type, input)
input.is_a?(type)
end
def nil?(input)
input.nil?
end
alias_method :none?, :nil?
def key?(name, input)
input.key?(name)
end
def attr?(name, input)
input.respond_to?(name)
end
def empty?(input)
case input
when String, Array, Hash then input.empty?
when nil then true
else
false
end
end
def filled?(input)
!empty?(input)
end
def bool?(input)
input.is_a?(TrueClass) || input.is_a?(FalseClass)
end
def date?(input)
input.is_a?(Date)
end
def date_time?(input)
input.is_a?(DateTime)
end
def time?(input)
input.is_a?(Time)
end
def number?(input)
true if Float(input)
rescue ArgumentError, TypeError
false
end
def int?(input)
input.is_a?(Integer)
end
def float?(input)
input.is_a?(Float)
end
def decimal?(input)
input.is_a?(BigDecimal)
end
def str?(input)
input.is_a?(String)
end
def hash?(input)
input.is_a?(Hash)
end
def array?(input)
input.is_a?(Array)
end
def odd?(input)
input.odd?
end
def even?(input)
input.even?
end
def lt?(num, input)
input < num
end
def gt?(num, input)
input > num
end
def lteq?(num, input)
!gt?(num, input)
end
def gteq?(num, input)
!lt?(num, input)
end
def size?(size, input)
case size
when Integer then size.equal?(input.size)
when Range, Array then size.include?(input.size)
else
raise ArgumentError, "+#{size}+ is not supported type for size? predicate."
end
end
def min_size?(num, input)
input.size >= num
end
def max_size?(num, input)
input.size <= num
end
def bytesize?(size, input)
case size
when Integer then size.equal?(input.bytesize)
when Range, Array then size.include?(input.bytesize)
else
raise ArgumentError, "+#{size}+ is not supported type for bytesize? predicate."
end
end
def min_bytesize?(num, input)
input.bytesize >= num
end
def max_bytesize?(num, input)
input.bytesize <= num
end
def inclusion?(list, input)
::Kernel.warn "inclusion is deprecated - use included_in instead."
included_in?(list, input)
end
def exclusion?(list, input)
::Kernel.warn "exclusion is deprecated - use excluded_from instead."
excluded_from?(list, input)
end
def included_in?(list, input)
list.include?(input)
end
def excluded_from?(list, input)
!list.include?(input)
end
def includes?(value, input)
if input.respond_to?(:include?)
input.include?(value)
else
false
end
rescue TypeError
false
end
def excludes?(value, input)
!includes?(value, input)
end
def eql?(left, right)
left.eql?(right)
end
def is?(left, right)
left.equal?(right)
end
def not_eql?(left, right)
!left.eql?(right)
end
def true?(value)
value.equal?(true)
end
def false?(value)
value.equal?(false)
end
def format?(regex, input)
!input.nil? && regex.match?(input)
end
def case?(pattern, input)
# rubocop:disable Style/CaseEquality
pattern === input
# rubocop:enable Style/CaseEquality
end
def uuid_v1?(input)
format?(UUIDv1, input)
end
def uuid_v2?(input)
format?(UUIDv2, input)
end
def uuid_v3?(input)
format?(UUIDv3, input)
end
def uuid_v4?(input)
format?(UUIDv4, input)
end
def uuid_v5?(input)
format?(UUIDv5, input)
end
def uri?(schemes, input)
uri_format = URI::DEFAULT_PARSER.make_regexp(schemes)
format?(uri_format, input)
end
def uri_rfc3986?(input)
format?(URI::RFC3986_Parser::RFC3986_URI, input)
end
def respond_to?(method, input)
input.respond_to?(method)
end
def predicate(name, &block)
define_singleton_method(name, &block)
end
end
extend Methods
def self.included(other)
super
other.extend(Methods)
end
end
# rubocop:enable Metrics/ModuleLength
end
end