Module: Ruote::Exp::Condition

Defined in:
lib/ruote/exp/condition.rb

Overview

A few helper methods for evaluating :if and :unless expression attributes in process definitions.

Defined Under Namespace

Classes: ConditionError

Constant Summary

REGEXES =
{
  'evl_set' => /^(.+?)( +is)?( +not)?( +set)$/,
  'evl_null' => /^(.+?)( +is)?( +not)?( +null)$/,
  'evl_empty' => /^(.+[\]}"'])( +is)?( +not)?( +empty)$/,
  'evl_in' => /^(.+?)( +is)?( +not)?( +in +)(\[.*\]|\{.*\})$/
}

Class Method Summary (collapse)

Class Method Details

+ (Boolean) apply?(sif, sunless)

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/ruote/exp/condition.rb', line 54

def self.apply?(sif, sunless)

  return (true?(sif)) if sif != nil
  return ( ! true?(sunless)) if sunless != nil

  true
end

+ (Object) eval(code)

Evaluates the given [conditional] code string and returns the result.

Note : this is not a full Ruby evaluation !



92
93
94
95
96
97
98
99
# File 'lib/ruote/exp/condition.rb', line 92

def self.eval(code)

  evl(code)

rescue ArgumentError => ae

  raise ConditionError.new(code)
end

+ (Boolean) false?(conditional)

Returns true if the given conditional string evaluates to false.

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/ruote/exp/condition.rb', line 82

def self.false?(conditional)

  ( ! true?(conditional))
end

+ (Boolean) true?(conditional)

Returns true if the given conditional string evaluates to true.

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruote/exp/condition.rb', line 64

def self.true?(conditional)

  conditional = unescape(conditional.to_s)

  REGEXES.each do |method, regex|
    m = regex.match(conditional)
    return self.send(method, m) if m
  end

  evl(conditional) ? true : false

rescue ArgumentError => ae

  raise ConditionError.new(conditional)
end