Class: Lookbook::Collection

Inherits:
Entity
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lookbook/collection.rb

Direct Known Subclasses

PageCollection, Preview, PreviewCollection

Constant Summary

Constants included from Utils

Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#hidden?, #hierarchy_depth, #lookup_path, #matchers, #path

Constructor Details

#initialize(path = "", items = []) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lookbook/collection.rb', line 7

def initialize(path = "", items = [])
  @items = []
  if path.is_a?(Array)
    items = path
    path = ""
  end

  @path = path.delete_prefix("/").delete_suffix("/")
  super(@path)

  items.each { |item| add(item) }
end

Class Method Details

.describe_asObject



156
157
158
# File 'lib/lookbook/collection.rb', line 156

def describe_as
  "items"
end

Instance Method Details

#add(item) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/lookbook/collection.rb', line 54

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lookbook/collection.rb', line 118

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

#clearObject



40
41
42
# File 'lib/lookbook/collection.rb', line 40

def clear
  @items = []
end

#collapsible?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/lookbook/collection.rb', line 141

def collapsible?
  false
end

#find(lookup = nil, &block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/lookbook/collection.rb', line 73

def find(lookup = nil, &block)
  if lookup
    lookup.is_a?(Symbol) ? find_by_id(lookup) : find_by_path(lookup)
  elsif block
    items.find(&block)
  end
end

#find_by_id(id) ⇒ Object



81
82
83
84
# File 'lib/lookbook/collection.rb', line 81

def find_by_id(id)
  id = id.to_s.tr("_", "-")
  items.find { |i| i.id.to_s == id }
end

#find_by_path(path) ⇒ Object



86
87
88
# File 'lib/lookbook/collection.rb', line 86

def find_by_path(path)
  items.find { |i| i.lookup_path == path }
end

#find_firstObject



104
105
106
# File 'lib/lookbook/collection.rb', line 104

def find_first
  ordered_entities.first
end

#find_next(item) ⇒ Object



108
109
110
111
# File 'lib/lookbook/collection.rb', line 108

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



90
91
92
93
# File 'lib/lookbook/collection.rb', line 90

def find_parent(child)
  parent_path = child.lookup_path.split("/").pop.join("/")
  find_by_path(parent_path)
end

#find_previous(item) ⇒ Object



113
114
115
116
# File 'lib/lookbook/collection.rb', line 113

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



64
65
66
67
# File 'lib/lookbook/collection.rb', line 64

def get(name)
  name = name.underscore
  items.find { |item| item.name.underscore == name }
end

#get_or_create(name) ⇒ Object



69
70
71
# File 'lib/lookbook/collection.rb', line 69

def get_or_create(name)
  get(remove_position_prefix(name)).presence || add(name)
end

#idObject



20
21
22
# File 'lib/lookbook/collection.rb', line 20

def id
  lookup_path.present? ? super : "root"
end

#itemsObject



36
37
38
# File 'lib/lookbook/collection.rb', line 36

def items
  @items.sort_by { |item| [item.hierarchy_depth, item&.position, item.label] }
end

#labelObject



28
29
30
# File 'lib/lookbook/collection.rb', line 28

def label
  name&.titleize
end

#nameObject



24
25
26
# File 'lib/lookbook/collection.rb', line 24

def name
  @name ||= remove_position_prefix(basename)
end

#non_empty_itemsObject



48
49
50
51
52
# File 'lib/lookbook/collection.rb', line 48

def non_empty_items
  items.select do |item|
    !item.is_a?(Lookbook::Collection) || item.items.any?
  end
end

#ordered_entitiesObject



95
96
97
98
99
100
101
102
# File 'lib/lookbook/collection.rb', line 95

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

#positionObject



32
33
34
# File 'lib/lookbook/collection.rb', line 32

def position
  @position ||= parse_position_prefix(basename).first
end

#typeObject



145
146
147
# File 'lib/lookbook/collection.rb', line 145

def type
  :collection
end

#visible_itemsObject



44
45
46
# File 'lib/lookbook/collection.rb', line 44

def visible_items
  reject { |i| i.hidden? }
end