Module: Simple::Service::Action::Comment::Extractor

Extended by:
Extractor
Included in:
Extractor
Defined in:
lib/simple/service/action/comment.rb

Instance Method Summary collapse

Instance Method Details

#_parse_source(file) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/simple/service/action/comment.rb', line 27

def _parse_source(file)
  File.readlines(file).map do |line|
    case line
    when /^\s*# ?(.*)$/ then $1
    when /^\s*end/ then :end
    end
  end
end

#extract_comment_lines(file:, before_line:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple/service/action/comment.rb', line 36

def extract_comment_lines(file:, before_line:)
  parsed_source = parse_source(file)

  # go down from before_line until we see a line which is either a comment
  # or an :end. Note that the line at before_line-1 should be the first
  # line of the method definition in question.
  last_line = before_line - 1
  last_line -= 1 while last_line >= 0 && !parsed_source[last_line]

  first_line = last_line
  first_line -= 1 while first_line >= 0 && parsed_source[first_line]
  first_line += 1

  comments = parsed_source[first_line..last_line]
  if comments.include?(:end)
    []
  else
    parsed_source[first_line..last_line]
  end
end

#parse_source(file) ⇒ Object

reads the source a file and turns each non-comment into :code and each comment into a string without the leading comment markup.



22
23
24
25
# File 'lib/simple/service/action/comment.rb', line 22

def parse_source(file)
  @parsed_sources ||= {}
  @parsed_sources[file] = _parse_source(file)
end