Class: ATP::AST::Builder

Inherits:
Object
  • Object
show all
Includes:
Factories
Defined in:
lib/atp/ast/builder.rb

Constant Summary collapse

CONDITION_KEYS =
[
  :if_enabled, :enabled, :enable_flag, :enable, :if_enable,
  :unless_enabled, :not_enabled, :disabled, :disable, :unless_enable,
  :if_failed, :unless_passed, :failed,
  :if_passed, :unless_failed, :passed,
  :if_ran, :if_executed,
  :unless_ran, :unless_executed,
  :job, :jobs, :if_job, :if_jobs,
  :unless_job, :unless_jobs,
  :if_any_failed, :unless_all_passed,
  :if_all_failed, :unless_any_passed,
  :if_any_passed, :unless_all_failed,
  :if_all_passed, :unless_any_failed,
  :if_flag, :unless_flag
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Factories

#n, #n0

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



9
10
11
# File 'lib/atp/ast/builder.rb', line 9

def initialize
  @context = { conditions: [] }
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/atp/ast/builder.rb', line 6

def context
  @context
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/atp/ast/builder.rb', line 7

def description
  @description
end

#source_fileObject

Returns the value of attribute source_file.



7
8
9
# File 'lib/atp/ast/builder.rb', line 7

def source_file
  @source_file
end

#source_line_numberObject

Returns the value of attribute source_line_number.



7
8
9
# File 'lib/atp/ast/builder.rb', line 7

def source_line_number
  @source_line_number
end

Instance Method Details

#apply_conditions(node, conditions) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/atp/ast/builder.rb', line 129

def apply_conditions(node, conditions)
  conditions.each do |key, value|
    # Sometimes conditions can be an array (in the case of the current context
    # being re-used), so rectify that now
    if key.is_a?(Hash)
      fail 'Something has gone wrong applying the test conditions' if key.size > 1
      key, value = key.first[0], key.first[1]
    end
    key = key.to_s.downcase.to_sym
    # Represent all condition values as lower cased strings internally
    if value.is_a?(Array)
      value = value.map { |v| v.to_s.downcase }
    else
      value = value.to_s.downcase
    end
    context[:conditions] << { key => value }
    case key
    when :if_enabled, :enabled, :enable_flag, :enable, :if_enable
      node = flow_flag(value, true, node)
    when :unless_enabled, :not_enabled, :disabled, :disable, :unless_enable
      node = flow_flag(value, false, node)
    when :if_failed, :unless_passed, :failed
      if value.is_a?(Array)
        fail 'if_failed only accepts one ID, use if_any_failed or if_all_failed for multiple IDs'
      end
      node = test_result(value, false, node)
    when :if_passed, :unless_failed, :passed
      if value.is_a?(Array)
        fail 'if_passed only accepts one ID, use if_any_passed or if_all_passed for multiple IDs'
      end
      node = test_result(value, true, node)
    when :if_any_failed, :unless_all_passed
      node = test_result(value, false, node)
    when :if_all_failed, :unless_any_passed
      node = value.reduce(nil) do |nodes, val|
        test_result(val, false, nodes ? nodes : node)
      end
    when :if_any_passed, :unless_all_failed
      node = test_result(value, true, node)
    when :if_all_passed, :unless_any_failed
      node = value.reduce(nil) do |nodes, val|
        test_result(val, true, nodes ? nodes : node)
      end
    when :if_ran, :if_executed
      node = test_executed(value, true, node)
    when :unless_ran, :unless_executed
      node = test_executed(value, false, node)
    when :job, :jobs, :if_job, :if_jobs
      node = job(value, true, node)
    when :unless_job, :unless_jobs
      node = job(value, false, node)
    when :if_flag
      if value.is_a?(Array)
        fail 'if_flag only accepts one flag'
      end
      node = run_flag(value, true, node)
    when :unless_flag
      if value.is_a?(Array)
        fail 'unless_flag only accepts one flag'
      end
      node = run_flag(value, false, node)
    else
      fail "Unknown test condition attribute - #{key} (#{value})"
    end
  end
  node
end

#continueObject



275
276
277
# File 'lib/atp/ast/builder.rb', line 275

def continue
  n0(:continue)
end

#cz(setup, node, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/atp/ast/builder.rb', line 98

def cz(setup, node, options = {})
  test = n(:cz, setup, node)
  if options[:conditions]
    apply_conditions(test, options[:conditions])
  else
    test
  end
end

#disable_flow_flag(var, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/atp/ast/builder.rb', line 71

def disable_flow_flag(var, options = {})
  test = n(:disable_flow_flag, var)
  if options[:conditions]
    apply_conditions(test, options[:conditions])
  else
    test
  end
end

#enable_flow_flag(var, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/atp/ast/builder.rb', line 62

def enable_flow_flag(var, options = {})
  test = n(:enable_flow_flag, var)
  if options[:conditions]
    apply_conditions(test, options[:conditions])
  else
    test
  end
end

#flow(str) ⇒ Object



13
14
15
# File 'lib/atp/ast/builder.rb', line 13

def flow(str)
  n(:flow, name(str))
end

#flow_flag(name, enabled, node) ⇒ Object



38
39
40
# File 'lib/atp/ast/builder.rb', line 38

def flow_flag(name, enabled, node)
  n(:flow_flag, name, enabled, node)
end

#group(group_name, nodes, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/atp/ast/builder.rb', line 80

def group(group_name, nodes, options = {})
  children = [name(group_name)]

  children << id(options[:id].to_s.downcase.to_sym) if options[:id]

  children << on_fail(options[:on_fail]) if options[:on_fail]
  children << on_pass(options[:on_pass]) if options[:on_pass]

  children += nodes
  group = n(:group, *children)

  if options[:conditions]
    apply_conditions(group, options[:conditions])
  else
    group
  end
end

#id(symbol) ⇒ Object



34
35
36
# File 'lib/atp/ast/builder.rb', line 34

def id(symbol)
  n(:id, symbol.to_sym)
end

#job(id, enabled, node) ⇒ Object



50
51
52
# File 'lib/atp/ast/builder.rb', line 50

def job(id, enabled, node)
  n(:job, id, enabled, node)
end

#log(str, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/atp/ast/builder.rb', line 21

def log(str, options = {})
  test = n(:log, str.to_s)
  if options[:conditions]
    apply_conditions(test, options[:conditions])
  else
    test
  end
end

#name(str) ⇒ Object



17
18
19
# File 'lib/atp/ast/builder.rb', line 17

def name(str)
  n(:name, str.to_s)
end

#new_contextObject



107
108
109
110
111
# File 'lib/atp/ast/builder.rb', line 107

def new_context
  @context = { conditions: [] }
  yield if block_given?
  @context
end

#number(val) ⇒ Object



271
272
273
# File 'lib/atp/ast/builder.rb', line 271

def number(val)
  n(:number, val.to_i)
end

#on_fail(options = {}) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/atp/ast/builder.rb', line 230

def on_fail(options = {})
  children = []
  if options[:bin] || options[:softbin]
    children << set_result(:fail, bin: options[:bin], softbin: options[:softbin], bin_description: options[:bin_description])
  end
  if options[:set_run_flag] || options[:set_flag]
    children << set_run_flag(options[:set_run_flag] || options[:set_flag])
  end
  children << continue if options[:continue]
  children << render(options[:render]) if options[:render]
  n(:on_fail, *children)
end

#on_pass(options = {}) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/atp/ast/builder.rb', line 243

def on_pass(options = {})
  children = []
  if options[:bin] || options[:softbin]
    children << set_result(:pass, bin: options[:bin], softbin: options[:softbin], bin_description: options[:bin_description])
  end
  if options[:set_run_flag] || options[:set_flag]
    children << set_run_flag(options[:set_run_flag] || options[:set_flag])
  end
  children << continue if options[:continue]
  children << render(options[:render]) if options[:render]
  n(:on_pass, *children)
end

#render(str) ⇒ Object



30
31
32
# File 'lib/atp/ast/builder.rb', line 30

def render(str)
  n(:render, str)
end

#run_flag(id, enabled, node) ⇒ Object



54
55
56
# File 'lib/atp/ast/builder.rb', line 54

def run_flag(id, enabled, node)
  n(:run_flag, id, enabled, node)
end

#set_result(type, options = {}) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/atp/ast/builder.rb', line 256

def set_result(type, options = {})
  children = []
  children << type
  children << n(:bin, options[:bin])  if options[:bin]
  children << n(:softbin, options[:softbin])  if options[:softbin]
  children << n(:bin_description, options[:bin_description])  if options[:bin_description]
  result = n(:set_result, *children)

  if options[:conditions]
    apply_conditions(result, options[:conditions])
  else
    result
  end
end

#set_run_flag(flag) ⇒ Object



58
59
60
# File 'lib/atp/ast/builder.rb', line 58

def set_run_flag(flag)
  n(:set_run_flag, flag)
end

#test(object, options = {}) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/atp/ast/builder.rb', line 197

def test(object, options = {})
  children = [n(:object, object)]

  n = (options[:name] || options[:tname] || options[:test_name])
  unless n
    [:name, :tname, :test_name].each do |m|
      n ||= object.respond_to?(m) ? object.send(m) : nil
    end
  end
  children << name(n) if n

  n = (options[:number] || options[:num] || options[:tnum] || options[:test_number])
  unless n
    [:number, :num, :tnum, :test_number].each do |m|
      n ||= object.respond_to?(m) ? object.send(m) : nil
    end
  end
  children << number(n) if n

  children << id(options[:id].to_s.downcase.to_sym) if options[:id]

  children << on_fail(options[:on_fail]) if options[:on_fail]
  children << on_pass(options[:on_pass]) if options[:on_pass]

  test = n(:test, *children)

  if options[:conditions]
    apply_conditions(test, options[:conditions])
  else
    test
  end
end

#test_executed(id, executed, node) ⇒ Object



46
47
48
# File 'lib/atp/ast/builder.rb', line 46

def test_executed(id, executed, node)
  n(:test_executed, id, executed, node)
end

#test_result(id, passed, node) ⇒ Object



42
43
44
# File 'lib/atp/ast/builder.rb', line 42

def test_result(id, passed, node)
  n(:test_result, id, passed, node)
end