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.



27
28
29
30
# File 'lib/factory_trace/readers/trace_reader.rb', line 27

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

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/factory_trace/readers/trace_reader.rb', line 6

def configuration
  @configuration
end

#ioObject (readonly)

Returns the value of attribute io.



6
7
8
# File 'lib/factory_trace/readers/trace_reader.rb', line 6

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:



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

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:



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

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

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