Class: Danger::MessageAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/danger_core/message_aggregator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger") ⇒ MessageAggregator

Returns a new instance of MessageAggregator.



12
13
14
15
16
17
18
19
# File 'lib/danger/danger_core/message_aggregator.rb', line 12

def initialize(warnings: [],
               errors: [],
               messages: [],
               markdowns: [],
               danger_id: "danger")
  @messages = warnings + errors + messages + markdowns
  @danger_id = danger_id
end

Class Method Details

.aggregate(*args) ⇒ Object



8
9
10
# File 'lib/danger/danger_core/message_aggregator.rb', line 8

def self.aggregate(*args)
  new(*args).aggregate
end

Instance Method Details

#aggregate[MessageGroup]

aggregates the messages into an array of MessageGroups

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/danger/danger_core/message_aggregator.rb', line 23

def aggregate
  # oookay I took some shortcuts with this one.
  # first, sort messages by file and line
  @messages.sort! { |a, b| a.compare_by_file_and_line(b) }

  # now create an initial empty message group
  first_group = MessageGroup.new(file: nil,
                                 line: nil)
  @message_groups = @messages.reduce([first_group]) do |groups, msg|
    # We get to take a shortcut because we sorted the messages earlier - only
    # have to see if we can append msg to the last group in the list
    if groups.last << msg
      # we appended it, so return groups unchanged
      groups
    else
      # have to create a new group since msg wasn't appended to the other
      # group
      new_group = MessageGroup.new(file: msg.file,
                                   line: msg.line)
      new_group << msg
      groups << new_group
    end
  end

  @message_groups.extend(Helpers::MessageGroupsArrayHelper)
end