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 are symbols, and that all names are lower-cased strings.

Instance Method Summary collapse

Methods inherited from ATP::Processor

#handler_missing, #n, #n0, #n1, #process, #run

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



50
51
52
53
54
55
56
# File 'lib/atp/processors/pre_cleaner.rb', line 50

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

#on_group(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/atp/processors/pre_cleaner.rb', line 25

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_test(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/atp/processors/pre_cleaner.rb', line 36

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

#on_test_executed(node) ⇒ Object Also known as: on_test_result

Make all ID references use the lower case symbols



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

def on_test_executed(node)
  children = node.children.dup
  children[0] = clean(children[0])
  node.updated(nil, process_all(children))
end