Class: Instant::LogCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/instant/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(keys = []) ⇒ LogCollector

Returns a new instance of LogCollector.



8
9
10
11
# File 'lib/instant/context.rb', line 8

def initialize(keys=[])
  @logs = {}
  keys.each {|k| @logs[k] = [] } if keys
end

Instance Method Details

#append(key, value) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/instant/context.rb', line 13

def append(key, value)
  @logs[key] ||= []

  if @logs[key].length + 1 > 10
    raise ::Instant::LoopTooDeepError.new("Loop too much")
  end

  @logs[key] << value
end

#fill_emptyObject



23
24
25
26
27
28
29
# File 'lib/instant/context.rb', line 23

def fill_empty
  max_length = @logs.values.collect{|v| v.length}.max
  fill_keys  = @logs.keys.select {|k| @logs[k].length < max_length }
  fill_keys.each do |key|
    @logs[key] << ""
  end
end

#to_sObject



31
32
33
34
35
36
# File 'lib/instant/context.rb', line 31

def to_s
  @logs.collect do |key, values|
    value = values.collect {|v| v.to_s.center(5) }.join("|")
    "#{key.to_s.ljust(8)} = #{value} "
  end.join("\n")
end