Class: FactoryTrace::Readers::TraceReader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TraceReader.



25
26
27
28
# File 'lib/factory_trace/readers/trace_reader.rb', line 25

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

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



4
5
6
# File 'lib/factory_trace/readers/trace_reader.rb', line 4

def configuration
  @configuration
end

#ioObject (readonly)

Returns the value of attribute io.



4
5
6
# File 'lib/factory_trace/readers/trace_reader.rb', line 4

def io
  @io
end

Class Method Details

.read_from_files(*file_names, configuration: Configuration.new) ⇒ Hash<Symbol, FactoryTrace::Structures::Collection>

Read the data from files and merge it

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/factory_trace/readers/trace_reader.rb', line 9

def self.read_from_files(*file_names, configuration: Configuration.new)
  result = {defined: FactoryTrace::Structures::Collection.new, used: FactoryTrace::Structures::Collection.new}

  file_names.each do |file_name|
    File.open(file_name, 'r') do |file|
      data = new(file, configuration: configuration).read

      [:defined, :used].each do |key|
        result[key].merge!(data[key])
      end
    end
  end

  result
end

Instance Method Details

#readHash<Symbol, FactoryTrace::Structures::Collection>

Read the data from file

Returns:



33
34
35
36
37
38
39
40
# File 'lib/factory_trace/readers/trace_reader.rb', line 33

def read
  hash = JSON.parse(io.read)

  {
    defined: parse_collection(hash['defined']),
    used: parse_collection(hash['used'])
  }
end