Class: Stattleship::StatFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/stattleship/formatters/stat_formatter.rb

Class Method Summary collapse

Class Method Details

.format(value:, precision: 0) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/stattleship/formatters/stat_formatter.rb', line 3

def self.format(value:, precision: 0)
  if value
    value.to_d.to_f.round(precision)
  else
    ''
  end
end

.stat(stat_name:, value:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stattleship/formatters/stat_formatter.rb', line 11

def self.stat(stat_name:, value:)
  if stat_name.nil? || value.nil?
    return ''
  end

  if stat_name.end_with?('average') ||
     stat_name.end_with?('avg') ||
     stat_name.end_with?('pct') ||
     stat_name.end_with?('percentage')
    self.format(value: value, precision: 3)
  elsif stat_name.end_with?('rating')
    self.format(value: value, precision: 5)
  else
     self.format(value: value)
  end
end