Class: Liquidbook::ParamParser

Inherits:
Object
  • Object
show all
Defined in:
lib/liquidbook/param_parser.rb

Overview

Extracts @param comments from snippet files to build editable parameter forms

Supported format:

@param {Type} name - description
@param {Type} name [default_value] - description

Constant Summary collapse

PARAM_REGEX =
/@param\s+\{(\w+)\}\s+(\w+)(?:\s+\[([^\]]*)\])?\s*(?:-\s*(.*))?/

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ ParamParser

Returns a new instance of ParamParser.



12
13
14
# File 'lib/liquidbook/param_parser.rb', line 12

def initialize(source)
  @source = source
end

Instance Method Details

#default_assignsObject

Build assigns hash from params with their defaults



28
29
30
31
32
# File 'lib/liquidbook/param_parser.rb', line 28

def default_assigns
  parse.each_with_object({}) do |param, hash|
    hash[param["name"]] = param["default"]
  end
end

#parseObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/liquidbook/param_parser.rb', line 16

def parse
  @source.scan(PARAM_REGEX).map do |type, name, default, description|
    {
      "name" => name,
      "type" => normalize_type(type),
      "default" => coerce_default(type, default),
      "description" => description&.strip
    }
  end
end