class CabbageDoc::Action

Constants

METHODS
METHODS_REGEXP
SECTION_REGEXP

Attributes

description[R]
examples[R]
label[R]
method[R]
name[R]
parameters[R]
path[R]
visibility[R]

Public Class Methods

new() click to toggle source
# File lib/cabbage_doc/action.rb, line 11
def initialize
  @parameters = []
  @examples = []
  @visibility = VISIBILITY.first
end

Public Instance Methods

param?(key) click to toggle source
# File lib/cabbage_doc/action.rb, line 35
def param?(key)
  @parameters.any? { |parameter| parameter.name =~ /#{key}/ }
end
parse(text, tag = TAG) click to toggle source
# File lib/cabbage_doc/action.rb, line 17
def parse(text, tag = TAG)
  @method, @path = parse_method_and_path(text)
  return unless valid?

  @name = parse_name(text)
  @label = parse_label(text)
  @description = parse_description(text)
  @visibility = parse_visibility(text)

  @parameters, @examples = parse_parameters_and_examples(text)

  valid?
end
valid?() click to toggle source
# File lib/cabbage_doc/action.rb, line 31
def valid?
  @method && @path
end

Private Instance Methods

parse_description(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 74
def parse_description(text)
  m = text.match(/#\s*Description:\s+(.*?)(#\s*(#{SECTION_REGEXP}):|\z)/m)
  text_to_lines(m[1]).join("\n").strip if m
end
parse_label(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 69
def parse_label(text)
  m = text.match(/#\s*(#{VISIBILITY_REGEXP}):(.*?)$/)
  m[2].strip if m
end
parse_method_and_path(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 59
def parse_method_and_path(text)
  m = text.match(/#\s*(#{METHODS_REGEXP}):\s*(.*?)$/)
  [m[1].strip.upcase, m[2].strip] if m
end
parse_name(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 64
def parse_name(text)
  m = text.match(/def\s+(.*?)\s*#\s*#{MARKER}$/)
  m[1].strip if m
end
parse_parameters_and_examples(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 41
def parse_parameters_and_examples(text)
  # FIXME: rewrite this to do a 'scan' with the right Regexp
  parameters = []
  examples = []

  lines = text_to_lines(text).map(&:strip).select { |line| line.size > 0 }

  lines.each do |line|
    if parameter = Parameter.parse(line)
      parameters << parameter
    elsif example = Example.parse(line)
      examples << example
    end
  end

  [parameters, examples]
end
parse_visibility(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 79
def parse_visibility(text)
  m = text.match(/#\s*(#{VISIBILITY_REGEXP}):(.*?)$/)
  if m
    m[1].strip.downcase.to_sym
  else
    VISIBILITY.first
  end
end
text_to_lines(text) click to toggle source
# File lib/cabbage_doc/action.rb, line 88
def text_to_lines(text)
  text.strip.gsub(/\r\n?/, "\n").split("\n").map do |line|
    line.sub!(/^(\s+)?#\s*/, "")
    line if line !~ /#\s+?#{MARKER}$/
  end.compact
end