Class: Lookbook::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "") ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
# File 'lib/lookbook/collection.rb', line 5

def initialize(path = "")
  @path = path.delete_prefix("/").delete_suffix("/")
  @items = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/lookbook/collection.rb', line 3

def path
  @path
end

Instance Method Details

#add(item) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/lookbook/collection.rb', line 30

def add(item)
  if item.is_a?(String)
    item = Collection.new([@path, item].join("/"))
  end
  @items << item
  item
end

#get(name) ⇒ Object



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

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

#get_or_create(name) ⇒ Object



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

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

#hierarchy_depthObject



22
23
24
# File 'lib/lookbook/collection.rb', line 22

def hierarchy_depth
  @path ? @path.split("/").size : 0
end

#idObject



10
11
12
# File 'lib/lookbook/collection.rb', line 10

def id
  (@path || "root").underscore.tr("_", "-")
end

#items(sorted = true) ⇒ Object



26
27
28
# File 'lib/lookbook/collection.rb', line 26

def items(sorted = true)
  sorted ? @items.sort_by(&:label) : @items
end

#labelObject



18
19
20
# File 'lib/lookbook/collection.rb', line 18

def label
  name&.titleize
end

#nameObject



14
15
16
# File 'lib/lookbook/collection.rb', line 14

def name
  @path.present? ? @path.split("/").last : "root"
end

#typeObject



47
48
49
# File 'lib/lookbook/collection.rb', line 47

def type
  :collection
end