Sha256: 0ee54f252483f7e2ab956d42430e7deca872dc587a2b191995c1731ae1be4f3e

Contents?: true

Size: 828 Bytes

Versions: 4

Compression:

Stored size: 828 Bytes

Contents

module Trestle
  class Navigation
    extend ActiveSupport::Autoload

    autoload :Block
    autoload :Item
    autoload :Group
    autoload :NullGroup, "trestle/navigation/group"

    attr_reader :items

    def initialize(items)
      @items = items
    end

    def by_group
      Hash[stable_sort(items.group_by(&:group)).map { |group, items| [group, stable_sort(items)] }]
    end

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

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

    def visible(context)
      self.class.new(items.select { |item| item.visible?(context) })
    end

    def self.build(blocks)
      new(blocks.map(&:items).flatten)
    end

  private
    def stable_sort(items)
      items.sort_by.with_index { |item, i| [item, i] }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trestle-0.8.10 lib/trestle/navigation.rb
trestle-0.8.9 lib/trestle/navigation.rb
trestle-0.8.8 lib/trestle/navigation.rb
trestle-0.8.7 lib/trestle/navigation.rb