Class: CommentScanner
- Inherits:
-
Object
- Object
- CommentScanner
- 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
-
#skip_pattern ⇒ Object
readonly
Returns the value of attribute skip_pattern.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#after(index) ⇒ Object
Detect comment blocks immediately after
index
. -
#before(index) ⇒ Object
Detect comment blocks immediately before
index
. -
#initialize(source, options = {}) ⇒ CommentScanner
constructor
Creates a new CommentScanner for
source
.
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, ={}) @source = source.is_a?(Array) ? source : source.each_line.to_a @skip_pattern = [:skip] end |
Instance Attribute Details
#skip_pattern ⇒ Object (readonly)
Returns the value of attribute skip_pattern.
7 8 9 |
# File 'lib/comment_scanner.rb', line 7 def skip_pattern @skip_pattern end |
#source ⇒ Object (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 |