Class: GemXray::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/gemxray/result.rb

Defined Under Namespace

Classes: Reason

Constant Summary collapse

SEVERITIES =
{ danger: 0, warning: 1, info: 2 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_name:, gemfile_line: nil, gemfile_end_line: nil, gemfile_group: nil, suggestion: nil, reasons: [], severity: nil) ⇒ Result

Returns a new instance of Result.



16
17
18
19
20
21
22
23
24
25
# File 'lib/gemxray/result.rb', line 16

def initialize(gem_name:, gemfile_line: nil, gemfile_end_line: nil, gemfile_group: nil, suggestion: nil,
               reasons: [], severity: nil)
  @gem_name = gem_name
  @gemfile_line = gemfile_line
  @gemfile_end_line = gemfile_end_line || gemfile_line
  @gemfile_group = gemfile_group
  @suggestion = suggestion || "Consider removing this entry from Gemfile"
  @reasons = reasons.dup
  @severity = severity || infer_severity
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



13
14
15
# File 'lib/gemxray/result.rb', line 13

def gem_name
  @gem_name
end

#gemfile_end_lineObject

Returns the value of attribute gemfile_end_line.



13
14
15
# File 'lib/gemxray/result.rb', line 13

def gemfile_end_line
  @gemfile_end_line
end

#gemfile_groupObject

Returns the value of attribute gemfile_group.



13
14
15
# File 'lib/gemxray/result.rb', line 13

def gemfile_group
  @gemfile_group
end

#gemfile_lineObject

Returns the value of attribute gemfile_line.



13
14
15
# File 'lib/gemxray/result.rb', line 13

def gemfile_line
  @gemfile_line
end

#reasonsObject (readonly)

Returns the value of attribute reasons.



13
14
15
# File 'lib/gemxray/result.rb', line 13

def reasons
  @reasons
end

#severityObject

Returns the value of attribute severity.



14
15
16
# File 'lib/gemxray/result.rb', line 14

def severity
  @severity
end

#suggestionObject

Returns the value of attribute suggestion.



13
14
15
# File 'lib/gemxray/result.rb', line 13

def suggestion
  @suggestion
end

Instance Method Details

#add_reason(type:, detail:, severity:) ⇒ Object



27
28
29
30
31
# File 'lib/gemxray/result.rb', line 27

def add_reason(type:, detail:, severity:)
  reasons << Reason.new(type: type.to_sym, detail: detail, severity: severity.to_sym)
  self.severity = infer_severity
  self
end

#danger?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gemxray/result.rb', line 56

def danger?
  severity == :danger
end

#info?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/gemxray/result.rb', line 60

def info?
  severity == :info
end

#merge!(other) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/gemxray/result.rb', line 33

def merge!(other)
  other.reasons.each do |reason|
    add_reason(type: reason.type, detail: reason.detail, severity: reason.severity)
  end
  self.gemfile_line ||= other.gemfile_line
  self.gemfile_end_line ||= other.gemfile_end_line
  self.gemfile_group ||= other.gemfile_group
  self.suggestion ||= other.suggestion
  self
end

#reason_typesObject



48
49
50
# File 'lib/gemxray/result.rb', line 48

def reason_types
  reasons.map(&:type).uniq
end

#severity_orderObject



44
45
46
# File 'lib/gemxray/result.rb', line 44

def severity_order
  SEVERITIES.fetch(severity)
end

#to_hObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/gemxray/result.rb', line 64

def to_h
  {
    gem_name: gem_name,
    severity: severity.to_s,
    reasons: reasons.map(&:to_h),
    gemfile_line: gemfile_line,
    gemfile_group: gemfile_group,
    suggestion: suggestion
  }
end

#type_labelObject



52
53
54
# File 'lib/gemxray/result.rb', line 52

def type_label
  reason_types.map { |type| type.to_s.tr("_", "-") }.join(" + ")
end