Module: Validation::Condition

Defined in:
lib/validation/condition.rb

Useful Conditions collapse

ANYTHING =
BasicObject
BOOLEAN =

BasicObject.=== always passing

->v{v.equal?(true) || v.equal?(false)}
STRINGABLE =
OR(String, Symbol, CAN(:to_str), CAN(:to_sym))

Support Building Conditions collapse

Useful Conditions collapse

Class Method Details

.ALL(condition, *conditions) ⇒ lambda Also known as: GENERICS

A condition builder.

Parameters:

  • condition (Proc, Method, #===)
  • conditions (Proc, Method, #===)

Returns:

  • (lambda)

    this lambda return true if all included objects match all conditions


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/validation/condition.rb', line 221

def ALL(condition, *conditions)
  conditions = [condition, *conditions]
  unless conditions.all?{|c|conditionable? c}
    raise TypeError, 'wrong object for condition'
  end
  
  ->list{
    enum = (
      (list.respond_to?(:each_value) && list.each_value) or
      (list.respond_to?(:all?) && list) or
      (list.respond_to?(:each) && list.each) or
      return false
    )
  
    conditions.all?{|cond|
      enum.all?{|v|
        _valid? cond, v
      }
    }
  }
end

.AND(cond1, cond2, *conds) ⇒ lambda

A condition builder.

Parameters:

  • cond1 (Proc, Method, #===)
  • cond2 (Proc, Method, #===)
  • conds (Proc, Method, #===)

Returns:

  • (lambda)

    this lambda return true if match all conditions


49
50
51
# File 'lib/validation/condition.rb', line 49

def AND(cond1, cond2, *conds)
  _logical_operator :all?, cond1, cond2, *conds
end

.ANYTHING?Boolean

Returns:

  • (Boolean)

272
273
274
# File 'lib/validation/condition.rb', line 272

def ANYTHING?
  ANYTHING
end

.BOOLEAN?BOOLEAN Also known as: BOOL?

A getter for a useful condition.

Returns:


278
279
280
# File 'lib/validation/condition.rb', line 278

def BOOLEAN?
  BOOLEAN
end

.CAN(message1, *messages) ⇒ lambda

A condition builder.

Parameters:

  • messages (Symbol, String)

Returns:

  • (lambda)

    this lambda return true if a argment respond to all messages


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/validation/condition.rb', line 133

def CAN(message1, *messages)
  messages = [message1, *messages]
  unless messages.all?{|s|
            [Symbol, String].any?{|klass|s.kind_of? klass}
          }
    raise TypeError, 'only Symbol or String for message'
  end
  
  ->v{
    messages.all?{|message|v.respond_to? message}
  }
end

.CATCH(exception, &condition) ⇒ lambda

A condition builder.

Parameters:

  • exception (Exception)

Returns:

  • (lambda)

    this lambda return true if catch a specific exception when a argment checking in a block parameter

Raises:

  • (ArgumentError)

200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/validation/condition.rb', line 200

def CATCH(exception, &condition)
  raise ArgumentError unless conditionable? condition
  raise TypeError, 'not error object' unless exception.ancestors.include? Exception
  
  ->v{
    begin
      _valid? condition, v
    rescue Exception
      $!.instance_of? exception
    else
      false
    end
  }
end

.conditionable?(object) ⇒ Boolean

true if object is sufficient for condition.

Parameters:

  • object (Object)

Returns:

  • (Boolean)

13
14
15
16
17
18
19
20
# File 'lib/validation/condition.rb', line 13

def conditionable?(object)
  case object
  when Proc, Method
    object.arity == 1
  else
    object.respond_to? :===
  end
end

.EQ(obj) ⇒ lambda

A condition builder.

Parameters:

  • obj (#==)

Returns:

  • (lambda)

    this lambda return true if a argment match under #== method


114
115
116
# File 'lib/validation/condition.rb', line 114

def EQ(obj)
  ->v{obj == v}
end

.EQUAL(obj) ⇒ lambda Also known as: SAME

A condition builder.

Parameters:

  • obj (#equal?)

Returns:

  • (lambda)

    this lambda return true if a argment match under #equal? method


122
123
124
# File 'lib/validation/condition.rb', line 122

def EQUAL(obj)
  ->v{obj.equal? v}
end

.MEMBER_OF(list, *lists) ⇒ lambda

A condition builder.

Parameters:

  • list (#all?)
  • lists (#all?)

Returns:

  • (lambda)

    this lambda return true if all lists including the object


252
253
254
255
256
257
258
259
260
261
# File 'lib/validation/condition.rb', line 252

def MEMBER_OF(list, *lists)
  lists = [list, *lists]
  unless lists.all?{|l|l.respond_to? :all?}
    raise TypeError, 'list must respond #all?'
  end
  
  ->v{
    lists.all?{|l|l.include? v}
  }
end

.NAND(cond1, cond2, *conds) ⇒ lambda

A condition builder.

Parameters:

  • cond1 (Proc, Method, #===)
  • cond2 (Proc, Method, #===)
  • conds (Proc, Method, #===)

Returns:

  • (lambda)

58
59
60
# File 'lib/validation/condition.rb', line 58

def NAND(cond1, cond2, *conds)
  NOT AND(cond1, cond2, *conds)
end

.NOR(cond1, cond2, *conds) ⇒ lambda

A condition builder.

Parameters:

  • cond1 (Proc, Method, #===)
  • cond2 (Proc, Method, #===)
  • conds (Proc, Method, #===)

Returns:

  • (lambda)

77
78
79
# File 'lib/validation/condition.rb', line 77

def NOR(cond1, cond2, *conds)
  NOT OR(cond1, cond2, *conds)
end

.NOT(condition) ⇒ lambda

A condition builder.

Parameters:

  • condition (Proc, Method, #===)

Returns:

  • (lambda)

    A condition invert the original condition.


102
103
104
105
106
107
108
# File 'lib/validation/condition.rb', line 102

def NOT(condition)
  unless conditionable? condition
    raise TypeError, 'wrong object for condition'
  end
  
  ->v{! _valid?(condition, v)}
end

.OR(cond1, cond2, *conds) ⇒ lambda

A condition builder.

Parameters:

  • cond1 (Proc, Method, #===)
  • cond2 (Proc, Method, #===)
  • conds (Proc, Method, #===)

Returns:

  • (lambda)

    this lambda return true if match a any condition


68
69
70
# File 'lib/validation/condition.rb', line 68

def OR(cond1, cond2, *conds)
  _logical_operator :any?, cond1, cond2, *conds
end

.QUIET(condition, *conditions) ⇒ lambda

A condition builder.

Parameters:

  • condition (Proc, Method, #===)
  • conditions (Proc, Method, #===)

Returns:

  • (lambda)

    this lambda return true if face no exception when a argment checking under all conditions


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/validation/condition.rb', line 152

def QUIET(condition, *conditions)
  conditions = [condition, *conditions]
  unless conditions.all?{|c|conditionable? c}
    raise TypeError, 'wrong object for condition'
  end
  
  ->v{
    conditions.all?{|cond|
      begin
        _valid? cond, v
      rescue Exception
        false
      else
        true
      end
    }
  }
end

.RESCUE(exception, *exceptions, &condition) ⇒ lambda

A condition builder.

Parameters:

  • exception (Exception)
  • exceptions (Exception)

Returns:

  • (lambda)

    this lambda return true if catch any kindly exceptions when a argment checking in a block parameter

Raises:

  • (ArgumentError)

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/validation/condition.rb', line 177

def RESCUE(exception, *exceptions, &condition)
  exceptions = [exception, *exceptions]
  raise ArgumentError unless conditionable? condition
  raise TypeError unless exceptions.all?{|e|e.ancestors.include? Exception}
  
  ->v{
    begin
      _valid? condition, v
    rescue *exceptions
      true
    rescue Exception
      false
    else
      false
    end
  }
end

.STRINGABLE?STRINGABLE

A getter for a useful condition.

Returns:


287
288
289
# File 'lib/validation/condition.rb', line 287

def STRINGABLE?
  STRINGABLE
end

.XNOR(cond1, cond2, *conds) ⇒ lambda

A condition builder.

Parameters:

  • cond1 (Proc, Method, #===)
  • cond2 (Proc, Method, #===)
  • conds (Proc, Method, #===)

Returns:

  • (lambda)

95
96
97
# File 'lib/validation/condition.rb', line 95

def XNOR(cond1, cond2, *conds)
  NOT XOR(cond1, cond2, *conds)
end

.XOR(cond1, cond2, *conds) ⇒ lambda

A condition builder.

Parameters:

  • cond1 (Proc, Method, #===)
  • cond2 (Proc, Method, #===)
  • conds (Proc, Method, #===)

Returns:

  • (lambda)

86
87
88
# File 'lib/validation/condition.rb', line 86

def XOR(cond1, cond2, *conds)
  _logical_operator :one?, cond1, cond2, *conds
end