Sha256: d612770b15da0197c28e15bebae285087a1ea0534b41c36e861a438cc0e1c760

Contents?: true

Size: 1.48 KB

Versions: 19

Compression:

Stored size: 1.48 KB

Contents

module Lookbook
  class ParamTag < YardTag
    VALUE_TYPE_MATCHER = /^(\[\s?([A-Z]{1}\w+)\s?\])/
    DESCRIPTION_MATCHER = /(?<=\s|^)"(.*[^\\])"(?:\s|$)/

    supports_options

    attr_reader :tag_args

    def initialize(text)
      @name, text_without_name = extract_name(text)
      @tag_args = parse_tag_body(text_without_name)
      super("param", tag_args[:rest], nil, @name)
    end

    def input
      tag_args[:input]&.downcase&.tr("_", "-")
    end

    def description
      tag_args[:description]
    end

    def value_type
      tag_args[:value_type]&.downcase
    end

    def value_default
      default_value_parts = object.parameters.find { |parts| parts[0].chomp(":") == name }
      if default_value_parts
        object.instance_eval(default_value_parts[1])
      else
        raise ParserError.new "Unknown method parameter '#{name}'"
      end
    end

    protected

    def extract_name(text)
      parts = text.to_s.split(" ")
      [parts.shift, parts.join(" ")]
    end

    def parse_tag_body(text)
      value_type = nil
      description = nil

      text.match(VALUE_TYPE_MATCHER) do |match_data|
        value_type = match_data[2]
        text.gsub!(VALUE_TYPE_MATCHER, "").strip!
      end

      text.match(DESCRIPTION_MATCHER) do |match_data|
        description = match_data[1]
        text.gsub!(DESCRIPTION_MATCHER, "").strip!
      end

      input, rest = text.split(" ", 2)

      {input: input, value_type: value_type, description: description, rest: rest}
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
lookbook-1.5.5 lib/lookbook/tags/param_tag.rb
lookbook-1.5.4 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.8 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.7 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.6 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.5 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.4 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.3 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.2 lib/lookbook/tags/param_tag.rb
lookbook-1.5.3 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.1 lib/lookbook/tags/param_tag.rb
lookbook-1.5.2 lib/lookbook/tags/param_tag.rb
lookbook-2.0.0.beta.0 lib/lookbook/tags/param_tag.rb
lookbook-1.5.1 lib/lookbook/tags/param_tag.rb
lookbook-1.5.0 lib/lookbook/tags/param_tag.rb
lookbook-1.4.5 lib/lookbook/tags/param_tag.rb
lookbook-1.4.4 lib/lookbook/tags/param_tag.rb
lookbook-1.4.3 lib/lookbook/tags/param_tag.rb
lookbook-1.4.2 lib/lookbook/tags/param_tag.rb