Sha256: c0544d6e98ab2feaaa7cecc04c8ed31b1065783b524d713ee78e036129c97c7e

Contents?: true

Size: 1.65 KB

Versions: 14

Compression:

Stored size: 1.65 KB

Contents

module Lookbook
  class PageCollection < EntityCollection
    include HierarchicalCollection

    TREE_BUILDER = PageTreeBuilder

    def load(page_paths, changes = nil)
      file_paths = PageCollection.file_paths(page_paths)
      reload_all(file_paths) # TODO: Fix incremental reloading
      # changes.present? ? reload_changed(file_paths, changes) : reload_all(file_paths)
    end

    def reload_all(file_paths)
      clear_all
      add(pages_from_paths(file_paths))
    end

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

      remove_by_file_path(removed)
      add(pages_from_paths(added))
    end

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

    def pages_from_paths(file_paths)
      entities = file_paths.map { |path| PageCollection.entity(path) }
      pages, sections = entities.partition { |page| page.type == :page }

      page_dict = pages.index_by(&:lookup_path)
      sections.each do |section|
        parent = page_dict[section.lookup_path]
        section.parent = parent
        parent.add_section(section)
      end

      pages
    end

    def self.file_paths(directories)
      directories.flat_map do |dir|
        PathUtils.normalize_paths(Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*", "#{dir}/**/*.md"].sort)
      end
    end

    def self.entity(file_path)
      File.basename(file_path).match?(%r{\[(.*?\w+)\]}) ? PageSectionEntity.new(file_path) : PageEntity.new(file_path)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.3.3 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.3.2 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.3.0 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.2.2 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.2.1 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.2.0 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.1.1 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.1.0 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.0.5 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.0.4 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.0.3 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.0.2 lib/lookbook/entities/collections/page_collection.rb
lookbook-2.0.1 lib/lookbook/entities/collections/page_collection.rb