Sha256: a8f578c1554e7290689d4d851cec626fc20125d8e8f5edde63b1fc98cdcc8a1a

Contents?: true

Size: 1.29 KB

Versions: 57

Compression:

Stored size: 1.29 KB

Contents

require 'csv'
require 'set'

module PactBroker

  module Api

    module Decorators

      class RelationshipsCsvDecorator

        def initialize pacts
          @pacts = pacts
          @relationships = pacts.collect{|pact| PactBroker::Domain::Relationship.new(pact.consumer,pact.provider)}
        end

        def to_csv
          hash = {}
          pacticipants = @relationships.collect{|r| r.pacticipants}.flatten.uniq

          @relationships.each do | relationship |
            hash[relationship.consumer.id] ||= pacticipant_array(relationship.consumer, hash.size + 1)
            hash[relationship.provider.id] ||= pacticipant_array(relationship.provider, hash.size + 1)
            hash[relationship.consumer.id] << relationship.provider.id
          end

          max_length = hash.values.collect{|array| array.size}.max

          hash.values.each do | array |
            while array.size < max_length
              array << 0
            end
          end

          CSV.generate do |csv|
            hash.values.each do | array |
              csv << array
            end
          end

        end

        def pacticipant_array pacticipant, order
          [pacticipant.id, pacticipant.name, 1, 1, 0, order]
        end

        private

        attr_accessor :pacts

      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
pact_broker-1.12.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.11.2 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.11.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.11.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.10.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.9.3 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.9.2 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.9.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.9.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.9.0.rc1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.8.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.8.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.7.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.6.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.5.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.4.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-1.3.2.rc1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb