class CabbageDoc::Parameter

Constants

TYPES

Attributes

default[R]
label[R]
name[R]
required[R]
type[R]
type_label[R]
values[R]

Public Class Methods

new() click to toggle source
# File lib/cabbage_doc/parameter.rb, line 9
def initialize
  @values = []
end

Public Instance Methods

parse(text, tag = TAG) click to toggle source
# File lib/cabbage_doc/parameter.rb, line 13
def parse(text, tag = TAG)
  m = text.match(/^(.*?\s+\(.*?\).*?)\s+-\s+(.*?)$/)
  return false unless m

  @name, @type_label, @required = parse_name_type_required(m[1].strip)
  @type = @type_label.downcase.to_sym if @type_label

  @required = !!@required

  @label, @default, @values = parse_label_default_values(m[2].strip)
  @values ||= []

  valid?
end
valid?() click to toggle source
# File lib/cabbage_doc/parameter.rb, line 28
def valid?
  @type && TYPES.include?(@type)
end

Private Instance Methods

parse_label_default_values(text) click to toggle source
# File lib/cabbage_doc/parameter.rb, line 34
def parse_label_default_values(text)
  m = text.match(/^(.*?)\s*(\(.*?\))?$/)
  return unless m

  index, options = parse_options(text)

  arr = [text[0..index-1].strip]

  if options.any?
    arr << options[:default]
    arr << Array(options[:values])
  end

  arr
end
parse_name_type_required(text) click to toggle source
# File lib/cabbage_doc/parameter.rb, line 50
def parse_name_type_required(text)
  text.split(/\s+/).map(&:strip).map do |component|
    if component =~ /^\((.*?)\)$/
      $1
    elsif component =~ /\[required\]/i
      true
    else
      component
    end
  end
end
parse_options(text) click to toggle source
# File lib/cabbage_doc/parameter.rb, line 62
def parse_options(text)
  m = text.match(/:.*?\)$/)
  return [0, {}] unless m

  index = text.rindex('(')
  return [0, {}] unless index
  
  [index, parse_option(text[index..-1].strip)]
end