Sha256: c21538fc1691e92af4aa98b5b3ae1127933183a5d2297aa5c8defd2f594bfb09

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module TraceLocation
  module Generator
    class Markdown < Base # :nodoc:
      def initialize(events, return_value, options)
        super
        @current_dir = ::TraceLocation.config.current_dir
        @dest_dir = options.fetch(:dest_dir) { ::TraceLocation.config.dest_dir }
        @current = Time.now
        @filename = "trace_location-#{@current.strftime('%Y%m%d%H%m%s')}.md"
        @file_path = File.join(@dest_dir, @filename)
      end

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

      private

      attr_reader :events, :return_value, :current_dir, :dest_dir, :current, :filename, :file_path

      def setup_dir
        FileUtils.mkdir_p(dest_dir)
      end

      def create_file
        File.open(file_path, 'wb+') do |io|
          io.write <<~MARKDOWN
            Generated by [trace_location](https://github.com/yhirano55/trace_location) at #{current}

          MARKDOWN

          events.select(&:call?).each do |e|
            path = e.path.to_s.gsub(%r{#{current_dir}/}, '')
            caller_path = e.caller_path.to_s.gsub(%r{#{current_dir}/}, '')

            io.write <<~MARKDOWN
              <details open>
              <summary>#{path}:#{e.lineno}</summary>

              ##### #{e.owner_with_name}

              ```ruby
              #{e.source}
              # called from #{caller_path}:#{e.caller_lineno}
              ```
              </details>
            MARKDOWN
          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/markdown.rb
trace_location-1.1 lib/trace_location/generator/markdown.rb
trace_location-1.0 lib/trace_location/generator/markdown.rb
trace_location-0.12.2 lib/trace_location/generator/markdown.rb
trace_location-0.12.1 lib/trace_location/generator/markdown.rb
trace_location-0.12.0 lib/trace_location/generator/markdown.rb
trace_location-0.11.0 lib/trace_location/generator/markdown.rb
trace_location-0.10.0 lib/trace_location/generator/markdown.rb
trace_location-0.9.6 lib/trace_location/generator/markdown.rb
trace_location-0.9.5 lib/trace_location/generator/markdown.rb
trace_location-0.9.4 lib/trace_location/generator/markdown.rb
trace_location-0.9.3.1 lib/trace_location/generator/markdown.rb