Class: FactoryTrace::TraceReader

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_trace/readers/trace_reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, config: Configuration.new) ⇒ TraceReader

Returns a new instance of TraceReader.



20
21
22
23
# File 'lib/factory_trace/readers/trace_reader.rb', line 20

def initialize(io, config: Configuration.new)
  @io = io
  @config = config
end

Class Method Details

.read_from_files(*file_names, config: Configuration.new) ⇒ Array<Hash>

Read the data from files and merge it First hash - all factories Second hash - used factories

Returns:



10
11
12
13
14
15
16
17
18
# File 'lib/factory_trace/readers/trace_reader.rb', line 10

def self.read_from_files(*file_names, config: Configuration.new)
  file_names.reduce([{}, {}]) do |array, file_name|
    reader = new(File.open(file_name, 'r'), config: config)
    new = reader.read
    array.each_with_index.map do |hash, index|
      hash.merge(new[index]) { |_key, v1, v2| v1 | v2 }
    end
  end
end

Instance Method Details

#readArray<Hash>

Read the data from file First hash - all factories Second hash - used factories

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/factory_trace/readers/trace_reader.rb', line 30

def read
  data = [{}, {}]
  point = -1

  io.each_line do |line|
    next point += 1 if line.match?(/-all-|-used-/)

    factory, *traits = line.strip.split(',')

    if factory
      data[point][factory] ||= Set.new
      data[point][factory] |= traits
    end
  end

  data
end