Sha256: a055e463f67778ebdf4195eabc5f6d1c123f9ad1a341ed328f1668b50a735990

Contents?: true

Size: 1.26 KB

Versions: 47

Compression:

Stored size: 1.26 KB

Contents

require 'csv'
require 'set'

module PactBroker

  module Api

    module Decorators

      class RelationshipsCsvDecorator

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

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

          @index_items.each do | index_item |
            hash[index_item.consumer.id] ||= pacticipant_array(index_item.consumer, hash.size + 1)
            hash[index_item.provider.id] ||= pacticipant_array(index_item.provider, hash.size + 1)
            hash[index_item.consumer.id] << index_item.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

47 entries across 47 versions & 1 rubygems

Version Path
pact_broker-2.40.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.39.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.38.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.38.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.37.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.36.1 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.36.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.35.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.34.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.33.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.32.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.31.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.30.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.29.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.27.6 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.27.5 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.27.4 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.27.3 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.27.2 lib/pact_broker/api/decorators/relationships_csv_decorator.rb
pact_broker-2.27.0 lib/pact_broker/api/decorators/relationships_csv_decorator.rb