Class: Conditions::InCondition

Inherits:
BaseCondition show all
Defined in:
lib/conditions.rb

Overview

Checks if an element (or elements of an array) belong to an array Can be used as an “equals to” condition

Instance Method Summary collapse

Constructor Details

#initialize(predicate) ⇒ InCondition

Returns a new instance of InCondition.

Parameters:

  • predicate (Array)

    An array predicate

Raises:



26
27
28
29
30
# File 'lib/conditions.rb', line 26

def initialize(predicate)
  raise ConditionError, "In condition value must be an Array, not #{predicate.class}" unless predicate.is_a? Array

  super(predicate)
end

Instance Method Details

#apply(value) ⇒ true, false

Parameters:

  • value (Any, Array)

    A value to be checked against the predicate

Returns:

  • (true)

    if value and predicate have overlapping values

  • (false)

    if value and predicate have no overlapping values



36
37
38
39
# File 'lib/conditions.rb', line 36

def apply(value)
  value = [value] unless value.is_a? Array
  (value & @predicate).any?
end