Class: Lookbook::PageCollection

Inherits:
EntityCollection show all
Includes:
HierarchicalCollection
Defined in:
lib/lookbook/entities/collections/page_collection.rb

Constant Summary collapse

TREE_BUILDER =
PageTreeBuilder

Instance Attribute Summary

Attributes inherited from EntityCollection

#entities

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityCollection

#add, #clear_all, #each, #find_by_id, #find_by_path, #flat_map, #initialize, #next, #previous

Constructor Details

This class inherits a constructor from Lookbook::EntityCollection

Class Method Details

.entity(file_path) ⇒ Object



53
54
55
# File 'lib/lookbook/entities/collections/page_collection.rb', line 53

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

.file_paths(directories) ⇒ Object



47
48
49
50
51
# File 'lib/lookbook/entities/collections/page_collection.rb', line 47

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

Instance Method Details

#load(page_paths, changes = nil) ⇒ Object



7
8
9
10
11
# File 'lib/lookbook/entities/collections/page_collection.rb', line 7

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

#pages_from_paths(file_paths) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lookbook/entities/collections/page_collection.rb', line 33

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

#reload_all(file_paths) ⇒ Object



13
14
15
16
# File 'lib/lookbook/entities/collections/page_collection.rb', line 13

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

#reload_changed(file_paths, changes) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/lookbook/entities/collections/page_collection.rb', line 18

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

#remove_by_file_path(paths) ⇒ Object



27
28
29
30
31
# File 'lib/lookbook/entities/collections/page_collection.rb', line 27

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