Class: Compass::Menu::ItemList

Inherits:
Object
  • Object
show all
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

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(options = {})
  filter(**options.fetch(:filter, {})).to_a.as_json(options)
end

#authorizedObject



41
42
43
# File 'lib/compass/menu/item_list.rb', line 41

def authorized(*)
  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

#pushObject



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.tags&.include?(tag) }
end