class RSpec::Apib::CommentsParser

Attributes

example[R]

Public Class Methods

new(example) click to toggle source
# File lib/rspec/apib/comments_parser.rb, line 6
def initialize(example)
  @example = example
end

Public Instance Methods

description(namespace = nil) click to toggle source
# File lib/rspec/apib/comments_parser.rb, line 28
def description(namespace = nil)
  matcher = start_matcher(namespace)
  comment = full_comment()
  in_comment = false
  return nil if comment.blank?

  comment.select do |elem|
    if elem == matcher
      in_comment = true
    elsif elem.match?(/\A---($|[^-])/)
      in_comment = false
    end

    in_comment && elem != matcher
  end.join("\n")
end
full_comment() click to toggle source
# File lib/rspec/apib/comments_parser.rb, line 10
def full_comment
  line = example.metadata[:line_number]
  return if line.nil? || line <= 0

  lines = read_example_file
  return if lines.count < line

  i = line -2
  result = []

  while (i >= 0 && match = lines[i].match(/\A\s*#\s(.*)/)) do
    result.unshift(match[1])
    i -= 1
  end

  result
end

Private Instance Methods

read_example_file() click to toggle source
# File lib/rspec/apib/comments_parser.rb, line 47
def read_example_file
  file = example.metadata[:absolute_file_path]
  return [] if file.nil? || file.empty?
  return [] unless File.exists?(file)
  IO.readlines(file)
end
start_matcher(namespace) click to toggle source
# File lib/rspec/apib/comments_parser.rb, line 54
def start_matcher(namespace)
  return "--- apib" if namespace.blank?
  "--- apib:#{namespace}"
end