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, *body = strip expressions
  if true_constant? conditional.to_html
    form
  elsif not body.empty?
    cond_operator body
  end
end

#decrement(variable) ⇒ Object



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

def decrement variable
  k = variable.to_html
  return variable_not_defined_error k if not is_defined? k
  old_value = get_operator(k).to_number
  return non_numeric_error old_value if not numeric? old_value
  update_variable k, old_value - 1
end

#define(expressions) ⇒ Object



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

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



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

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



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

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



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

def get_operator variable
  key = variable.to_html
  if scope.has_key? key
    scope[key]
  elsif @parser.variables.has_key? key
    @parser.variables[key].to_html
  else
    variable_not_defined_error key
  end
end

#if_operator(expressions) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/teepee/commander-mixins/control.rb', line 144

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



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

def increment variable
  k = variable.to_html
  return variable_not_defined_error k if not is_defined? k
  old_value = get_operator(k).to_number
  return non_numeric_error old_value if not numeric? old_value
  update_variable k, old_value + 1
end

#is_defined?(variables) ⇒ Boolean

Returns:



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

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

#is_scoped?(variables) ⇒ Boolean

Returns:



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

def is_scoped? variables
  if not variables.is_a? Array
    return is_scoped? [variables]
  else
    Set.new(variables.map(&:to_html))
      .subset? Set.new(scope.keys)
  end
end

#let1(expressions) ⇒ Object



167
168
169
170
171
# File 'lib/teepee/commander-mixins/control.rb', line 167

def let1 expressions
  variable, _, value, _, *body = expressions
  scope[variable.to_html] = value.to_html
  update_scopes(body).map(&:to_html)
end

#not_equal(numbers) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/teepee/commander-mixins/control.rb', line 173

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



183
184
185
# File 'lib/teepee/commander-mixins/control.rb', line 183

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

#progn_operator(expressions) ⇒ Object



187
188
189
# File 'lib/teepee/commander-mixins/control.rb', line 187

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

#prognil(expressions) ⇒ Object



191
192
193
194
# File 'lib/teepee/commander-mixins/control.rb', line 191

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

#undefine(expressions) ⇒ Object



196
197
198
199
200
201
# File 'lib/teepee/commander-mixins/control.rb', line 196

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

#unless_operator(expressions) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/teepee/commander-mixins/control.rb', line 203

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

#update_scopes(body) ⇒ Object



162
163
164
165
# File 'lib/teepee/commander-mixins/control.rb', line 162

def update_scopes body
  body.map {|node| node.use_scope scope}
  body
end

#update_variable(variable, value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/teepee/commander-mixins/control.rb', line 104

def update_variable variable, value
  k = variable.to_html
  value = value.to_html
  if is_scoped? k
    scope[k] = value
  elsif is_defined? k
    @parser.variables[k] = value
  else
    variable_not_defined_error k
  end
  value
end

#when_operator(expressions) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/teepee/commander-mixins/control.rb', line 216

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