Module: IGMarkets::Format
- Defined in:
- lib/ig_markets/format.rb
Overview
This module contains shared methods for formatting different content types for display.
Class Method Summary collapse
-
.colored_currency(amount, currency_name) ⇒ String
Returns a formatted string for the specified currency amount and currency, and colors it red for negative values and green for positive values.
-
.currency(amount, currency_name) ⇒ String
Returns a formatted string for the specified currency amount and currency.
-
.level(value) ⇒ String
Returns a formatted string for the specified level.
-
.seconds(value) ⇒ String
Returns a formatted string for the specified number of seconds in the format ‘[<hours>:]<minutes>:<seconds>`.
-
.symbol(value) ⇒ String
Formats the passed symbol into a human-readable string, replacing underscores with spaces and capitalizing the first letter.
Class Method Details
.colored_currency(amount, currency_name) ⇒ String
Returns a formatted string for the specified currency amount and currency, and colors it red for negative values and green for positive values. Two decimal places are used for all currencies except the Japanese Yen.
43 44 45 46 47 48 49 |
# File 'lib/ig_markets/format.rb', line 43 def colored_currency(amount, currency_name) return '' unless amount color = amount.negative? ? :red : :green ColorizedString[currency(amount, currency_name)].colorize color end |
.currency(amount, currency_name) ⇒ String
Returns a formatted string for the specified currency amount and currency. Two decimal places are used for all currencies except the Japanese Yen.
26 27 28 29 30 31 32 33 34 |
# File 'lib/ig_markets/format.rb', line 26 def currency(amount, currency_name) return '' unless amount if ['JPY', '¥'].include? currency_name "#{currency_name} #{format '%i', amount.to_i}" else "#{currency_name} #{format '%.2f', amount.to_f}" end end |
.level(value) ⇒ String
Returns a formatted string for the specified level. At most four decimal places are used to format levels.
13 14 15 16 17 |
# File 'lib/ig_markets/format.rb', line 13 def level(value) return '' unless value Float(format('%.4f', value.to_f)).to_s end |
.seconds(value) ⇒ String
Returns a formatted string for the specified number of seconds in the format ‘[<hours>:]<minutes>:<seconds>`.
56 57 58 59 60 61 62 63 64 |
# File 'lib/ig_markets/format.rb', line 56 def seconds(value) result = if value >= 60 * 60 "#{value / 60 / 60}:#{Kernel.format('%02i', (value / 60) % 60)}" else (value / 60).to_s end "#{result}:#{Kernel.format('%02i', value % 60)}" end |
.symbol(value) ⇒ String
Formats the passed symbol into a human-readable string, replacing underscores with spaces and capitalizing the first letter.
72 73 74 |
# File 'lib/ig_markets/format.rb', line 72 def symbol(value) value.to_s.capitalize.tr '_', ' ' end |