Class: Veto::ConditionsEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/veto/conditions_evaluator.rb

Class Method Summary collapse

Class Method Details

.truthy?(condition, context, entity) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/veto/conditions_evaluator.rb', line 3

def self.truthy? condition, context, entity
  case condition
  when String
    !!entity.instance_eval(condition)
  when Symbol
    !!context.send(condition)
  when Proc
    !!condition.call(entity)
  else
    !!condition
  end
end

.truthy_conditions?(conditions, context, entity) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/veto/conditions_evaluator.rb', line 16

def self.truthy_conditions? conditions, context, entity
  return true if !conditions.key?(:if) && !conditions.key?(:unless)

  [*conditions[:if]].each do |condition| 
    return false unless truthy?(condition, context, entity) 
  end

  [*conditions[:unless]].each do |condition| 
    return false if truthy?(condition, context, entity) 
  end

  true
end