class Trestle::Navigation

Attributes

items[R]

Public Class Methods

build(blocks, context) click to toggle source
# 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
new(items) click to toggle source
# File lib/trestle/navigation.rb, line 9
def initialize(items)
  @items = items
end

Public Instance Methods

by_group() click to toggle source
# 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) click to toggle source
# File lib/trestle/navigation.rb, line 20
def each(&block)
  by_group.each(&block)
end
first() click to toggle source
# File lib/trestle/navigation.rb, line 24
def first
  sorted = by_group.values
  sorted.first.first if sorted.any?
end

Private Instance Methods

groups() click to toggle source
# File lib/trestle/navigation.rb, line 42
def groups
  @groups ||= items.inject({}) { |groups, item|
    group = groups[item.group.id]

    groups[item.group.id] = group ? group.merge(item.group) : item.group
    groups
  }
end
stable_sort(items) click to toggle source
# File lib/trestle/navigation.rb, line 38
def stable_sort(items)
  items.sort_by.with_index { |item, i| [item, i] }
end