Class: Bogo::Ui::Table

Inherits:
Object
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/bogo/ui/table.rb

Overview

Table output helper

Defined Under Namespace

Classes: BufferedRow, BufferedTable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui, inst = nil) { ... } ⇒ self

Create a new instance

Parameters:

Yields:

  • table content



23
24
25
26
27
28
29
# File 'lib/bogo/ui/table.rb', line 23

def initialize(ui, inst=nil, &block)
  @proxy_to = inst
  @ui = ui
  @base = block
  @content = []
  @printed_lines = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m_name, *args, &block) ⇒ Object

If proxy instance is provided, forward if possible



32
33
34
35
36
37
38
# File 'lib/bogo/ui/table.rb', line 32

def method_missing(m_name, *args, &block)
  if(proxy_to && proxy_to.respond_to?(m_name, true))
    proxy_to.send(m_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#proxy_toObject (readonly)

Returns:

  • (Object)


16
17
18
# File 'lib/bogo/ui/table.rb', line 16

def proxy_to
  @proxy_to
end

#table(options = {}) ⇒ self (readonly)

Override to provide buffered support

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (self)


14
15
16
# File 'lib/bogo/ui/table.rb', line 14

def table
  @table
end

#uiBogo::Ui (readonly)

Returns:



12
13
14
# File 'lib/bogo/ui/table.rb', line 12

def ui
  @ui
end

Instance Method Details

#displayself

Note:

can be called multiple times to print table updates

Output table to defined UI

Returns:

  • (self)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bogo/ui/table.rb', line 76

def display
  # init the table
  instance_exec(&@base)
  # load the table
  @content.each do |tblock|
    instance_exec(&tblock)
  end
  @table.output
  @table.buffer.rewind
  output = @table.buffer.read.split("\n")
  output = output.find_all do |line|
    !@printed_lines.include?(
      Digest::SHA256.hexdigest(line.gsub(/\s/, ''))
    )
  end
  @printed_lines.concat(
    output.map{|l| Digest::SHA256.hexdigest(l.gsub(/\s/, '')) }
  )
  ui.puts output.join("\n") unless output.empty?
  self
end

#row(options = {}) ⇒ self

Override to provide buffered support

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (self)


64
65
66
67
68
69
70
# File 'lib/bogo/ui/table.rb', line 64

def row(options={})
  options[:encoding] ||= @table.encoding
  @row = BufferedRow.new(options.merge(:buffer => @table.buffer))
  yield
  @table.add(@row)
  self
end

#update { ... } ⇒ self

Update the table content

Yields:

  • table content

Returns:

  • (self)


44
45
46
47
# File 'lib/bogo/ui/table.rb', line 44

def update(&block)
  @content << block
  self
end