class GetComments::Code

Attributes

code[R]
comment_index[R]

Public Class Methods

new(code) click to toggle source
# File lib/getcomments/code.rb, line 5
def initialize(code)
  @code = code
  @comment_index = 0
end

Public Instance Methods

comments() click to toggle source
# File lib/getcomments/code.rb, line 10
def comments
  @comments ||= comments!
end

Private Instance Methods

comments!() click to toggle source
# File lib/getcomments/code.rb, line 16
def comments!
  last_comment = []
  result = {}

  lines.each do |line|
    line.strip!

    if line[0] == '#'
      last_comment << line.gsub(/^# ?/, '')
    elsif !last_comment.empty?
      result[get_key line] = last_comment.join("\n")
      last_comment = []
    end
  end

  unless last_comment.empty?
    result[get_key] = last_comment.join("\n")
  end

  result
end
get_key(line='') click to toggle source
# File lib/getcomments/code.rb, line 42
def get_key(line='')
  if match = line.match(/^([\w_]+ [\w\:]+)/)
    match.captures.first
  else
    @comment_index += 1
    "comment_#{comment_index}"
  end
end
lines() click to toggle source
# File lib/getcomments/code.rb, line 38
def lines
  @lines ||= code.lines
end