Class: Spout::Helpers::TableFormatting

Inherits:
Object
  • Object
show all
Defined in:
lib/spout/helpers/table_formatting.rb

Overview

Formats numbers in coverage and outlier tables.

Class Method Summary collapse

Class Method Details

.format_count(number) ⇒ Object

count:

   0          ->             "-"
  10          ->            "10"
1000          ->         "1,000"

Input (Numeric) -> Output (String)



33
34
35
# File 'lib/spout/helpers/table_formatting.rb', line 33

def self.format_count(number)
  number.zero? || number.nil? ? "-" : Spout::Helpers::NumberHelper.number_with_delimiter(number)
end

.format_decimal(number, format) ⇒ Object

decimal:

   0          ->           "0.0"
  10          ->          "10.0"
 -50.2555     ->         "-50.3"
1000          ->       "1,000.0"

12412423.42252525 -> “12,412,423.4” Input (Numeric) -> Output (String)



44
45
46
47
48
49
50
51
# File 'lib/spout/helpers/table_formatting.rb', line 44

def self.format_decimal(number, format)
  precision = 1
  precision = -Math.log10(number.abs).floor if number.abs < 1.0 && !number.zero?

  number = Spout::Helpers::NumberHelper.number_with_delimiter(number.to_f.round(precision))
  number = format % number if format
  number
end

.format_nilObject



24
25
26
# File 'lib/spout/helpers/table_formatting.rb', line 24

def self.format_nil
  "-"
end

.format_number(number, type, format = nil) ⇒ Object

type: :count or :decimal



14
15
16
17
18
19
20
21
22
# File 'lib/spout/helpers/table_formatting.rb', line 14

def self.format_number(number, type, format = nil)
  if number.nil?
    format_nil
  elsif type == :count
    format_count(number)
  else
    format_decimal(number, format)
  end
end