Module: Teepee::CommanderMixins::Control

Included in:
Teepee::Commander
Defined in:
lib/teepee/commander-mixins/control.rb

Instance Method Summary collapse

Instance Method Details

#case_operator(expressions) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/teepee/commander-mixins/control.rb', line 42

def case_operator expressions
  value, _, *rest = strip expressions
  if value and not rest.empty?
    def cond_helper value, expressions
      test_value, _, form, *rest = strip expressions
      if equal [value.to_html, test_value.to_html]
        form
      elsif not rest.empty?
        cond_helper value, rest
      end
    end
    cond_helper value, rest
  end
end

#comment(expressions) ⇒ Object



57
58
59
# File 'lib/teepee/commander-mixins/control.rb', line 57

def comment expressions
  nil
end

#cond_operator(expressions) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/teepee/commander-mixins/control.rb', line 61

def cond_operator expressions
  conditional, _, form, *rest = strip expressions
  if true_constant? conditional.to_html
    form
  elsif not rest.empty?
    cond_operator rest
  end
end

#decrement(variable) ⇒ Object



70
71
72
73
74
75
# File 'lib/teepee/commander-mixins/control.rb', line 70

def decrement variable
  return variable_not_defined_error variable if not is_defined? variable
  old_value = get_operator(variable).to_number
  return non_numeric_error old_value if not numeric? old_value
  @parser.variables[variable.to_html] = old_value - 1
end

#define(expressions) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/teepee/commander-mixins/control.rb', line 77

def define expressions
  variable, _, value = expressions
  k = variable.to_html
  v = value.to_html
  @parser.variables[k] = v
  get_operator k
end

#dotimes(expressions) ⇒ Object



95
96
97
98
99
# File 'lib/teepee/commander-mixins/control.rb', line 95

def dotimes expressions
  n = expressions.first.to_number
  return "" if n.nil? or n < 1
  span_operator expressions[1..-1] * n
end

#equal(expressions) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/teepee/commander-mixins/control.rb', line 101

def equal expressions
  if expressions.empty?
    true_constant
  elsif expressions.length == 1
    true_constant
  else
    expressions[0].to_html == expressions[1].to_html and equal expressions.rest
  end
end

#get_operator(variable) ⇒ Object



111
112
113
# File 'lib/teepee/commander-mixins/control.rb', line 111

def get_operator variable
  @parser.variables[variable.to_html].to_html
end

#if_operator(expressions) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/teepee/commander-mixins/control.rb', line 115

def if_operator expressions
  expressions = strip expressions
  conditional, _, true_clause, _, false_clause = expressions
  if true_constant? conditional.to_html
    true_clause.to_html
  elsif false_clause
    false_clause.to_html
  end
end

#increment(variable) ⇒ Object



125
126
127
128
129
130
# File 'lib/teepee/commander-mixins/control.rb', line 125

def increment variable
  return variable_not_defined_error variable if not is_defined? variable
  old_value = get_operator(variable).to_number
  return non_numeric_error old_value if not numeric? old_value
  @parser.variables[variable.to_html] = old_value + 1
end

#is_defined?(variables) ⇒ Boolean

Returns:



85
86
87
88
89
90
91
92
93
# File 'lib/teepee/commander-mixins/control.rb', line 85

def is_defined? variables
  if not variables.is_a? Array
    return is_defined? [variables]
  elsif Set.new(variables.map(&:to_html)).subset? Set.new(@parser.variables.keys)
    true_constant
  else
    false_constant
  end
end

#not_equal(numbers) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/teepee/commander-mixins/control.rb', line 132

def not_equal numbers
  if numbers.empty?
    true_constant
  elsif numbers.length == 1
    true_constant
  else
    numbers[0].to_number != numbers[1].to_number and equal numbers.rest
  end
end

#prog1_operator(expressions) ⇒ Object



142
143
144
# File 'lib/teepee/commander-mixins/control.rb', line 142

def prog1_operator expressions
  expressions.map(&:to_html).first
end

#progn_operator(expressions) ⇒ Object



146
147
148
# File 'lib/teepee/commander-mixins/control.rb', line 146

def progn_operator expressions
  expressions.map(&:to_html).last
end

#prognil(expressions) ⇒ Object



150
151
152
153
# File 'lib/teepee/commander-mixins/control.rb', line 150

def prognil expressions
  expressions.map(&:to_html)
  ""
end

#undefine(expressions) ⇒ Object



155
156
157
158
159
160
# File 'lib/teepee/commander-mixins/control.rb', line 155

def undefine expressions
  expressions.each do |expression|
    @parser.variables.delete expression.to_html
  end
  ""
end

#unless_operator(expressions) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/teepee/commander-mixins/control.rb', line 162

def unless_operator expressions
  expressions = strip expressions
  conditional = expressions.first
  expressions = strip expressions.rest
  if false_constant? conditional.to_html
    if expressions.length <= 1
      expressions.first
    else
      span_operator expressions
    end
  end
end

#when_operator(expressions) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/teepee/commander-mixins/control.rb', line 175

def when_operator expressions
  expressions = strip expressions
  conditional = expressions.first
  expressions = strip expressions.rest
  if true_constant? conditional.to_html
    if expressions.length <= 1
      expressions.first
    else
      span_operator expressions
    end
  end
end