Class: Lookbook::Collection
- Inherits:
-
Object
- Object
- Lookbook::Collection
show all
- Includes:
- Enumerable, Utils
- Defined in:
- lib/lookbook/collection.rb
Constant Summary
Constants included
from Utils
Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path = "", items = []) ⇒ Collection
Returns a new instance of Collection.
9
10
11
12
13
14
15
16
17
|
# File 'lib/lookbook/collection.rb', line 9
def initialize(path = "", items = [])
if path.is_a?(Array)
@items = path
path = ""
else
@items = items
end
@path = path.delete_prefix("/").delete_suffix("/")
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
6
7
8
|
# File 'lib/lookbook/collection.rb', line 6
def path
@path
end
|
Class Method Details
.describe_as ⇒ Object
148
149
150
|
# File 'lib/lookbook/collection.rb', line 148
def describe_as
"items"
end
|
Instance Method Details
#add(item) ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/lookbook/collection.rb', line 51
def add(item)
@ordered_entities = nil
@tree = nil
if item.is_a?(String)
item = Collection.new([@path, item].join("/"))
end
@items << item
item
end
|
#as_tree(filter_hidden: true) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/lookbook/collection.rb', line 114
def as_tree(filter_hidden: true)
return self if hierarchy_depth > 0
return @tree if @tree.present?
@tree = self.class.new
candidates = filter_hidden ? visible_items : items
candidates.each do |item|
current = @tree
if item.hierarchy_depth == 1
current.add(item)
else
item.parent_collections_names.each.with_index(1) do |name, i|
target = current.get_or_create(name)
if item.hierarchy_depth == i + 1
target.add(item)
else
current = target
end
end
end
end
@tree
end
|
#find(lookup = nil, &block) ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/lookbook/collection.rb', line 70
def find(lookup = nil, &block)
if lookup
lookup.is_a?(Symbol) ? find_by_id(lookup.to_s.tr("_", "-")) : find_by_path(lookup)
elsif block
items.find(&block)
end
end
|
#find_by_id(id) ⇒ Object
78
79
80
|
# File 'lib/lookbook/collection.rb', line 78
def find_by_id(id)
items.find { |i| i.id == id }
end
|
#find_by_path(path) ⇒ Object
82
83
84
|
# File 'lib/lookbook/collection.rb', line 82
def find_by_path(path)
items.find { |i| i.lookup_path == path }
end
|
#find_first ⇒ Object
100
101
102
|
# File 'lib/lookbook/collection.rb', line 100
def find_first
ordered_entities.first
end
|
#find_next(item) ⇒ Object
104
105
106
107
|
# File 'lib/lookbook/collection.rb', line 104
def find_next(item)
index = ordered_entities.find_index { |i| i.lookup_path == item.lookup_path }
ordered_entities[index + 1] unless index.nil?
end
|
#find_parent(child) ⇒ Object
86
87
88
89
|
# File 'lib/lookbook/collection.rb', line 86
def find_parent(child)
parent_path = child.lookup_path.split("/").pop.join("/")
find_by_path(parent_path)
end
|
#find_previous(item) ⇒ Object
109
110
111
112
|
# File 'lib/lookbook/collection.rb', line 109
def find_previous(item)
index = ordered_entities.find_index { |i| i.lookup_path == item.lookup_path }
ordered_entities[index - 1] if !index.nil? && index > 0
end
|
#get(name) ⇒ Object
61
62
63
64
|
# File 'lib/lookbook/collection.rb', line 61
def get(name)
name = name.underscore
items.find { |item| item.name.underscore == name }
end
|
#get_or_create(name) ⇒ Object
66
67
68
|
# File 'lib/lookbook/collection.rb', line 66
def get_or_create(name)
get(remove_position_prefix(name)).presence || add(name)
end
|
#hierarchy_depth ⇒ Object
39
40
41
|
# File 'lib/lookbook/collection.rb', line 39
def hierarchy_depth
@path ? @path.split("/").size : 0
end
|
#id ⇒ Object
19
20
21
|
# File 'lib/lookbook/collection.rb', line 19
def id
generate_id(lookup_path || "root")
end
|
#items ⇒ Object
43
44
45
|
# File 'lib/lookbook/collection.rb', line 43
def items
@items.sort_by { |item| [item.hierarchy_depth, item.position, item.label] }
end
|
#label ⇒ Object
27
28
29
|
# File 'lib/lookbook/collection.rb', line 27
def label
name&.titleize
end
|
#lookup_path ⇒ Object
31
32
33
|
# File 'lib/lookbook/collection.rb', line 31
def lookup_path
@lookup_path ||= to_lookup_path(@path)
end
|
#name ⇒ Object
23
24
25
|
# File 'lib/lookbook/collection.rb', line 23
def name
@name ||= remove_position_prefix(basename)
end
|
#ordered_entities ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/lookbook/collection.rb', line 91
def ordered_entities
return @ordered_entities if @ordered_entities.present?
entities = []
as_tree.items.each do |item|
entities.append(item.is_a?(Collection) ? item.ordered_entities : item)
end
@ordered_entities ||= entities.flatten
end
|
#position ⇒ Object
35
36
37
|
# File 'lib/lookbook/collection.rb', line 35
def position
@position ||= parse_position_prefix(basename).first
end
|
#type ⇒ Object
137
138
139
|
# File 'lib/lookbook/collection.rb', line 137
def type
:collection
end
|
#visible_items ⇒ Object
47
48
49
|
# File 'lib/lookbook/collection.rb', line 47
def visible_items
reject { |i| i.hidden? }
end
|