Sha256: d85cd93afb53108fcb9b1fa92ae934e71267d790b65193201a449eff3a62b730

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module Lookbook
  class YardTag < ::YARD::Tags::Tag
    def initialize(*args)
      if args.size < 2
        tag_name = self.class.name.demodulize.underscore.chomp("_tag")
        super(tag_name, args.first, nil, @name)
      else
        super(*args)
      end
    end

    def text
      tag_parts[:text]
    end

    def options
      tag_parts[:options]
    end

    def to_s
      value.to_s
    end

    # The `value` attribute should be overriden in child classes
    # to return the resolved value of the tag, where appropriate.
    # i.e. for the @hidden tag this would return boolean true/false.
    alias_method :value, :text

    protected

    def tag_parts
      if @tag_parts.nil?
        options, text = TagOptionsParser.call(@text, {
          file: host_file,
          base_dir: (File.dirname(host_file) if host_file),
          eval_context: host_class_instance,
          permit_eval: Lookbook.config.preview_params_options_eval
        })
      end
      @tag_parts ||= {options: options, text: text}
    end

    def host_file
      location = object&.files&.first # [file, line_number]
      location&.first
    end

    def host_class_instance
      host_class = lookup_host_class
      host_class&.new
    end

    def lookup_host_class
      host_code_object = if object.is_a?(YARD::CodeObjects::MethodObject)
        object.parent
      elsif object.is_a?(YARD::CodeObjects::ClassObject)
        object
      end
      host_code_object&.path&.constantize
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lookbook-1.3.4 lib/lookbook/tags/yard_tag.rb
lookbook-1.3.3 lib/lookbook/tags/yard_tag.rb
lookbook-1.3.1 lib/lookbook/tags/yard_tag.rb
lookbook-1.3.0 lib/lookbook/tags/yard_tag.rb