Class: DuckPuncher::Ducks::Method::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/duck_puncher/ducks/method.rb

Instance Method Summary collapse

Constructor Details

#initialize(method_handle) ⇒ Definition

Returns a new instance of Definition.



14
15
16
17
# 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

Instance Method Details

#find_indent_size(line) ⇒ Object



50
51
52
# File 'lib/duck_puncher/ducks/method.rb', line 50

def find_indent_size(line)
  line[/(\s*)/].size
end

#linesObject

restricted when it comes to parsing crappy formatted ruby files



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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_sObject



42
43
44
45
46
47
48
# 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