module ProcParser

module to get parsed proc as string the codes in this file are based on secret-garden.hatenablog.com/entry/2019/09/08/145037 by @osyo-manga

Constants

ELEMENTS

Public Instance Methods

body() click to toggle source
# File lib/factory_strategist/proc_parser.rb, line 32
def body # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  path, (start_lnum, start_col, end_lnum, end_col) = code_location

  raise "Unsupported file" if path.nil?

  File.readlines(path).then do |lines|
    start_line = lines[start_lnum - 1]
    end_line = lines[end_lnum - 1]
    if start_lnum == end_lnum
      start_line[(start_col + 1)...(end_col - 1)]
    elsif end_lnum - start_lnum == 1
      start_line[(start_col + 1)...] + end_line[...(end_col - 1)]
    else
      lines[start_lnum...(end_lnum - 1)].join
    end
  end
end
code_location() click to toggle source
# File lib/factory_strategist/proc_parser.rb, line 50
def code_location
  RubyVM::InstructionSequence.of(self).to_h
                             .then { |iseq| [iseq[:absolute_path], iseq.dig(:misc, :code_location)] }
end
to_h() click to toggle source
# File lib/factory_strategist/proc_parser.rb, line 24
def to_h
  ELEMENTS.zip(to_a).to_h
end