Class: PactBroker::Relationships::Groupify

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/relationships/groupify.rb

Class Method Summary collapse

Class Method Details

.call(index_items) ⇒ Object



13
14
15
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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