Module: Yummi::Colorizers

Defined in:
lib/yummi/colorizers.rb

Overview

A module with useful colorizers

Defined Under Namespace

Classes: PatternColorizer, PercentageColorizer, StripeColorizer

Class Method Summary collapse

Class Method Details

.boolean(params = {}) ⇒ Object

A colorizer for boolean values.

Parameters:

- if_true: color used if the value is true (defaults to green)
- if_false: color used if the value is false (defaults to yellow)


125
126
127
128
129
130
131
132
133
134
# File 'lib/yummi/colorizers.rb', line 125

def self.boolean params = {}
  Yummi::to_colorize do |ctx|
    value = ctx.value
    if value.to_s.downcase == 'true'
      (params[:if_true] or :green)
    else
      (params[:if_false] or :yellow)
    end
  end
end

.join(*colorizers) ⇒ Object

Joins the given colorizers to work as one



76
77
78
79
80
# File 'lib/yummi/colorizers.rb', line 76

def self.join *colorizers
  join = Yummi::GroupedComponent::new
  colorizers.each { |c| join << c }
  join.extend Colorizer
end

.numeric(params) ⇒ Object

A colorizer for numeric values

Hash Args

:negative => color to use when value is negative :zero => color to use when value is zero :positive => color to use when value is positive :any => color to use for any value without specified color



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/yummi/colorizers.rb', line 176

def self.numeric params
  Yummi::to_format do |ctx|
    value = ctx.value
    negative = (params[:negative] or params[:any])
    positive = (params[:positive] or params[:any])
    zero = (params[:zero] or params[:any])
    if negative and value < 0
      negative
    elsif positive and value > 0
      positive
    elsif zero and value == 0
      zero
    end
  end
end

.pattern(mappings) ⇒ Object

Returns a new instance of #PatternColorizer



88
89
90
# File 'lib/yummi/colorizers.rb', line 88

def self.pattern mappings
  PatternColorizer::new(mappings)
end

.percentage(params) ⇒ Object



162
163
164
# File 'lib/yummi/colorizers.rb', line 162

def self.percentage params
  PercentageColorizer::new params
end

.stripe(*colors) ⇒ Object

Returns a new instance of #StripeColorizer



83
84
85
# File 'lib/yummi/colorizers.rb', line 83

def self.stripe *colors
  StripeColorizer::new(*colors)
end

.threshold(params) ⇒ Object

A colorizer that uses a set of minimum values to use a color.

Parameters:

- MINIMUM_VALUE: COLOR_TO_USE


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/yummi/colorizers.rb', line 142

def self.threshold params
  params = params.dup
  mode = (params.delete(:mode) or :min)
  colorizer = lambda do |ctx|
    value = ctx.value
    case mode.to_sym
    when :min
      params.sort.reverse_each do |limit, color|
        return color if value >= limit
      end
    when :max
      params.sort.each do |limit, color|
        return color if value <= limit
      end
    end
    nil
  end
  Yummi::to_colorize(&colorizer)
end

.when(params) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/yummi/colorizers.rb', line 101

def self.when params
  Yummi::to_colorize do |ctx|
    value = ctx.value
    color = params[value]
    return color if color
    if value.respond_to? :to_sym
      color = params[value.to_sym]
    end
    unless color
      params.each do |k, v|
        return v if k.to_s == value.to_s
      end
    end
    color
  end
end

.with(color) ⇒ Object

A colorizer that uses the given color.



95
96
97
98
99
# File 'lib/yummi/colorizers.rb', line 95

def self.with color
  Yummi::to_colorize do |ctx|
    color
  end
end