class Docks::Tags::Param

Constants

NAME_MATCHER
PROP_MATCHER

Public Class Methods

new() click to toggle source
# File lib/docks/tags/param_tag.rb, line 4
def initialize
  @name = :param
  @synonyms = [:arg, :argument, :parameter]
  @multiple_allowed = true
end

Public Instance Methods

process(symbol) click to toggle source
# File lib/docks/tags/param_tag.rb, line 10
def process(symbol)
  symbol.update(@name) do |params|
    params = Array(params).map do |param|
      param = multiline_description(param) { |first_line| proccess_first_line_of_param(first_line) }
      OpenStruct.new(param)
    end

    join_sub_params(params)
  end
end

Private Instance Methods

join_sub_params(params) click to toggle source
# File lib/docks/tags/param_tag.rb, line 64
def join_sub_params(params)
  final_params = []

  params.each do |param|
    param.properties ||= []

    if matches = param.name.match(PROP_MATCHER)
      base_param_name, param.name = matches[:base], matches[:name]
      base_param = final_params.find { |final_param| final_param.name == base_param_name }

      if base_param.nil?
        base_param = OpenStruct.new({
          name: base_param_name,
          types: ["Object"],
          properties: []
        })

        final_params << base_param
      end

      base_param.properties << param
    else
      final_params << param
    end
  end

  final_params
end
proccess_first_line_of_param(first_line) click to toggle source
# File lib/docks/tags/param_tag.rb, line 26
def proccess_first_line_of_param(first_line)
  first_line = first_line.strip
  result = {
    multiple: false,
    types: Array.new,
    optional: false,
    description: nil
  }

  if type_match = first_line.match(/^\{([^\}]*)\}\s*/)
    result[:types] = split_types(type_match.captures.first)
    first_line = first_line.sub(type_match.to_s, "")
  end

  first_line.sub!(/([^=\s])\[\]/, '\1')
  name_match = first_line.match(NAME_MATCHER)
  result[:optional] = !name_match[:optional].nil?
  result[:name] = name_match[:name]
  first_line = first_line.sub(name_match.to_s, "")

  result[:name].sub!(/^\.{3}/) do
    result[:multiple] = true
    ""
  end

  description_match = first_line.match(/\s*\-\s*(.*)/)
  result[:description] = description_match.nil? ? nil : description_match.captures.first
  first_line.sub!(description_match.to_s, "") unless description_match.nil?

  default_match = first_line.match(/(.*)\]\s*/)
  result[:default] = default_match.nil? ? nil : default_match.captures.first
  first_line = first_line.sub(default_match.to_s, "") unless default_match.nil?

  result[:description] = first_line if result[:description].nil? && !first_line.empty?

  result
end