Class: Trestle::Navigation

Inherits:
Object
  • Object
show all
Defined in:
lib/trestle/navigation.rb,
lib/trestle/navigation/item.rb,
lib/trestle/navigation/block.rb,
lib/trestle/navigation/group.rb

Defined Under Namespace

Classes: Block, Group, Item, NullGroup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Navigation

Returns a new instance of Navigation.


9
10
11
# File 'lib/trestle/navigation.rb', line 9

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items


7
8
9
# File 'lib/trestle/navigation.rb', line 7

def items
  @items
end

Class Method Details

.build(blocks, context) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/trestle/navigation.rb', line 29

def self.build(blocks, context)
  new(blocks.map { |block|
    block.items(context)
  }.flatten.select { |item|
    item.visible?(context)
  })
end

Instance Method Details

#by_groupObject


13
14
15
16
17
18
# File 'lib/trestle/navigation.rb', line 13

def by_group
  sorted_groups = stable_sort(items.group_by { |item| groups[item.group.id] })
  sorted_items = sorted_groups.map { |group, items| [group, stable_sort(items)] }

  Hash[sorted_items]
end

#each(&block) ⇒ Object


20
21
22
# File 'lib/trestle/navigation.rb', line 20

def each(&block)
  by_group.each(&block)
end

#firstObject


24
25
26
27
# File 'lib/trestle/navigation.rb', line 24

def first
  sorted = by_group.values
  sorted.first.first if sorted.any?
end