Module: Dry::Logic::Predicates::Methods

Included in:
Dry::Logic::Predicates
Defined in:
lib/dry/logic/predicates.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

UUIDv1 =
uuid_format(1)
UUIDv2 =
uuid_format(2)
UUIDv3 =
uuid_format(3)
UUIDv4 =
uuid_format(4)
UUIDv5 =
uuid_format(5)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.uuid_format(version) ⇒ Object



12
13
14
15
16
# File 'lib/dry/logic/predicates.rb', line 12

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

Instance Method Details

#[](name) ⇒ Object



28
29
30
# File 'lib/dry/logic/predicates.rb', line 28

def [](name)
  method(name)
end

#array?(input) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/dry/logic/predicates.rb', line 104

def array?(input)
  input.is_a?(Array)
end

#attr?(name, input) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/dry/logic/predicates.rb', line 45

def attr?(name, input)
  input.respond_to?(name)
end

#bool?(input) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/dry/logic/predicates.rb', line 62

def bool?(input)
  input.is_a?(TrueClass) || input.is_a?(FalseClass)
end

#bytesize?(size, input) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
# File 'lib/dry/logic/predicates.rb', line 149

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

#case?(pattern, input) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
225
226
# File 'lib/dry/logic/predicates.rb', line 222

def case?(pattern, input)
  # rubocop:disable Style/CaseEquality
  pattern === input
  # rubocop:enable Style/CaseEquality
end

#date?(input) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dry/logic/predicates.rb', line 66

def date?(input)
  input.is_a?(Date)
end

#date_time?(input) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/dry/logic/predicates.rb', line 70

def date_time?(input)
  input.is_a?(DateTime)
end

#decimal?(input) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/dry/logic/predicates.rb', line 92

def decimal?(input)
  input.is_a?(BigDecimal)
end

#empty?(input) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/dry/logic/predicates.rb', line 49

def empty?(input)
  case input
  when String, Array, Hash then input.empty?
  when nil then true
  else
    false
  end
end

#eql?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/dry/logic/predicates.rb', line 198

def eql?(left, right)
  left.eql?(right)
end

#even?(input) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/dry/logic/predicates.rb', line 112

def even?(input)
  input.even?
end

#excluded_from?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/dry/logic/predicates.rb', line 180

def excluded_from?(list, input)
  !list.include?(input)
end

#excludes?(value, input) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/dry/logic/predicates.rb', line 194

def excludes?(value, input)
  !includes?(value, input)
end

#exclusion?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/dry/logic/predicates.rb', line 171

def exclusion?(list, input)
  ::Kernel.warn "exclusion is deprecated - use excluded_from instead."
  excluded_from?(list, input)
end

#false?(value) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/dry/logic/predicates.rb', line 214

def false?(value)
  value.equal?(false)
end

#filled?(input) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/dry/logic/predicates.rb', line 58

def filled?(input)
  !empty?(input)
end

#float?(input) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/dry/logic/predicates.rb', line 88

def float?(input)
  input.is_a?(Float)
end

#format?(regex, input) ⇒ Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/dry/logic/predicates.rb', line 218

def format?(regex, input)
  !input.nil? && regex.match?(input)
end

#gt?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/dry/logic/predicates.rb', line 120

def gt?(num, input)
  input > num
end

#gteq?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/dry/logic/predicates.rb', line 128

def gteq?(num, input)
  !lt?(num, input)
end

#hash?(input) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/dry/logic/predicates.rb', line 100

def hash?(input)
  input.is_a?(Hash)
end

#included_in?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/dry/logic/predicates.rb', line 176

def included_in?(list, input)
  list.include?(input)
end

#includes?(value, input) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
# File 'lib/dry/logic/predicates.rb', line 184

def includes?(value, input)
  if input.respond_to?(:include?)
    input.include?(value)
  else
    false
  end
rescue TypeError
  false
end

#inclusion?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/dry/logic/predicates.rb', line 166

def inclusion?(list, input)
  ::Kernel.warn "inclusion is deprecated - use included_in instead."
  included_in?(list, input)
end

#int?(input) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/dry/logic/predicates.rb', line 84

def int?(input)
  input.is_a?(Integer)
end

#is?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/dry/logic/predicates.rb', line 202

def is?(left, right)
  left.equal?(right)
end

#key?(name, input) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dry/logic/predicates.rb', line 41

def key?(name, input)
  input.key?(name)
end

#lt?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/dry/logic/predicates.rb', line 116

def lt?(num, input)
  input < num
end

#lteq?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/dry/logic/predicates.rb', line 124

def lteq?(num, input)
  !gt?(num, input)
end

#max_bytesize?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/dry/logic/predicates.rb', line 162

def max_bytesize?(num, input)
  input.bytesize <= num
end

#max_size?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/dry/logic/predicates.rb', line 145

def max_size?(num, input)
  input.size <= num
end

#min_bytesize?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/dry/logic/predicates.rb', line 158

def min_bytesize?(num, input)
  input.bytesize >= num
end

#min_size?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/dry/logic/predicates.rb', line 141

def min_size?(num, input)
  input.size >= num
end

#nil?(input) ⇒ Boolean Also known as: none?

Returns:

  • (Boolean)


36
37
38
# File 'lib/dry/logic/predicates.rb', line 36

def nil?(input)
  input.nil?
end

#not_eql?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/dry/logic/predicates.rb', line 206

def not_eql?(left, right)
  !left.eql?(right)
end

#number?(input) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/dry/logic/predicates.rb', line 78

def number?(input)
  true if Float(input)
rescue ArgumentError, TypeError
  false
end

#odd?(input) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/dry/logic/predicates.rb', line 108

def odd?(input)
  input.odd?
end

#predicate(name, &block) ⇒ Object



261
262
263
# File 'lib/dry/logic/predicates.rb', line 261

def predicate(name, &block)
  define_singleton_method(name, &block)
end

#respond_to?(method, input) ⇒ Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/dry/logic/predicates.rb', line 257

def respond_to?(method, input)
  input.respond_to?(method)
end

#size?(size, input) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
# File 'lib/dry/logic/predicates.rb', line 132

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

#str?(input) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/dry/logic/predicates.rb', line 96

def str?(input)
  input.is_a?(String)
end

#time?(input) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/dry/logic/predicates.rb', line 74

def time?(input)
  input.is_a?(Time)
end

#true?(value) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/dry/logic/predicates.rb', line 210

def true?(value)
  value.equal?(true)
end

#type?(type, input) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dry/logic/predicates.rb', line 32

def type?(type, input)
  input.is_a?(type)
end

#uri?(schemes, input) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
251
# File 'lib/dry/logic/predicates.rb', line 248

def uri?(schemes, input)
  uri_format = URI::DEFAULT_PARSER.make_regexp(schemes)
  format?(uri_format, input)
end

#uri_rfc3986?(input) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/dry/logic/predicates.rb', line 253

def uri_rfc3986?(input)
  format?(URI::RFC3986_Parser::RFC3986_URI, input)
end

#uuid_v1?(input) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/dry/logic/predicates.rb', line 228

def uuid_v1?(input)
  format?(UUIDv1, input)
end

#uuid_v2?(input) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/dry/logic/predicates.rb', line 232

def uuid_v2?(input)
  format?(UUIDv2, input)
end

#uuid_v3?(input) ⇒ Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/dry/logic/predicates.rb', line 236

def uuid_v3?(input)
  format?(UUIDv3, input)
end

#uuid_v4?(input) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/dry/logic/predicates.rb', line 240

def uuid_v4?(input)
  format?(UUIDv4, input)
end

#uuid_v5?(input) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/dry/logic/predicates.rb', line 244

def uuid_v5?(input)
  format?(UUIDv5, input)
end