Module: Rtml::TmlizedConditions

Defined in:
lib/rtml/tmlized_conditions.rb

Overview

This module provides helpers to “TML-ize” conditions.

Instance Method Summary collapse

Instance Method Details

#invert_condition(op) ⇒ Object

Returns the operation that is the opposite of the specified operation.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rtml/tmlized_conditions.rb', line 25

def invert_condition(op)
  case op
    when 'less'              then 'greater'
    when 'less_or_equal'     then 'greater_or_equal'
    when 'greater_or_equal'  then 'less_or_equal'
    when 'greater'           then 'less'
    when 'equal'             then 'not_equal'
    when 'not_equal'         then 'equal'
    when 'plus'              then 'minus'
    when 'minus'             then 'plus'
  end
end

#tmlize_condition(lo, op, ro, condition = :if) ⇒ Object

returns [lo, op, ro] such that each are valid within a <variant> element.

Condition can be :if or :unless.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rtml/tmlized_conditions.rb', line 7

def tmlize_condition(lo, op, ro, condition = :if)
  raise ArgumentError, "Expected condition to be :if or :unless" unless [:if, :unless].include? condition
  case op
    when '<',  'less',             'less_than'                then lo, op, ro = lo, 'less', ro
    when '<=', 'less_or_equal',    'less_than_or_equal_to'    then lo, op, ro = lo, 'less_or_equal', ro
    when '>=', 'greater_or_equal', 'greater_than_or_equal_to' then lo, op, ro = ro, 'less', lo
    when '>',  'greater',          'greater_than'             then lo, op, ro = ro, 'less_or_equal', lo
    when '==', 'equal',            'equal_to'                 then lo, op, ro = lo, 'equal', ro
    when '!=', 'not_equal',        'not_equal_to'             then lo, op, ro = lo, 'not_equal', ro
    when '=~'                                                 then op = 'contains'
    when '+',  'plus'                                         then op = 'plus'
    when '-',  'minus'                                        then op = 'minus'
  end
  op = invert_condition(op) if condition == :unless
  [lo, op, ro]
end