class FactoryTrace::Readers::TraceReader

Attributes

configuration[R]
io[R]

Public Class Methods

new(io, configuration: Configuration.new) click to toggle source
# File lib/factory_trace/readers/trace_reader.rb, line 25
def initialize(io, configuration: Configuration.new)
  @io = io
  @configuration = configuration
end
read_from_files(*file_names, configuration: Configuration.new) click to toggle source

Read the data from files and merge it

@return [Hash<Symbol, FactoryTrace::Structures::Collection>]

# 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

Public Instance Methods

read() click to toggle source

Read the data from file

@return [Hash<Symbol, FactoryTrace::Structures::Collection>]

# 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

Private Instance Methods

parse_collection(hash) click to toggle source
# File lib/factory_trace/readers/trace_reader.rb, line 58
def parse_collection(hash)
  collection = FactoryTrace::Structures::Collection.new

  hash['factories'].each do |h|
    collection.add(parse_factory(h))
  end

  hash['traits'].each do |h|
    collection.add(parse_trait(h))
  end

  collection
end
parse_factory(hash) click to toggle source
# File lib/factory_trace/readers/trace_reader.rb, line 48
def parse_factory(hash)
  FactoryTrace::Structures::Factory.new(
    hash['names'],
    hash['traits'].map(&method(:parse_trait)),
    parent_name: hash['parent_name'],
    declaration_names: hash['declaration_names'],
    definition_path: hash['definition_path']
  )
end
parse_trait(hash) click to toggle source
# File lib/factory_trace/readers/trace_reader.rb, line 44
def parse_trait(hash)
  FactoryTrace::Structures::Trait.new(hash['name'], declaration_names: hash['declaration_names'], definition_path: hash['definition_path'])
end