Module: TRMNL::Liquid::Fallback
- Defined in:
- lib/trmnl/liquid/fallback.rb
Overview
library-native formatting functions that don't rely on ActionView helpers
Class Method Summary collapse
-
.number_to_currency(number, unit, delimiter, separator, precision) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
.number_with_delimiter(number, delimiter, separator) ⇒ Object
:reek:TooManyStatements rubocop:todo Metrics/MethodLength.
-
.ordinalize(number) ⇒ Object
:reek:TooManyStatements.
- .pluralize(count, singular, plural) ⇒ Object
Class Method Details
.number_to_currency(number, unit, delimiter, separator, precision) ⇒ Object
rubocop:disable Metrics/ParameterLists
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/trmnl/liquid/fallback.rb', line 33 def number_to_currency number, unit, delimiter, separator, precision result = number_with_delimiter number, delimiter, separator dollars, cents = result.split separator if precision <= 0 "#{unit}#{dollars}" else cents = cents.to_s[0..(precision - 1)].ljust precision, "0" "#{unit}#{dollars}#{separator}#{cents}" end end |
.number_with_delimiter(number, delimiter, separator) ⇒ Object
:reek:TooManyStatements rubocop:todo Metrics/MethodLength
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/trmnl/liquid/fallback.rb', line 11 def number_with_delimiter number, delimiter, separator value = number.to_s # return early if it's not a simple numeric-like string return value unless value.match?(/\A-?\d+(\.\d+)?\z/) integer, fractional = value.split "." negative = integer.start_with? "-" integer = integer[1..] if negative integer_with_delimiters = integer.reverse.scan(/\d{1,3}/).join(delimiter).reverse integer_with_delimiters = "-#{integer_with_delimiters}" if negative if fractional integer_with_delimiters + separator + fractional else integer_with_delimiters end end |
.ordinalize(number) ⇒ Object
:reek:TooManyStatements
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/trmnl/liquid/fallback.rb', line 47 def ordinalize number return "#{number}th" if (11..13).cover? number % 100 suffix = case number % 10 when 1 then "st" when 2 then "nd" when 3 then "rd" else "th" end "#{number}#{suffix}" end |
.pluralize(count, singular, plural) ⇒ Object
60 61 62 63 |
# File 'lib/trmnl/liquid/fallback.rb', line 60 def pluralize count, singular, plural plural ||= "#{singular}s" count == 1 ? "1 #{singular}" : "#{count} #{plural}" end |