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

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

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



9
10
11
# File 'lib/dry/logic/predicates.rb', line 9

def [](name)
  method(name)
end

#array?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#attr?(name, input) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/dry/logic/predicates.rb', line 26

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

#bool?(input) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dry/logic/predicates.rb', line 43

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

#case?(pattern, input) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/dry/logic/predicates.rb', line 196

def case?(pattern, input)
  pattern === input
end

#date?(input) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dry/logic/predicates.rb', line 47

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

#date_time?(input) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dry/logic/predicates.rb', line 51

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

#decimal?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#empty?(input) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/dry/logic/predicates.rb', line 30

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)


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

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

#even?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def even?(input)
  input.even?
end

#excluded_from?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#excludes?(value, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#exclusion?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#false?(value) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/dry/logic/predicates.rb', line 182

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

#filled?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#float?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#format?(regex, input) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/dry/logic/predicates.rb', line 187

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

#gt?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#gteq?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hash?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#included_in?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#includes?(value, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inclusion?(list, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#int?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#key?(name, input) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dry/logic/predicates.rb', line 22

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

#lt?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lteq?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#max_size?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#min_size?(num, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

def nil?(input)
  input.nil?
end

#not_eql?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#number?(input) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/dry/logic/predicates.rb', line 59

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

#odd?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def odd?(input)
  input.odd?
end

#predicate(name, &block) ⇒ Object



200
201
202
# File 'lib/dry/logic/predicates.rb', line 200

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

#size?(size, input) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
# File 'lib/dry/logic/predicates.rb', line 115

def size?(size, input)
  case size
  when Integer then size == 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)


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

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

#time?(input) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/dry/logic/predicates.rb', line 55

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

#true?(value) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/dry/logic/predicates.rb', line 178

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

#type?(type, input) ⇒ Boolean

Returns:

  • (Boolean)


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

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