Sha256: 1b247de049824eac34b7a4a6fedc628698216ad2174eea92d466056b4df000f8

Contents?: true

Size: 687 Bytes

Versions: 6

Compression:

Stored size: 687 Bytes

Contents

module Trestle
  class Navigation
    extend ActiveSupport::Autoload

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

    def initialize(blocks)
      @blocks = blocks
    end

    def items
      @blocks.map(&:items).flatten
    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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
trestle-0.8.6 lib/trestle/navigation.rb
trestle-0.8.5 lib/trestle/navigation.rb
trestle-0.8.4 lib/trestle/navigation.rb
trestle-0.8.3 lib/trestle/navigation.rb
trestle-0.8.2 lib/trestle/navigation.rb
trestle-0.8.0 lib/trestle/navigation.rb