Module: StarlingTerminal::Utils

Defined in:
lib/starling_terminal/utils.rb

Overview

Utility functions for building, formatting and styling the statement

Constant Summary collapse

CURRENCY_SYMBOLS =

A map from currency code (e.g. “GBP”, “EUR”) to symbol for display in the table

{
  'GBP' => '£',
  'USD' => '$',
  'EUR' => ''
}.freeze

Class Method Summary collapse

Class Method Details

.colour_for_amount(amount) ⇒ :red, :green

Returns the colour an amount should be displayed in (red for negative amounts, green for positive)

Parameters:

  • amount (Float)

    the amount

Returns:

  • (:red, :green)

    the colour the amount should be displayed in


40
41
42
# File 'lib/starling_terminal/utils.rb', line 40

def self.colour_for_amount(amount)
  amount > 0 ? :green : :red
end

.float_to_currency(float, currency:) ⇒ String

Converts a Float to a currency valuable with symbol or code, suitable for display

Parameters:

  • float (Float)

    the float to convert to currency

  • currency (String)

    the currency code (e.g. “GBP”, “EUR”) which will be converted to a symbol if possible, or displayed in full

Returns:

  • (String)

    the currency amount, suitable for display (e.g. $5.99, 50 UAH)


19
20
21
22
23
24
# File 'lib/starling_terminal/utils.rb', line 19

def self.float_to_currency(float, currency:)
  currency_symbol = CURRENCY_SYMBOLS.fetch(currency, nil)

  return format("#{currency_symbol}%.2f", float) if currency_symbol
  format("%.2f #{currency}", float)
end

.present_time(time) ⇒ String

Presents a Time as a string in the standard UK format

Parameters:

  • time (Time)

    the time

Returns:

  • (String)

    the string formatted in the standard UK form (e.g. “4/6/2017 10:00”)


31
32
33
# File 'lib/starling_terminal/utils.rb', line 31

def self.present_time(time)
  time.strftime('%-d/%-m/%Y %H:%M')
end