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-2.9.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.9.0.beta.5 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.8.0.beta.5 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.8.0.beta.4 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.7.0.beta.3 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.7.0.beta.2 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.7.0.beta.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.6.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.5.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.5.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.4.2 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.4.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.4.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.3.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.2.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.1.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.1.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.0.5 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.0.4 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.0.3 lib/pact_broker/api/decorators/relationships_csv_decorator.rb