Sha256: bfab741814735bbfb9d1ad361fb207e7b571dad6ac1dcd0e67ed181a401ba271

Contents?: true

Size: 1.9 KB

Versions: 10

Compression:

Stored size: 1.9 KB

Contents

module Lookbook
  class PreviewCollection < EntityCollection
    include HierarchicalCollection

    def find_scenario_by_path(lookup_path)
      scenarios.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, changes = nil)
      changes.present? ? reload_changed(code_objects, changes) : reload_all(code_objects)
    end

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

    def reload_changed(code_objects, changes)
      modified = Array(changes[:modified])
      removed = Array(changes[:removed]) + modified
      added = Array(changes[:added]) + modified

      remove_by_file_path(removed)

      previews = added.map do |path|
        code_object = code_objects.find { |obj| obj if obj&.file.to_s == path.to_s }
        PreviewCollection.preview_from_code_object(code_object) if code_object
      end.compact

      add(previews)
    end

    def remove_by_file_path(paths)
      paths = Array(paths).map(&:to_s)
      @entities.reject! { |preview| preview.file_path.to_s.in?(paths) }
      clear_cache
    end

    class << self
      def preview_from_code_object(code_object)
        klass = code_object.path.constantize
        PreviewEntity.new(code_object) if preview_class?(klass)
      rescue => exception
        Lookbook.logger.error exception.to_s
        nil
      end

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

    protected

    def scenarios
      @_cache[:scenarios] ||= ScenarioCollection.new(flat_map(&:scenarios))
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lookbook-2.0.0.beta.9 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.8 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.7 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.6 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.5 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.4 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.3 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.2 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.1 lib/lookbook/entities/collections/preview_collection.rb
lookbook-2.0.0.beta.0 lib/lookbook/entities/collections/preview_collection.rb