Module: InlineTesting::CommentParser

Defined in:
lib/inline_testing/comment_parser.rb

Class Method Summary collapse

Class Method Details

.parse(&blk) ⇒ Array

Returns an Array of Hash objects.

Examples:

Parse some comments

cp = CommentParser.parse do
  # ignored
  1 != 2 # One obviously isn't two. # ignored
end

p cp.first 

outputs:

[
  {
    :code => "1 != 2",
    :comment => "One obviously isn't two."
  }
]

Returns:

  • (Array)

    an Array of Hash objects.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/inline_testing/comment_parser.rb', line 22

def parse(&blk)
  blk.to_raw_source(:strip_enclosure => true).lines.collect { |str|
    # Delete lines that only consist of comments.... like this one! Irony.
    str.strip.gsub(/^#.*$/, "")
  }.delete_if { |str| str.empty? }.collect { |str|
    str.strip.match(/^\s*([^\s#][^#\n\r]*)(?:#([^#\n\r]*))?/).captures.collect { |str|
      str.strip if str.respond_to?(:strip) # Try getting it drunk first
    }
  }.inject([]) { |result, ary|
    result << { :code => ary.first, :comment => ary.last }
    result # Needed? comment this line..
  }
end