Skip to content

Commit 1e72b21

Browse files
committed
Use concise syntax for predicates
This is where it's useful
1 parent ab0714a commit 1e72b21

File tree

1 file changed

+52
-140
lines changed

1 file changed

+52
-140
lines changed

lib/dry/logic/predicates.rb

Lines changed: 52 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
module Dry
1111
module Logic
1212
module Predicates
13-
include Dry::Core::Constants
13+
include ::Dry::Core::Constants
1414

1515
# rubocop:disable Metrics/ModuleLength
1616
module Methods
@@ -40,139 +40,89 @@ def [](name)
4040
method(name)
4141
end
4242

43-
def type?(type, input)
44-
input.is_a?(type)
45-
end
43+
def type?(type, input) = input.is_a?(type)
4644

47-
def nil?(input)
48-
input.nil?
49-
end
45+
def nil?(input) = input.nil?
5046
alias_method :none?, :nil?
5147

52-
def key?(name, input)
53-
input.key?(name)
54-
end
48+
def key?(name, input) = input.key?(name)
5549

56-
def attr?(name, input)
57-
input.respond_to?(name)
58-
end
50+
def attr?(name, input) = input.respond_to?(name)
5951

6052
def empty?(input)
6153
case input
62-
when String, Array, Hash then input.empty?
54+
when ::String, ::Array, ::Hash then input.empty?
6355
when nil then true
6456
else
6557
false
6658
end
6759
end
6860

69-
def filled?(input)
70-
!empty?(input)
71-
end
61+
def filled?(input) = !empty?(input)
7262

73-
def bool?(input)
74-
input.is_a?(TrueClass) || input.is_a?(FalseClass)
75-
end
63+
def bool?(input) = input.equal?(true) || input.equal?(false)
7664

77-
def date?(input)
78-
input.is_a?(Date)
79-
end
65+
def date?(input) = input.is_a?(::Date)
8066

81-
def date_time?(input)
82-
input.is_a?(DateTime)
83-
end
67+
def date_time?(input) = input.is_a?(::DateTime)
8468

85-
def time?(input)
86-
input.is_a?(Time)
87-
end
69+
def time?(input) = input.is_a?(::Time)
8870

8971
def number?(input)
9072
true if Float(input)
91-
rescue ArgumentError, TypeError
73+
rescue ::ArgumentError, ::TypeError
9274
false
9375
end
9476

95-
def int?(input)
96-
input.is_a?(Integer)
97-
end
77+
def int?(input) = input.is_a?(::Integer)
9878

99-
def float?(input)
100-
input.is_a?(Float)
101-
end
79+
def float?(input) = input.is_a?(::Float)
10280

103-
def decimal?(input)
104-
input.is_a?(BigDecimal)
105-
end
81+
def decimal?(input) = input.is_a?(::BigDecimal)
10682

107-
def str?(input)
108-
input.is_a?(String)
109-
end
83+
def str?(input) = input.is_a?(::String)
11084

111-
def hash?(input)
112-
input.is_a?(Hash)
113-
end
85+
def hash?(input) = input.is_a?(::Hash)
11486

115-
def array?(input)
116-
input.is_a?(Array)
117-
end
87+
def array?(input) = input.is_a?(::Array)
11888

119-
def odd?(input)
120-
input.odd?
121-
end
89+
def odd?(input) = input.odd?
12290

123-
def even?(input)
124-
input.even?
125-
end
91+
def even?(input) = input.even?
12692

127-
def lt?(num, input)
128-
input < num
129-
end
93+
def lt?(num, input) = input < num
13094

131-
def gt?(num, input)
132-
input > num
133-
end
95+
def gt?(num, input) = input > num
13496

135-
def lteq?(num, input)
136-
!gt?(num, input)
137-
end
97+
def lteq?(num, input) = !gt?(num, input)
13898

139-
def gteq?(num, input)
140-
!lt?(num, input)
141-
end
99+
def gteq?(num, input) = !lt?(num, input)
142100

143101
def size?(size, input)
144102
case size
145-
when Integer then size.equal?(input.size)
146-
when Range, Array then size.include?(input.size)
103+
when ::Integer then size.equal?(input.size)
104+
when ::Range, ::Array then size.include?(input.size)
147105
else
148-
raise ArgumentError, "+#{size}+ is not supported type for size? predicate."
106+
raise ::ArgumentError, "+#{size}+ is not supported type for size? predicate."
149107
end
150108
end
151109

152-
def min_size?(num, input)
153-
input.size >= num
154-
end
110+
def min_size?(num, input) = input.size >= num
155111

156-
def max_size?(num, input)
157-
input.size <= num
158-
end
112+
def max_size?(num, input) = input.size <= num
159113

160114
def bytesize?(size, input)
161115
case size
162-
when Integer then size.equal?(input.bytesize)
163-
when Range, Array then size.include?(input.bytesize)
116+
when ::Integer then size.equal?(input.bytesize)
117+
when ::Range, ::Array then size.include?(input.bytesize)
164118
else
165-
raise ArgumentError, "+#{size}+ is not supported type for bytesize? predicate."
119+
raise ::ArgumentError, "+#{size}+ is not supported type for bytesize? predicate."
166120
end
167121
end
168122

169-
def min_bytesize?(num, input)
170-
input.bytesize >= num
171-
end
123+
def min_bytesize?(num, input) = input.bytesize >= num
172124

173-
def max_bytesize?(num, input)
174-
input.bytesize <= num
175-
end
125+
def max_bytesize?(num, input) = input.bytesize <= num
176126

177127
def inclusion?(list, input)
178128
deprecated(:inclusion?, :included_in?)
@@ -184,13 +134,9 @@ def exclusion?(list, input)
184134
excluded_from?(list, input)
185135
end
186136

187-
def included_in?(list, input)
188-
list.include?(input)
189-
end
137+
def included_in?(list, input) = list.include?(input)
190138

191-
def excluded_from?(list, input)
192-
!list.include?(input)
193-
end
139+
def excluded_from?(list, input) = !list.include?(input)
194140

195141
def includes?(value, input)
196142
if input.respond_to?(:include?)
@@ -202,9 +148,7 @@ def includes?(value, input)
202148
false
203149
end
204150

205-
def excludes?(value, input)
206-
!includes?(value, input)
207-
end
151+
def excludes?(value, input) = !includes?(value, input)
208152

209153
# This overrides Object#eql? so we need to make it compatible
210154
def eql?(left, right = Undefined)
@@ -213,72 +157,40 @@ def eql?(left, right = Undefined)
213157
left.eql?(right)
214158
end
215159

216-
def is?(left, right)
217-
left.equal?(right)
218-
end
160+
def is?(left, right) = left.equal?(right)
219161

220-
def not_eql?(left, right)
221-
!left.eql?(right)
222-
end
162+
def not_eql?(left, right) = !left.eql?(right)
223163

224-
def true?(value)
225-
value.equal?(true)
226-
end
164+
def true?(value) = value.equal?(true)
227165

228-
def false?(value)
229-
value.equal?(false)
230-
end
166+
def false?(value) = value.equal?(false)
231167

232-
def format?(regex, input)
233-
!input.nil? && regex.match?(input)
234-
end
168+
def format?(regex, input) = !input.nil? && regex.match?(input)
235169

236-
def case?(pattern, input)
237-
# rubocop:disable Style/CaseEquality
238-
pattern === input
239-
# rubocop:enable Style/CaseEquality
240-
end
170+
def case?(pattern, input) = pattern === input # rubocop:disable Style/CaseEquality
241171

242-
def uuid_v1?(input)
243-
format?(UUIDv1, input)
244-
end
172+
def uuid_v1?(input) = format?(UUIDv1, input)
245173

246-
def uuid_v2?(input)
247-
format?(UUIDv2, input)
248-
end
174+
def uuid_v2?(input) = format?(UUIDv2, input)
249175

250-
def uuid_v3?(input)
251-
format?(UUIDv3, input)
252-
end
176+
def uuid_v3?(input) = format?(UUIDv3, input)
253177

254-
def uuid_v4?(input)
255-
format?(UUIDv4, input)
256-
end
178+
def uuid_v4?(input) = format?(UUIDv4, input)
257179

258-
def uuid_v5?(input)
259-
format?(UUIDv5, input)
260-
end
180+
def uuid_v5?(input) = format?(UUIDv5, input)
261181

262-
def uuid_v6?(input)
263-
format?(UUIDv6, input)
264-
end
182+
def uuid_v6?(input) = format?(UUIDv6, input)
265183

266-
def uuid_v7?(input)
267-
format?(UUIDv7, input)
268-
end
184+
def uuid_v7?(input) = format?(UUIDv7, input)
269185

270-
def uuid_v8?(input)
271-
format?(UUIDv8, input)
272-
end
186+
def uuid_v8?(input) = format?(UUIDv8, input)
273187

274188
def uri?(schemes, input)
275189
uri_format = ::URI::DEFAULT_PARSER.make_regexp(schemes)
276190
format?(uri_format, input)
277191
end
278192

279-
def uri_rfc3986?(input)
280-
format?(::URI::RFC3986_Parser::RFC3986_URI, input)
281-
end
193+
def uri_rfc3986?(input) = format?(::URI::RFC3986_Parser::RFC3986_URI, input)
282194

283195
# This overrides Object#respond_to? so we need to make it compatible
284196
def respond_to?(method, input = Undefined)

0 commit comments

Comments
 (0)