Class: FlightControlTower::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/flight-control-tower/parser.rb

Class Method Summary collapse

Class Method Details

.create_connections(files_to_parse) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flight-control-tower/parser.rb', line 11

def self.create_connections(files_to_parse)
  results = {}
  results['document'] = { outbound: [], inbound: [] }
  files_to_parse.each do |f|
    content = File.read(f)
    next unless content.match(/defineComponent/)

    component_name = File.basename(f)
    results[component_name] = { outbound: [], inbound: [] }

    inbound = content.scan(/this.on\((.+?)\)/).map{|e| e[0].split(',').map(&:strip) }
    inbound.each do |args|
      next if args.include?('click')
      results[component_name][:inbound] << args[0] if args.length == 2
      results[component_name][:inbound] << args[1] if args.length == 3
    end

    outbound = content.scan(/this.trigger\((.+?)\)/).map{|e| e[0].split(',').map(&:strip) }
    outbound.each do |args|
      args.shift if args[0] == 'document' 
      results[component_name][:outbound] << args.first
    end
    results[component_name][:inbound].uniq!
    results[component_name][:outbound].uniq!
  end
  results
end

.create_graph(results) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flight-control-tower/parser.rb', line 39

def self.create_graph(results)
  graph = []
  results.each_pair do |component_name, component_events|
    component_events[:inbound].each do |event|
      results.each_pair do |next_component_name, next_component_events|
        next if component_name == next_component_name
        if results[next_component_name][:outbound].include?(event)
          graph << { source: next_component_name, target: component_name, eventName: event }
        end
      end
    end
  end

  results.each_pair do |component_name, component_events|
    component_events[:outbound].each do |event|
      results.each_pair do |next_component_name, next_component_events|
        next if component_name == next_component_name
        if results[next_component_name][:inbound].include?(event)
          graph << { source: component_name, target: next_component_name, eventName: event }
        end
      end
    end
  end
  return graph
end

.parse(files_to_parse) ⇒ Object



6
7
8
# File 'lib/flight-control-tower/parser.rb', line 6

def self.parse files_to_parse
  create_graph(create_connections(files_to_parse)).to_json
end