Sha256: 5f63cc26ed57fcc90bafaa8c3bc91eada6b403939891d91d637e17233b1248ab

Contents?: true

Size: 1.03 KB

Versions: 10

Compression:

Stored size: 1.03 KB

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 preview_class?(klass)
    rescue => exception
      Lookbook.logger.error exception.to_s
      nil
    end

    def self.preview_class?(klass)
      if klass.ancestors.include?(ViewComponent::Preview)
        !klass.respond_to?(:abstract_class) || klass.abstract_class != true
      end
    end

    protected

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lookbook-1.5.5 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.5.4 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.5.3 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.5.2 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.5.1 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.5.0 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.4.5 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.4.4 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.4.3 lib/lookbook/entities/collections/preview_collection.rb
lookbook-1.4.2 lib/lookbook/entities/collections/preview_collection.rb