Class: Danger::BaseMessage

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

Direct Known Subclasses

Markdown, Violation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, message:, file: nil, line: nil, start_line: nil, side: nil, start_side: nil) ⇒ BaseMessage

Returns a new instance of BaseMessage.



7
8
9
10
11
12
13
14
15
# File 'lib/danger/danger_core/messages/base.rb', line 7

def initialize(type:, message:, file: nil, line: nil, start_line: nil, side: nil, start_side: nil)
  @type = type
  @message = message
  @file = file
  @line = line
  @start_line = start_line
  @side = side
  @start_side = start_side
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def file
  @file
end

#lineObject

Returns the value of attribute line.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def line
  @line
end

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def message
  @message
end

#sideObject

Returns the value of attribute side.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def side
  @side
end

#start_lineObject

Returns the value of attribute start_line.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def start_line
  @start_line
end

#start_sideObject

Returns the value of attribute start_side.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def start_side
  @start_side
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/danger/danger_core/messages/base.rb', line 5

def type
  @type
end

Instance Method Details

#cmp_nils(a, b) ⇒ Object

compares a and b based entirely on whether one or the other is nil arguments are in the same order as ‘a <=> b` nil is sorted earlier - so cmp_nils(nil, 1) => -1

If neither are nil, rather than returning ‘a <=> b` which would seem like the obvious shortcut, `nil` is returned. This allows us to distinguish between cmp_nils returning 0 for a comparison of filenames, which means “a comparison on the lines is meaningless - you cannot have a line number for a nil file - so they should be sorted the same”, and a <=> b returning 0, which means “the files are the same, so compare on the lines”

Returns:

  • 0, 1, -1, or nil



43
44
45
46
47
48
49
50
51
# File 'lib/danger/danger_core/messages/base.rb', line 43

def cmp_nils(a, b)
  if a.nil? && b.nil?
    0
  elsif a.nil?
    -1
  elsif b.nil?
    1
  end
end

#compare_by_file_and_line(other) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/danger/danger_core/messages/base.rb', line 17

def compare_by_file_and_line(other)
  order = cmp_nils(file, other.file)
  return order unless order.nil?

  order = file <=> other.file
  return order unless order.zero?

  order = cmp_nils(line, other.line)
  return order unless order.nil?

  line <=> other.line
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/danger/danger_core/messages/base.rb', line 53

def eql?(other)
  return self == other
end

#inline?Boolean

Returns true if is a file or line, false otherwise

Returns:

  • (Boolean)

    returns true if is a file or line, false otherwise



58
59
60
# File 'lib/danger/danger_core/messages/base.rb', line 58

def inline?
  file || line
end