class SimpleCovSmallBadge::Formatter

Basic Badge Formater Class that creates the badges.

Public Class Methods

new(output = nil) click to toggle source
# File lib/simplecov_small_badge/formatter.rb, line 9
def initialize(output = nil)
  @output = output || STDOUT
  @config = SimpleCovSmallBadge.config
end

Public Instance Methods

format(result) click to toggle source
# File lib/simplecov_small_badge/formatter.rb, line 14
def format(result)
  percent = result.source_files.covered_percent.round(0)
  @image = RepoSmallBadge::Image.new(map_image_config(state(percent)))
  badge('total', 'total', percent)
  group_percent_from_result(result) do |name, title, cov_percent|
    badge(name, title, cov_percent.round(0))
  end
end

Private Instance Methods

badge(name, title, percent) click to toggle source
# File lib/simplecov_small_badge/formatter.rb, line 25
def badge(name, title, percent)
  percent_txt = percent_text(percent)
  @image.config_merge(map_image_config(state(percent)))
  @image.badge(name, title, percent_txt)
end
group_percent_from_result(result) { |name, name, covered, covered| ... } click to toggle source

converts the result to a hash consisting of the groupname array of percentage (integer in percent), strength float and the state [ 'good', 'bad', 'unknown' ] consolidated for each group.

# File lib/simplecov_small_badge/formatter.rb, line 63
def group_percent_from_result(result)
  result.groups.each do |name, files|
    covered = files.covered_percent.round(0)
    yield name, name, covered, covered
  end
end
map_image_config(state) click to toggle source
# File lib/simplecov_small_badge/formatter.rb, line 47
def map_image_config(state)
  hash = {}
  @config.to_hash.map do |key, value|
    key = key
          .to_s.sub(/^coverage_background_#{state}/, 'value_background')
          .to_sym
    key = key.to_s.sub(/^coverage_/, 'value_').to_sym
    hash[key] = value
  end
  hash
end
percent_text(percent) click to toggle source
# File lib/simplecov_small_badge/formatter.rb, line 31
def percent_text(percent)
  "#{percent}#{@config.percent_sign}"
end
state(covered_percent) click to toggle source
# File lib/simplecov_small_badge/formatter.rb, line 35
def state(covered_percent)
  if SimpleCov.minimum_coverage&.positive?
    if covered_percent >= SimpleCov.minimum_coverage
      'good'
    else
      'bad'
    end
  else
    'unknown'
  end
end