class PactBroker::Api::Decorators::RelationshipsCsvDecorator

Attributes

pacts[R]

Public Class Methods

new(pacts) click to toggle source
# File lib/pact_broker/api/decorators/relationships_csv_decorator.rb, line 7
def initialize pacts
  @pacts = pacts
  @index_items = pacts.collect{|pact| PactBroker::Domain::IndexItem.new(pact.consumer, pact.provider)}
end

Public Instance Methods

pacticipant_array(pacticipant, order) click to toggle source

rubocop: enable Metrics/CyclomaticComplexity

# File lib/pact_broker/api/decorators/relationships_csv_decorator.rb, line 38
def pacticipant_array pacticipant, order
  [pacticipant.id, pacticipant.name, 1, 1, 0, order]
end
to_csv() click to toggle source

rubocop: disable Metrics/CyclomaticComplexity

# File lib/pact_broker/api/decorators/relationships_csv_decorator.rb, line 13
def to_csv
  hash = {}

  @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(&: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