class PactBroker::Relationships::Groupify

Public Class Methods

call(index_items) click to toggle source
# File lib/pact_broker/relationships/groupify.rb, line 13
def self.call index_items
  recurse_groups([], index_items.dup).collect { |group| Domain::Group.new(group) }
end
recurse_groups(groups, index_item_pool) click to toggle source
# File lib/pact_broker/relationships/groupify.rb, line 17
def self.recurse_groups groups, index_item_pool
  if index_item_pool.empty?
    groups
  else
    first, *rest = index_item_pool
    group = [first]
    new_connections = true
    while new_connections
      new_connections = false
      group = rest.inject(group) do |connected, candidate|
        if connected.select { |index_item| index_item.connected?(candidate) }.any?
          new_connections = true
          connected + [candidate]
        else
          connected
        end
      end

      rest = rest - group
      group.uniq
    end

    recurse_groups(groups + [group], index_item_pool - group)
  end
end