Sha256: b949fbaa330298da9f5133309f00955609348804fe86cc4c06cbfb55059d229b

Contents?: true

Size: 911 Bytes

Versions: 2

Compression:

Stored size: 911 Bytes

Contents

module Lookbook
  class PreviewCollection < EntityCollection
    include HierarchicalCollection

    def find_example_by_path(lookup_path)
      examples.find_by_path(lookup_path)
    end

    def find_by_preview_class(klass)
      find { |preview| preview.preview_class.name == klass.to_s }
    end

    def load(code_objects)
      @entities = []
      clear_cache

      previews = Array(code_objects).map { |obj| PreviewCollection.preview_from_code_object(obj) }.compact
      add(previews)
    end

    def self.preview_from_code_object(code_object)
      klass = code_object.path.constantize
      Preview.new(code_object) if klass.ancestors.include?(ViewComponent::Preview)
    rescue => exception
      Lookbook.logger.error LookbookError.new(exception)
      nil
    end

    protected

    def examples
      @_cache[:examples] ||= PreviewExampleCollection.new(flat_map(&:examples))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lookbook-1.4.1 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.4.0 lib/lookbook/entities/collections/preview_collection.rb