Module: Collimator::BarGraph

Defined in:
lib/collimator/bar_graph.rb

Class Method Summary collapse

Class Method Details

.clear_allObject



8
9
10
11
12
# File 'lib/collimator/bar_graph.rb', line 8

def self.clear_all
  @bar_character = "\u2588"
  @data = []
  @options = {}
end

.clear_dataObject



14
15
16
# File 'lib/collimator/bar_graph.rb', line 14

def self.clear_data
  @data = []
end

.dataObject



26
27
28
# File 'lib/collimator/bar_graph.rb', line 26

def self.data
  @data
end

.data=(data) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/collimator/bar_graph.rb', line 18

def self.data=(data)
  if data.is_a?(Array)
    @data = data
  elsif data.is_a?(Hash)
    @data << data
  end
end

.optionsObject



30
31
32
# File 'lib/collimator/bar_graph.rb', line 30

def self.options
  @options
end

.options=(options) ⇒ Object



34
35
36
# File 'lib/collimator/bar_graph.rb', line 34

def self.options=(options)
  @options.merge!(options)
end

.plot(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/collimator/bar_graph.rb', line 38

def self.plot(options = {})
  self.options = options
  header_width = max_header_length

  @data.each do |data|
    value = data[:value].to_i
    header = data[:name].ljust(header_width)
    bar = @bar_character * value

    color = data.fetch(:color, 'cyan')

    begin
      bar = bar.send(color)
    rescue NoMethodError
      bar = bar
    end

    value = @options[:show_values] ? "#{value.to_s.rjust(3)}" : ''
    full_output = "  #{header} #{value} #{bar}"

    puts full_output
  end

end