Sha256: 67933f947dbd408c173deeb16d97da08bbca1e8079c67134bf07fc007356d025

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Lookbook
  # @api private
  module AnnotatableEntity
    extend ActiveSupport::Concern

    included do
      delegate :has_tag?, to: :code_object

      def notes
        code_object.docstring.to_s.strip
      end

      def tags(name = nil)
        code_object.tags(name)
      end

      def tag(name = nil)
        tags(name).first
      end

      protected

      attr_reader :code_object

      def fetch_config(key, fallback = nil, &block)
        value = case key.to_sym
        when :renders
          renders_config
        when :display_options
          display_options_config
        else
          tag(key).value if has_tag?(key)
        end

        Utils.value_or_fallback(value, fallback, &block)
      end

      private

      def renders_config
        return unless has_tag?(:component) || has_tag?(:renders)

        [*tags(:component), *tags(:renders)].map(&:value).compact
      end

      def display_options_config
        return unless has_tag?(:display)

        # Dynamic params set at the entity level are
        # not (yet?) supported so filter them out.
        display_tags = tags(:display).select do |tag|
          !tag.value.is_a?(Hash) && !tag.value.is_a?(Array)
        end

        display_tags.map { |tag| [tag.key.to_sym, tag.value] }.to_h
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lookbook-2.0.0.beta.3 lib/lookbook/entities/concerns/annotatable_entity.rb
lookbook-2.0.0.beta.2 lib/lookbook/entities/concerns/annotatable_entity.rb
lookbook-2.0.0.beta.1 lib/lookbook/entities/concerns/annotatable_entity.rb
lookbook-2.0.0.beta.0 lib/lookbook/entities/concerns/annotatable_entity.rb