Class: Lookbook::Collection
- Inherits:
-
Object
- Object
- Lookbook::Collection
- Defined in:
- lib/lookbook/collection.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #add(item) ⇒ Object
- #get(name) ⇒ Object
- #get_or_create(name) ⇒ Object
- #hierarchy_depth ⇒ Object
- #id ⇒ Object
-
#initialize(path = "") ⇒ Collection
constructor
A new instance of Collection.
- #items(sorted = true) ⇒ Object
- #label ⇒ Object
- #name ⇒ Object
- #type ⇒ Object
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
#path ⇒ Object (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_depth ⇒ Object
22 23 24 |
# File 'lib/lookbook/collection.rb', line 22 def hierarchy_depth @path ? @path.split("/").size : 0 end |
#id ⇒ Object
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 |
#label ⇒ Object
18 19 20 |
# File 'lib/lookbook/collection.rb', line 18 def label name&.titleize end |
#name ⇒ Object
14 15 16 |
# File 'lib/lookbook/collection.rb', line 14 def name @path.present? ? @path.split("/").last : "root" end |
#type ⇒ Object
47 48 49 |
# File 'lib/lookbook/collection.rb', line 47 def type :collection end |