Sha256: 3ba54cebbadd4eb090d310dcd5464d22cc8205c71dd0dc9c408000dc7ea883de

Contents?: true

Size: 1.31 KB

Versions: 12

Compression:

Stored size: 1.31 KB

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 owner_with_name hierarchy].freeze

      def initialize(events, return_value, options)
        super
        @current_dir = ::TraceLocation.config.current_dir
        @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, :current_dir, :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 << [
              event.id,
              event.event,
              event.path.to_s.gsub(%r{#{current_dir}/}, ''),
              event.lineno,
              event.caller_path.to_s.gsub(%r{#{current_dir}/}, ''),
              event.caller_lineno,
              event.owner_with_name,
              event.hierarchy
            ]
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
trace_location-1.2.0 lib/trace_location/generator/csv.rb
trace_location-1.1 lib/trace_location/generator/csv.rb
trace_location-1.0 lib/trace_location/generator/csv.rb
trace_location-0.12.2 lib/trace_location/generator/csv.rb
trace_location-0.12.1 lib/trace_location/generator/csv.rb
trace_location-0.12.0 lib/trace_location/generator/csv.rb
trace_location-0.11.0 lib/trace_location/generator/csv.rb
trace_location-0.10.0 lib/trace_location/generator/csv.rb
trace_location-0.9.6 lib/trace_location/generator/csv.rb
trace_location-0.9.5 lib/trace_location/generator/csv.rb
trace_location-0.9.4 lib/trace_location/generator/csv.rb
trace_location-0.9.3.1 lib/trace_location/generator/csv.rb