Class: Shades::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/shades/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(spacer = " ") ⇒ Formatter

Returns a new instance of Formatter.



3
4
5
# File 'lib/shades/formatter.rb', line 3

def initialize(spacer = " ")
  @spacer = spacer
end

Instance Method Details

#find_abs_max(m, events) ⇒ Object



51
52
53
# File 'lib/shades/formatter.rb', line 51

def find_abs_max(m, events)
  events.inject(Float::MIN) { |max, e| [max, e.measure(m).abs].max }
end

#find_longest_field(d, events) ⇒ Object



54
55
56
# File 'lib/shades/formatter.rb', line 54

def find_longest_field(d, events)
  events.inject(d.length) { |max, e| [max, e.dimension(d).length].max }
end

#pretty_text(out, events) ⇒ Object



18
19
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
45
46
47
48
49
50
# File 'lib/shades/formatter.rb', line 18

def pretty_text(out, events)
  if events.empty?
    return
  end
   = events[0].
  lines = []
  (events.length+1).times {|i|lines[i] = []}
  .dimensions.each do |d|
    w = find_longest_field(d, events)
    row = 1
    events.each do |e|
      v = e.dimension(d)
      lines[row] << "%-#{w}s" % v
      row += 1
    end
    lines[0] << "%-#{w}s" % d
  end
  .measures.each do |m|
    row = 1
    max_value = find_abs_max(m, events) + 1.0
    vlen = Integer(Math::log10(10*(max_value+1))) + 5
    w = [vlen, m.length].max
    events.each do |e|
      v = e.measure(m)
      lines[row] << "%#{w}.4f" % v
      row +=1 
    end
    lines[0] << "%-#{w}s" % m
  end
  lines.map{|l|l.join(@spacer)}.each do |line|
    out.puts line
  end
end

#text(out, events) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/shades/formatter.rb', line 6

def text(out, events)
  if events.empty?
    return
  end
   = events[0].
  lines = []
  out.puts "# dimensions: %s" % (.dimensions.join(@spacer))
  out.puts "# measures: %s"   % (.measures.join(@spacer))
  events.each do |e|
    out.puts e.line
  end
end