class Lookbook::Collection

Attributes

path[R]

Public Class Methods

new(path = "") click to toggle source
# File lib/lookbook/collection.rb, line 5
def initialize(path = "")
  @path = path.delete_prefix("/").delete_suffix("/")
  @items = []
end

Public Instance Methods

add(item) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lookbook/collection.rb, line 43
def get_or_create(name)
  get(name).presence || add(name)
end
hierarchy_depth() click to toggle source
# File lib/lookbook/collection.rb, line 22
def hierarchy_depth
  @path ? @path.split("/").size : 0
end
id() click to toggle source
# File lib/lookbook/collection.rb, line 10
def id
  (@path || "root").underscore.tr("_", "-")
end
items(sorted = true) click to toggle source
# File lib/lookbook/collection.rb, line 26
def items(sorted = true)
  sorted ? @items.sort_by(&:name) : @items
end
label() click to toggle source
# File lib/lookbook/collection.rb, line 18
def label
  name.titleize
end
name() click to toggle source
# File lib/lookbook/collection.rb, line 14
def name
  @path ? @path.split("/").last : "root"
end
type() click to toggle source
# File lib/lookbook/collection.rb, line 47
def type
  :collection
end