class DuckPuncher::Ducks::Method::Definition

Public Class Methods

new(method_handle) click to toggle source
# File lib/duck_puncher/ducks/method.rb, line 14
def initialize(method_handle)
  @file_path, @line_num = *method_handle.source_location
  @line_num = @line_num.to_i
end

Public Instance Methods

find_indent_size(line) click to toggle source
# File lib/duck_puncher/ducks/method.rb, line 50
def find_indent_size(line)
  line[/(\s*)/].size
end
lines() click to toggle source

@description finds the method's source code using the indent size of the file. This means we are restricted when it comes to parsing crappy formatted ruby files

# File lib/duck_puncher/ducks/method.rb, line 21
def lines
  return @lines if defined? @lines
  return [] unless @file_path
  @lines = []
  File.open(@file_path) do |f|
    found = false
    i = 0
    while line = f.gets and i += 1 and !found
      next if i < @line_num
      @lines << line
      if @indent_size
        found = @indent_size == find_indent_size(line)
      else
        @indent_size = find_indent_size(line)
        found = line.end_with?("end\n")
      end
    end
  end
  @lines
end
to_s() click to toggle source
# File lib/duck_puncher/ducks/method.rb, line 42
def to_s
  if lines.any?
    lines.join.gsub(/^\s{#{find_indent_size(lines.first)}}/, '')
  else
    ''
  end
end