class WhatTheGem::Stats::Metric

Attributes

color[R]
name[R]
value[R]

Public Class Methods

new(name, value, *thresholds) click to toggle source
# File lib/whatthegem/stats/meters.rb, line 31
def initialize(name, value, *thresholds)
  @name = name
  @value = value
  @color = deduce_color(*thresholds)
end

Public Instance Methods

format() click to toggle source
# File lib/whatthegem/stats/meters.rb, line 37
def format
  '%20s: %s' % [name, colorized_value]
end

Private Instance Methods

colorized_value() click to toggle source
# File lib/whatthegem/stats/meters.rb, line 43
def colorized_value
  Pastel.new.send(color, formatted_value)
end
deduce_color(red = nil, yellow = nil) click to toggle source
# File lib/whatthegem/stats/meters.rb, line 63
def deduce_color(red = nil, yellow = nil)
  return :dark if value.nil?
  return :white if !yellow # no thresholds given

  # special trick to tell "lower is better" from "higher is better" situations
  val = red.is_a?(Numeric) && red < 0 ? -value : value

  case
  when val < red then :red
  when val < yellow then :yellow
  else :green
  end
end
formatted_value() click to toggle source
# File lib/whatthegem/stats/meters.rb, line 47
def formatted_value
  case value
  when nil
    '—'
  when String
    value
  when Numeric
    # 100000 => 100,000
    value.to_s.chars.reverse.each_slice(3).to_a.map(&:join).join(',').reverse
  when Date, Time
    I.ago_text(value)
  else
    fail ArgumentError, "Unformattable #{value.inspect}"
  end
end