Sha256: ed6f2bc2e0619450545a4ae053207f715313314826438932ecded6f5676c075e

Contents?: true

Size: 988 Bytes

Versions: 2

Compression:

Stored size: 988 Bytes

Contents

# frozen_string_literal: true

require 'csv'

module TraceLocation
  module Generator
    class Csv < Base # :nodoc:
      ATTRIBUTES = %w[id event path lineno caller_path caller_lineno method_str hierarchy].freeze

      def initialize(events, return_value, options)
        super
        @dest_dir = options.fetch(:dest_dir) { ::TraceLocation.config.dest_dir }
        @file_path = File.join(@dest_dir, "trace_location-#{Time.now.strftime('%Y%m%d%H%m%s')}.csv")
      end

      def generate
        setup_dir
        create_file
        $stdout.puts "Created at #{file_path}"
      end

      private

      attr_reader :events, :return_value, :dest_dir, :file_path

      def setup_dir
        FileUtils.mkdir_p(dest_dir)
      end

      def create_file
        CSV.open(file_path, 'wb+') do |csv|
          csv << ATTRIBUTES

          events.each do |event|
            csv << ATTRIBUTES.map { |attr| event.public_send(attr) }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trace_location-0.9.1 lib/trace_location/generator/csv.rb
trace_location-0.9.0 lib/trace_location/generator/csv.rb