Class: Danger::MessageGroup

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, line: nil) ⇒ MessageGroup

Returns a new instance of MessageGroup.



5
6
7
8
# File 'lib/danger/danger_core/message_group.rb', line 5

def initialize(file: nil, line: nil)
  @file = file
  @line = line
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



51
52
53
# File 'lib/danger/danger_core/message_group.rb', line 51

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



51
52
53
# File 'lib/danger/danger_core/message_group.rb', line 51

def line
  @line
end

Instance Method Details

#<<(message) ⇒ Object

Adds a message to the group.

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/danger/danger_core/message_group.rb', line 29

def <<(message)
  # TODO: insertion sort
  return nil unless same_line?(message)

  inserted = false
  messages.each.with_index do |other, idx|
    if (message <=> other) == -1
      inserted = true
      messages.insert(idx, message)
      break
    end
  end
  messages << message unless inserted
  messages
end

#markdownsObject



64
65
66
# File 'lib/danger/danger_core/message_group.rb', line 64

def markdowns
  messages.select { |x| x.type == :markdown }
end

#merge(other) ⇒ Object

Merges two ‘MessageGroup`s that represent the same line of code In future, perhaps `MessageGroup` will be able to represent a group of messages for multiple lines.

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/danger/danger_core/message_group.rb', line 21

def merge(other)
  raise ArgumentError, "Cannot merge with MessageGroup for a different line" unless same_line?(other)

  @messages = (messages + other.messages).uniq
end

#messagesObject

The list of messages in this group. This list will be sorted in decreasing order of severity (error, warning, message, markdown)



47
48
49
# File 'lib/danger/danger_core/message_group.rb', line 47

def messages
  @messages ||= []
end

#same_line?(other) ⇒ Boolean

Returns whether this ‘MessageGroup` is for the same line of code as

`other`, taking which file they are in to account.

Parameters:

Returns:

  • (Boolean)

    whether this ‘MessageGroup` is for the same line of code



14
15
16
# File 'lib/danger/danger_core/message_group.rb', line 14

def same_line?(other)
  other.file == file && other.line == line
end

#statsObject

:errors_count

Returns:

  • a hash of statistics. Currently only :warnings_count and



55
56
57
58
59
60
61
62
# File 'lib/danger/danger_core/message_group.rb', line 55

def stats
  stats = { warnings_count: 0, errors_count: 0 }
  messages.each do |msg|
    stats[:warnings_count] += 1 if msg.type == :warning
    stats[:errors_count] += 1 if msg.type == :error
  end
  stats
end