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



16
17
18
19
20
# File 'lib/dry/logic/predicates.rb', line 16

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



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

def [](name)
  method(name)
end

#array?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#attr?(name, input) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/dry/logic/predicates.rb', line 49

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

#bool?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#bytesize?(size, input) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
159
160
# File 'lib/dry/logic/predicates.rb', line 153

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)


229
230
231
232
233
# File 'lib/dry/logic/predicates.rb', line 229

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

#date?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#date_time?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#decimal?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#deprecated(name, in_favor_of) ⇒ Object



275
276
277
278
279
280
281
282
283
# File 'lib/dry/logic/predicates.rb', line 275

def deprecated(name, in_favor_of)
  Core::Deprecations.warn(
    "#{name} predicate is deprecated and will " \
    "be removed in the next major version\n" \
    "Please use #{in_favor_of} predicate instead",
    tag: "dry-logic",
    uplevel: 3
  )
end

#empty?(input) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/dry/logic/predicates.rb', line 53

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

#eql?(left, right = Undefined) ⇒ Boolean

This overrides Object#eql? so we need to make it compatible

Returns:

  • (Boolean)


203
204
205
206
207
# File 'lib/dry/logic/predicates.rb', line 203

def eql?(left, right = Undefined)
  return super(left) if right.equal?(Undefined)

  left.eql?(right)
end

#even?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def even?(input)
  input.even?
end

#excluded_from?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/dry/logic/predicates.rb', line 184

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

#excludes?(value, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#exclusion?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


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

def exclusion?(list, input)
  deprecated(:exclusion?, :excluded_from?)
  excluded_from?(list, input)
end

#false?(value) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/dry/logic/predicates.rb', line 221

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

#filled?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#float?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#format?(regex, input) ⇒ Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/dry/logic/predicates.rb', line 225

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

#gt?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#gteq?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/dry/logic/predicates.rb', line 132

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

#hash?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#included_in?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#includes?(value, input) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
192
193
194
195
196
# File 'lib/dry/logic/predicates.rb', line 188

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)


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

def inclusion?(list, input)
  deprecated(:inclusion?, :included_in?)
  included_in?(list, input)
end

#int?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#key?(name, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lt?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lteq?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#max_bytesize?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#max_size?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/dry/logic/predicates.rb', line 149

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

#min_bytesize?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#min_size?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

def nil?(input)
  input.nil?
end

#not_eql?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#number?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#odd?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def odd?(input)
  input.odd?
end

#predicate(name, &block) ⇒ Object



271
272
273
# File 'lib/dry/logic/predicates.rb', line 271

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

#respond_to?(method, input = Undefined) ⇒ Boolean

This overrides Object#respond_to? so we need to make it compatible

Returns:

  • (Boolean)


265
266
267
268
269
# File 'lib/dry/logic/predicates.rb', line 265

def respond_to?(method, input = Undefined)
  return super if input.equal?(Undefined)

  input.respond_to?(method)
end

#size?(size, input) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
# File 'lib/dry/logic/predicates.rb', line 136

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)


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

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

#time?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#true?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#type?(type, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uri?(schemes, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uri_rfc3986?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uuid_v1?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uuid_v2?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uuid_v3?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uuid_v4?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#uuid_v5?(input) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/dry/logic/predicates.rb', line 251

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