Class: ATP::Processors::PreCleaner

Inherits:
ATP::Processor show all
Defined in:
lib/atp/processors/pre_cleaner.rb

Overview

Modifies the AST by performing some basic clean up, mainly to sanitize user input. For example it will ensure that all IDs and references are underscored and lower cased.

Instance Method Summary collapse

Methods inherited from ATP::Processor

#clean_flag, #extract_volatiles, #handler_missing, #process, #process_all, #run, #volatile?, #volatile_flags

Constructor Details

#initializePreCleaner

Returns a new instance of PreCleaner.



7
8
9
# File 'lib/atp/processors/pre_cleaner.rb', line 7

def initialize
  @group_ids = []
end

Instance Method Details

#clean(id) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/atp/processors/pre_cleaner.rb', line 55

def clean(id)
  if id.is_a?(Array)
    id.map { |i| clean(i) }
  else
    id.to_s.symbolize.to_s
  end
end

#on_group(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/atp/processors/pre_cleaner.rb', line 30

def on_group(node)
  if id = node.children.find { |n| n.type == :id }
    @group_ids << process(id).value
  else
    @group_ids << nil
  end
  group = node.updated(nil, process_all(node.children))
  @group_ids.pop
  group
end

#on_id(node) ⇒ Object

Make all IDs lower cased symbols



12
13
14
15
# File 'lib/atp/processors/pre_cleaner.rb', line 12

def on_id(node)
  id = node.to_a[0]
  node.updated(nil, [clean(id)])
end

#on_if_failed(node) ⇒ Object Also known as: on_if_passed, on_if_any_failed, on_if_all_failed, on_if_any_passed, on_if_all_passed, on_if_ran, on_unless_ran

Make all ID references use the lower case symbols



18
19
20
21
# File 'lib/atp/processors/pre_cleaner.rb', line 18

def on_if_failed(node)
  id, *children = *node
  node.updated(nil, [clean(id)] + process_all(children))
end

#on_test(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/atp/processors/pre_cleaner.rb', line 41

def on_test(node)
  # Remove IDs nodes from test nodes if they refer to the ID of a parent group
  if @group_ids.last
    children = node.children.reject do |n|
      if n.type == :id
        @group_ids.last == process(n).value
      end
    end
  else
    children = node.children
  end
  node.updated(nil, process_all(children))
end