module Dry::Logic::Predicates::Methods

rubocop:disable Metrics/ModuleLength

Constants

UUIDv1
UUIDv2
UUIDv3
UUIDv4
UUIDv5

Public Class Methods

uuid_format(version) click to toggle source
# 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

Public Instance Methods

[](name) click to toggle source
# File lib/dry/logic/predicates.rb, line 32
def [](name)
  method(name)
end
array?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 108
def array?(input)
  input.is_a?(Array)
end
attr?(name, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 49
def attr?(name, input)
  input.respond_to?(name)
end
bool?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 66
def bool?(input)
  input.is_a?(TrueClass) || input.is_a?(FalseClass)
end
bytesize?(size, input) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/dry/logic/predicates.rb, line 70
def date?(input)
  input.is_a?(Date)
end
date_time?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 74
def date_time?(input)
  input.is_a?(DateTime)
end
decimal?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 96
def decimal?(input)
  input.is_a?(BigDecimal)
end
deprecated(name, in_favor_of) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

Calls superclass method
# 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) click to toggle source
# File lib/dry/logic/predicates.rb, line 116
def even?(input)
  input.even?
end
excluded_from?(list, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 184
def excluded_from?(list, input)
  !list.include?(input)
end
excludes?(value, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 198
def excludes?(value, input)
  !includes?(value, input)
end
exclusion?(list, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 175
def exclusion?(list, input)
  deprecated(:exclusion?, :excluded_from?)
  excluded_from?(list, input)
end
false?(value) click to toggle source
# File lib/dry/logic/predicates.rb, line 221
def false?(value)
  value.equal?(false)
end
filled?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 62
def filled?(input)
  !empty?(input)
end
float?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 92
def float?(input)
  input.is_a?(Float)
end
format?(regex, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 225
def format?(regex, input)
  !input.nil? && regex.match?(input)
end
gt?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 124
def gt?(num, input)
  input > num
end
gteq?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 132
def gteq?(num, input)
  !lt?(num, input)
end
hash?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 104
def hash?(input)
  input.is_a?(Hash)
end
included_in?(list, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 180
def included_in?(list, input)
  list.include?(input)
end
includes?(value, input) click to toggle source
# 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) click to toggle source
# File lib/dry/logic/predicates.rb, line 170
def inclusion?(list, input)
  deprecated(:inclusion?, :included_in?)
  included_in?(list, input)
end
int?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 88
def int?(input)
  input.is_a?(Integer)
end
is?(left, right) click to toggle source
# File lib/dry/logic/predicates.rb, line 209
def is?(left, right)
  left.equal?(right)
end
key?(name, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 45
def key?(name, input)
  input.key?(name)
end
lt?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 120
def lt?(num, input)
  input < num
end
lteq?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 128
def lteq?(num, input)
  !gt?(num, input)
end
max_bytesize?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 166
def max_bytesize?(num, input)
  input.bytesize <= num
end
max_size?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 149
def max_size?(num, input)
  input.size <= num
end
min_bytesize?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 162
def min_bytesize?(num, input)
  input.bytesize >= num
end
min_size?(num, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 145
def min_size?(num, input)
  input.size >= num
end
nil?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 40
def nil?(input)
  input.nil?
end
Also aliased as: none?
none?(input)
Alias for: nil?
not_eql?(left, right) click to toggle source
# File lib/dry/logic/predicates.rb, line 213
def not_eql?(left, right)
  !left.eql?(right)
end
number?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 82
def number?(input)
  true if Float(input)
rescue ArgumentError, TypeError
  false
end
odd?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 112
def odd?(input)
  input.odd?
end
predicate(name, &block) click to toggle source
# File lib/dry/logic/predicates.rb, line 271
def predicate(name, &block)
  define_singleton_method(name, &block)
end
respond_to?(method, input = Undefined) click to toggle source

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

Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
# File lib/dry/logic/predicates.rb, line 100
def str?(input)
  input.is_a?(String)
end
time?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 78
def time?(input)
  input.is_a?(Time)
end
true?(value) click to toggle source
# File lib/dry/logic/predicates.rb, line 217
def true?(value)
  value.equal?(true)
end
type?(type, input) click to toggle source
# File lib/dry/logic/predicates.rb, line 36
def type?(type, input)
  input.is_a?(type)
end
uri?(schemes, input) click to toggle source
# 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) click to toggle source
# File lib/dry/logic/predicates.rb, line 260
def uri_rfc3986?(input)
  format?(URI::RFC3986_Parser::RFC3986_URI, input)
end
uuid_v1?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 235
def uuid_v1?(input)
  format?(UUIDv1, input)
end
uuid_v2?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 239
def uuid_v2?(input)
  format?(UUIDv2, input)
end
uuid_v3?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 243
def uuid_v3?(input)
  format?(UUIDv3, input)
end
uuid_v4?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 247
def uuid_v4?(input)
  format?(UUIDv4, input)
end
uuid_v5?(input) click to toggle source
# File lib/dry/logic/predicates.rb, line 251
def uuid_v5?(input)
  format?(UUIDv5, input)
end