Sha256: b86304af04e114243c702c8b0d6256f462af6265badd9b71898962c1bd2e9c6c

Contents?: true

Size: 1.72 KB

Versions: 25

Compression:

Stored size: 1.72 KB

Contents

module Lookbook
  class CodeInspector
    include Utils

    attr_reader :code_object
    delegate :groups, :source, to: :@code_object, allow_nil: true

    def initialize(taggable_object_path)
      @code_object = Lookbook::Engine.parser.get_code_object(taggable_object_path)
    end

    def hidden?
      if code_object&.tag(:hidden)
        code_object.tag(:hidden).text.strip != "false"
      end
    end

    def id
      if code_object&.tag(:id)&.text&.present?
        generate_id(code_object&.tag(:id)&.text)
      end
    end

    def label
      code_object&.tag(:label)&.text
    end

    def notes
      if code_object&.docstring
        code_object.docstring.to_s.strip
      end
    end

    def group
      code_object&.group
    end

    def position
      code_object&.tag(:position)&.text&.to_i || 10000
    end

    def display_params
      display_params = {}.with_indifferent_access
      if code_object&.tags(:display).present?
        code_object.tags(:display).each do |tag|
          parts = tag.text.strip.match(/^([^\s]*)\s?(.*)$/)
          if parts.present?
            begin
              display_params[parts[1]] = YAML.safe_load(parts[2] || "~")
            rescue SyntaxError => err
              Lookbook.logger.error("\nšŸ‘€ [Lookbook] Invalid JSON in @display tag.\nšŸ‘€ [Lookbook] (#{err})\n")
            end
          end
        end
      end
      display_params
    end

    def parameter_defaults
      code_object&.parameters&.map { |str| Params.parse_method_param_str(str) }&.compact&.to_h
    end

    def params
      code_object&.tags("param")&.map do |param|
        Lookbook::Params.build_param(param, parameter_defaults[param.name])
      end
    end

    def methods
      code_object&.meths
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
lookbook-0.9.8 lib/lookbook/code_inspector.rb
lookbook-0.9.7 lib/lookbook/code_inspector.rb
lookbook-0.9.6 lib/lookbook/code_inspector.rb
lookbook-0.9.5 lib/lookbook/code_inspector.rb
lookbook-0.9.4 lib/lookbook/code_inspector.rb
lookbook-0.9.3 lib/lookbook/code_inspector.rb
lookbook-0.9.2 lib/lookbook/code_inspector.rb
lookbook-0.9.1 lib/lookbook/code_inspector.rb
lookbook-0.9.0 lib/lookbook/code_inspector.rb
lookbook-0.8.3 lib/lookbook/code_inspector.rb
lookbook-0.8.2 lib/lookbook/code_inspector.rb
lookbook-0.8.1 lib/lookbook/code_inspector.rb
lookbook-0.8.0 lib/lookbook/code_inspector.rb
lookbook-0.8.0.beta.0 lib/lookbook/code_inspector.rb
lookbook-0.7.4 lib/lookbook/code_inspector.rb
lookbook-0.7.3 lib/lookbook/code_inspector.rb
lookbook-0.7.2 lib/lookbook/code_inspector.rb
lookbook-0.7.2.beta.5 lib/lookbook/code_inspector.rb
lookbook-0.7.2.beta.4 lib/lookbook/code_inspector.rb
lookbook-0.7.2.beta.3 lib/lookbook/code_inspector.rb