class AnyGood::Metric
Attributes
color[R]
name[R]
value[R]
Public Class Methods
new(name, value, *thresholds)
click to toggle source
# File lib/any_good/meters.rb, line 24 def initialize(name, value, *thresholds) @name = name @value = value @color = deduce_color(*thresholds) end
Public Instance Methods
format()
click to toggle source
# File lib/any_good/meters.rb, line 30 def format '%20s: %s' % [name, colorized_value] end
Private Instance Methods
colorized_value()
click to toggle source
# File lib/any_good/meters.rb, line 36 def colorized_value Pastel.new.send(color, formatted_value) end
deduce_color(red = nil, yellow = nil)
click to toggle source
# File lib/any_good/meters.rb, line 57 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 if val < red :red elsif val < yellow :yellow else :green end end
formatted_value()
click to toggle source
# File lib/any_good/meters.rb, line 40 def formatted_value case value when nil '—' when String value when Numeric value.to_s.chars.reverse.each_slice(3).to_a.map(&:join).join(',').reverse when Date, Time diff = TimeMath.measure(value, Time.now) unit, num = diff.detect { |_, v| !v.zero? } "#{num} #{unit} ago" else fail ArgumentError, "Unformattable #{value.inspect}" end end