Class: CommentScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/comment_scanner.rb,
lib/comment_scanner/version.rb

Overview

CommentScanner will scan for comment blocks in Ruby source.

Constant Summary collapse

COMMENT_REGEXP =
/^\s*#/
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ CommentScanner

Creates a new CommentScanner for source.

Options:

:skip

Skip lines matching this pattern before searching for comments.



16
17
18
19
# File 'lib/comment_scanner.rb', line 16

def initialize(source, options={})
  @source = source.is_a?(Array) ? source : source.each_line.to_a
  @skip_pattern = options[:skip]
end

Instance Attribute Details

#skip_patternObject (readonly)

Returns the value of attribute skip_pattern.



7
8
9
# File 'lib/comment_scanner.rb', line 7

def skip_pattern
  @skip_pattern
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/comment_scanner.rb', line 6

def source
  @source
end

Instance Method Details

#after(index) ⇒ Object

Detect comment blocks immediately after index. This is generally less useful, you’re probably looking for CommentScanner#before.

Note that index is 0 indexed, where as an editor or stack trace is typically starts at 1.

Returns nil if there is no match.



41
42
43
44
45
46
# File 'lib/comment_scanner.rb', line 41

def after(index)
  lines = source[index+1..-1]
  matches = match_comments(lines)
  
  strip_lines(matches).join.chomp unless matches.empty?
end

#before(index) ⇒ Object

Detect comment blocks immediately before index.

Note that index is 0 indexed, where as an editor or stack trace is typically starts at 1.

# Returns nil if there is no match.



27
28
29
30
31
32
# File 'lib/comment_scanner.rb', line 27

def before(index)
  lines = source[0...index].reverse
  matches = match_comments(lines)
  
  strip_lines(matches).reverse.join.chomp unless matches.empty?
end