Module: GemsBond::Helpers::FormattingHelper

Included in:
Printers::HTML, Printers::Stdout
Defined in:
lib/gems_bond/helpers/formatting_helper.rb

Overview

Formatting helper

Constant Summary collapse

MISSING =
"-"

Class Method Summary collapse

Class Method Details

.human_date(date) ⇒ String

Returns a date with a readable format

Examples:

human_date(Date.new(2017, 11, 19)) #=> "2007-11-19"
human_date(nil) #=> "-"

Parameters:

  • date (Date)

Returns:

  • (String)


17
18
19
20
21
# File 'lib/gems_bond/helpers/formatting_helper.rb', line 17

def human_date(date)
  return MISSING if date.nil?

  date.strftime("%F")
end

.human_number(number) ⇒ String

Returns a number with a readable format

Examples:

human_number(1_000_000) #=> "1 000 000"
human_number(nil) #=> "-"

Parameters:

  • date (Integer)

Returns:

  • (String)


29
30
31
32
33
# File 'lib/gems_bond/helpers/formatting_helper.rb', line 29

def human_number(number)
  return MISSING if number.nil?

  number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 ")
end

.human_score(score) ⇒ String

Returns score out of 100

Examples:

human_score(0.5) #=> "50"
human_score(nil) #=> "-"

Parameters:

  • date (Float)

Returns:

  • (String)


41
42
43
44
45
# File 'lib/gems_bond/helpers/formatting_helper.rb', line 41

def human_score(score)
  return MISSING if score.nil?

  (score * 100).round
end