Sha256: 178e891e50c390c8eb65caa1f76c66cfe9526ff1244606eda358efd68c49aaba

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require 'pact_broker/domain/group'

=begin
  Splits all relationships up into groups of non-connecting relationships.
=end

module PactBroker

  module Functions

    class Groupify

      def self.call relationships
        recurse_groups([], relationships.dup).collect{ | group | Domain::Group.new(group) }
      end

      def self.recurse_groups groups, relationship_pool
        if relationship_pool.empty?
          groups
        else
          first, *rest = *relationship_pool
          group = recurse first, rest
          recurse_groups(groups + [group], relationship_pool - group)
        end
      end

      def self.recurse relationship, relationship_pool
        connected_relationships = relationship_pool.select{ | candidate| candidate.connected?(relationship) }
        if connected_relationships.empty?
          [relationship]
        else
          ([relationship] + connected_relationships.map{| connected_relationship| recurse(connected_relationship, relationship_pool - connected_relationships)}.flatten).uniq
        end
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pact_broker-1.4.0 lib/pact_broker/functions/groupify.rb
pact_broker-1.3.2.rc1 lib/pact_broker/functions/groupify.rb