Class: Lookbook::PageCollection
Instance Attribute Summary
#entities
Class Method Summary
collapse
Instance Method Summary
collapse
#add, #clear_all, #each, #find_by_id, #find_by_path, #flat_map, #initialize, #next, #previous
Class Method Details
.entity(file_path) ⇒ Object
51
52
53
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 51
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
45
46
47
48
49
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 45
def self.file_paths(directories)
directories.flat_map do |dir|
PathUtils.normalize_paths(Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*"].sort)
end
end
|
Instance Method Details
#load(page_paths, changes = nil) ⇒ Object
5
6
7
8
9
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 5
def load(page_paths, changes = nil)
file_paths = PageCollection.file_paths(page_paths)
reload_all(file_paths) end
|
#pages_from_paths(file_paths) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 31
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
11
12
13
14
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 11
def reload_all(file_paths)
clear_all
add(pages_from_paths(file_paths))
end
|
#reload_changed(file_paths, changes) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 16
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
25
26
27
28
29
|
# File 'lib/lookbook/entities/collections/page_collection.rb', line 25
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
|