module Minireq::Core::Reader

Constants

HEAD_PARTS
QUOTATION

Public Instance Methods

markdow_header_level(line) click to toggle source
# File lib/minireq/core/reader.rb, line 85
def markdow_header_level(line)
  9.downto(1) { |l| return l if line.start_with?('#' * l) }
  0
end
parse_requirement(text) click to toggle source
# File lib/minireq/core/reader.rb, line 123
def parse_requirement(text)
  h = requirement_text_to_hash(text)

  attributes = {}
  attributes.merge! excerpt_to_hash(h[:excerpt]) if h[:excerpt]
  attributes[:title] = h[:title]
  attributes[:body] = h[:body]

  while attributes[:body].start_with?("\n")
    attributes[:body] = attributes[:body][1..-1]
  end

  Requirement.new(h[:id], attributes)
end
read_file(file_name) click to toggle source

@param file_name [String] requirements file @return [Array<Requirement>] from file_name TODO exception handing - erorr header

# File lib/minireq/core/reader.rb, line 27
def read_file(file_name)
  reqary = []
  header = ''
  paragraph = ''
  quotation = false

  File.foreach(file_name) do |line|
    if line.start_with?('#') && !quotation
      unless header.empty?
        reqary << header + paragraph
        paragraph = ''
      end
      header = line
    else
      paragraph << line
      quotation = !quotation if line.start_with?('```')
    end
  end

  # TODO refactor this - move into new method
  reqary << header + paragraph
  root = []
  reqary.each do |req_text|
    begin
      req = parse_requirement(req_text)
      lev = markdow_header_level(req_text)

      case lev
      when 1 then root << req
      when 2
        root.last.items << req
        req.parent = root.last
      when 3
        root.last.items.last.items << req
        req.parent = root.last.items.last
      # FIXME raise exception unless one header found
      else
        raise MinireqError, "#{file_name}. Maximum 3 level of hierarhy supported!"
      end
    rescue Exception => e
      msg = ""
      unless req.nil?
        msg << "An error occurred during reading [#{req.id}] in '#{file_name}'\n"
        msg << "[#{req.id}] placed to root of requirements. Fix errors in '#{file_name}' and try again."
        root << req
      else
        msg << "An error occurred during reading file '#{file_name}'.\n"
        msg << "You maybe missed the [id] in header.\n"
        msg << "Fix errors in '#{file_name}' and try again.\n"
      end
      msg << e.message
      msg << "\n\n"
      puts msg
    end
  end
  root
end
read_requirement_files() click to toggle source

@return [Hash<FileName, Array<Requirements>] TODO excepiton handing file with errors

# File lib/minireq/core/reader.rb, line 18
def read_requirement_files
  items = {}
  Dir.glob('**/*.md') {|f| items[f] = read_file(f)}
  items
end
requirement_text_to_hash(text) click to toggle source
# File lib/minireq/core/reader.rb, line 90
def requirement_text_to_hash(text)
  result = {}
  lines = text.split("\n")

  # TODO уйти от "всегда должен быть ID" для заголовков 1-го уровня?
  # TODO встроенный маркдаун в цитатах ... все-таки нужен парсер?
  #                                        или скипаем `, ```
  # header
  if lines.first.start_with?('#')
    header = lines.first
    lines.shift
    head = /^\#* \[(.*)\] (.*)$/.match(header)
    result[:id] = head[1]
    result[:title] = head[2]
  end

  # excerpt
  if lines.first && lines.first.start_with?(LEXCERPT)
    excerpt = ''
    lines.shift
    while !lines.first.start_with?(REXCERPT)
      excerpt << lines.first + "\n"
      lines.shift
    end
    lines.shift
    result[:excerpt] = excerpt
  end

  # body
  result[:body] = lines.join("\n")
  result
end