Class: Kibo::Helpers::Info

Inherits:
Array
  • Object
show all
Defined in:
lib/kibo/helpers/info.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#<, #<=, #>, #>=, diff

Class Method Details

.build {|info| ... } ⇒ Object

Yields:

  • (info)


8
9
10
11
12
# File 'lib/kibo/helpers/info.rb', line 8

def self.build(&block)
  info = new
  yield info
  info
end


4
5
6
# File 'lib/kibo/helpers/info.rb', line 4

def self.print(out = STDOUT, &block)
  out.puts build(&block).to_s
end

Instance Method Details

#head(msg) ⇒ Object



14
15
16
# File 'lib/kibo/helpers/info.rb', line 14

def head(msg)
  push [ :head, msg ]
end

#line(msg, value) ⇒ Object



18
19
20
# File 'lib/kibo/helpers/info.rb', line 18

def line(msg, value)
  push [ :line, msg, value ]
end

#to_sObject



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
51
# File 'lib/kibo/helpers/info.rb', line 22

def to_s
  key_length = map do |kind, msg, value| 
    kind == :line ? msg.length : 0
  end.max

  key_length = 60 if key_length > 60
  
  key_format = "%#{key_length + 4}s"
  
  map do |kind, msg, value|
    case kind
    when :head
      "== #{msg} " + "=" * (100 - msg.length)
    when :line
      case value
      when [], nil then value = "<none>"
      when Array   then value = value.map(&:inspect).join(", ")
      end

      if msg == ""
        "#{key_format % msg}  #{value}"
      elsif msg.length > key_length
        msg = msg[0..20] + "..." + msg[(25 - key_length) ..-1]
        "#{key_format % msg}: #{value}"
      else
        "#{key_format % msg}: #{value}"
      end
    end
  end.join("\n")
end