Class: Alf::Renderer::Text::Cell

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/alf/renderer/text.rb

Instance Method Summary collapse

Methods included from Utils

#max

Constructor Details

#initialize(value, options = {}) ⇒ Cell


22
23
24
25
# File 'lib/alf/renderer/text.rb', line 22

def initialize(value, options = {})
  @value = value
  @options = options
end

Instance Method Details

#array_rendering(value) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/alf/renderer/text.rb', line 64

def array_rendering(value)
  if TupleLike===value.first
    Text.render(value, "")
  elsif value.empty?
    "[]"
  else
    values = value.map{|x| Cell.new(x).text_rendering}
    if values.inject(0){|memo,s| memo + s.size} < 20
      "[" + values.join(", ") + "]"
    else
      "[" + values.join(",\n ") + "]"
    end
  end
end

#min_widthObject


27
28
29
30
31
# File 'lib/alf/renderer/text.rb', line 27

def min_width
  @min_width ||= rendering_lines.inject(0) do |maxl,line|
    max(maxl,line.size)
  end
end

#rendering_lines(size = nil) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/alf/renderer/text.rb', line 33

def rendering_lines(size = nil)
  if size.nil?
    text_rendering.split(/\n/)
  elsif @value.is_a?(Numeric)
    rendering_lines(nil).map{|l| "%#{size}s" % l}
  else
    rendering_lines(nil).map{|l| "%-#{size}s" % l}
  end
end

#text_renderingObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alf/renderer/text.rb', line 43

def text_rendering
  @text_rendering ||= case (value = @value)
    when NilClass
      "[nil]"
    when Symbol
      value.inspect
    when Float
      (@options[:float_format] || "%.3f") % value
    when Hash
      value.inspect
    when RelationLike
      Text.render(value, "", @options)
    when Array
      array_rendering(value)
    when Time, DateTime
      value.to_s
    else
      value.to_s
  end
end