Class: Compass::Menu::ItemList
- Inherits:
-
Object
- Object
- Compass::Menu::ItemList
- Includes:
- Enumerable
- Defined in:
- lib/compass/menu/item_list.rb
Overview
It is a list of Compass::Menu::Item, which can be filtered and represented as_json recursivelly throuhout the entire tree.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #authorized ⇒ Object
- #each(&block) ⇒ Object
- #filter(**filters) ⇒ Object
-
#initialize(items = []) ⇒ ItemList
constructor
A new instance of ItemList.
- #maximum(attr) ⇒ Object
- #push ⇒ Object
- #select(&block) ⇒ Object
- #sort(attrs) ⇒ Object
- #tagged(tag) ⇒ Object
Constructor Details
#initialize(items = []) ⇒ ItemList
Returns a new instance of ItemList.
9 10 11 |
# File 'lib/compass/menu/item_list.rb', line 9 def initialize(items = []) @items = items end |
Instance Method Details
#as_json(options = {}) ⇒ Object
23 24 25 |
# File 'lib/compass/menu/item_list.rb', line 23 def as_json( = {}) filter(**.fetch(:filter, {})).to_a.as_json() end |
#authorized ⇒ Object
41 42 43 |
# File 'lib/compass/menu/item_list.rb', line 41 def (*) select(&:authorized) end |
#each(&block) ⇒ Object
17 18 19 20 21 |
# File 'lib/compass/menu/item_list.rb', line 17 def each(&block) @items.each do |item| item.each(&block) end end |
#filter(**filters) ⇒ Object
27 28 29 30 31 |
# File 'lib/compass/menu/item_list.rb', line 27 def filter(**filters) filters.reduce(self) do |items, (filter, arg)| arg ? items.public_send(filter, arg) : items end end |
#maximum(attr) ⇒ Object
33 34 35 |
# File 'lib/compass/menu/item_list.rb', line 33 def maximum(attr) filter_map(&attr).max end |
#push ⇒ Object
13 14 15 |
# File 'lib/compass/menu/item_list.rb', line 13 def push(...) @items.push(...) end |
#select(&block) ⇒ Object
37 38 39 |
# File 'lib/compass/menu/item_list.rb', line 37 def select(&block) self.class.new(super(&block)) end |
#sort(attrs) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/compass/menu/item_list.rb', line 49 def sort(attrs) attrs = Array(attrs) sorted = sort_by do |item| attrs.map { |attr| item.public_send(attr) } end self.class.new(sorted) end |
#tagged(tag) ⇒ Object
45 46 47 |
# File 'lib/compass/menu/item_list.rb', line 45 def tagged(tag) select { |item| item.&.include?(tag) } end |