Module: SqaDemo::Sinatra::Helpers::Formatting
- Defined in:
- lib/sqa_demo/sinatra/helpers/formatting.rb
Instance Method Summary collapse
-
#find_extremes(stocks_data, key, higher_is_better) ⇒ Object
Find best/worst tickers for a given metric.
-
#format_compare_value(value, format_type) ⇒ Object
Format comparison value based on type.
- #format_currency(value) ⇒ Object
- #format_number(value) ⇒ Object
- #format_percent(value) ⇒ Object
Instance Method Details
#find_extremes(stocks_data, key, higher_is_better) ⇒ Object
Find best/worst tickers for a given metric
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sqa_demo/sinatra/helpers/formatting.rb', line 47 def find_extremes(stocks_data, key, higher_is_better) return [nil, nil] if higher_is_better.nil? values = stocks_data.map { |t, d| [t, d[key]] }.reject { |_, v| v.nil? } return [nil, nil] if values.empty? sorted = values.sort_by { |_, v| v } if higher_is_better [sorted.last[0], sorted.first[0]] # best is highest, worst is lowest else [sorted.first[0], sorted.last[0]] # best is lowest, worst is highest end end |
#format_compare_value(value, format_type) ⇒ Object
Format comparison value based on type
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sqa_demo/sinatra/helpers/formatting.rb', line 20 def format_compare_value(value, format_type) return '-' if value.nil? case format_type when :currency sprintf('$%.2f', value) when :currency_billions sprintf('$%.2fB', value / 1_000_000_000.0) when :percent sprintf('%.2f%%', value) when :percent_sign prefix = value >= 0 ? '+' : '' "#{prefix}#{sprintf('%.2f', value)}%" when :percent_from_decimal sprintf('%.1f%%', value * 100) when :decimal2 sprintf('%.2f', value) when :decimal3 sprintf('%.3f', value) when :number value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse else value.to_s end end |
#format_currency(value) ⇒ Object
11 12 13 |
# File 'lib/sqa_demo/sinatra/helpers/formatting.rb', line 11 def format_currency(value) sprintf("$%.2f", value) end |
#format_number(value) ⇒ Object
15 16 17 |
# File 'lib/sqa_demo/sinatra/helpers/formatting.rb', line 15 def format_number(value) value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end |
#format_percent(value) ⇒ Object
7 8 9 |
# File 'lib/sqa_demo/sinatra/helpers/formatting.rb', line 7 def format_percent(value) sprintf("%.2f%%", value) end |