Class: PactBroker::Api::Decorators::RelationshipsCsvDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/api/decorators/relationships_csv_decorator.rb

Instance Method Summary collapse

Constructor Details

#initialize(pacts) ⇒ RelationshipsCsvDecorator

Returns a new instance of RelationshipsCsvDecorator.



7
8
9
10
# 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

Instance Method Details

#pacticipant_array(pacticipant, order) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



38
39
40
# 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_csvObject

rubocop: disable Metrics/CyclomaticComplexity



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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