Class: Gitlab::Badge::Coverage::Template

Inherits:
Template
  • Object
show all
Defined in:
lib/gitlab/badge/coverage/template.rb

Overview

Class that represents a coverage badge template.

Template object will be passed to badge.svg.erb template.

Constant Summary collapse

STATUS_COLOR =
{
  good: '#4c1',
  acceptable: '#a3c51c',
  medium: '#dfb317',
  low: '#e05d44',
  unknown: '#9f9f9f'
}.freeze

Constants inherited from Template

Template::MAX_KEY_TEXT_SIZE, Template::MAX_KEY_WIDTH

Instance Method Summary collapse

Methods inherited from Template

#key_color, #key_text_anchor, #value_text_anchor, #width

Constructor Details

#initialize(badge) ⇒ Template

Returns a new instance of Template.


20
21
22
23
24
25
# File 'lib/gitlab/badge/coverage/template.rb', line 20

def initialize(badge)
  @entity = badge.entity
  @status = badge.status
  @key_text = badge.customization.dig(:key_text)
  @key_width = badge.customization.dig(:key_width)
end

Instance Method Details

#key_textObject


27
28
29
30
31
32
33
# File 'lib/gitlab/badge/coverage/template.rb', line 27

def key_text
  if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
    @key_text
  else
    @entity.to_s
  end
end

#key_widthObject


39
40
41
42
43
44
45
# File 'lib/gitlab/badge/coverage/template.rb', line 39

def key_width
  if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
    @key_width
  else
    62
  end
end

#value_colorObject


51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/badge/coverage/template.rb', line 51

def value_color
  case @status
  when 95..100 then STATUS_COLOR[:good]
  when 90..95 then STATUS_COLOR[:acceptable]
  when 75..90 then STATUS_COLOR[:medium]
  when 0..75 then STATUS_COLOR[:low]
  else
    STATUS_COLOR[:unknown]
  end
end

#value_textObject


35
36
37
# File 'lib/gitlab/badge/coverage/template.rb', line 35

def value_text
  @status ? ("%.2f%%" % @status) : 'unknown'
end

#value_widthObject


47
48
49
# File 'lib/gitlab/badge/coverage/template.rb', line 47

def value_width
  @status ? 54 : 58
end