Class: TermUtils::Tab::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/term_utils/tab.rb

Overview

Represents a table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Table

Returns a new instance of Table.

Parameters:

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

Options Hash (opts):

  • :id (Symbol)


27
28
29
30
# File 'lib/term_utils/tab.rb', line 27

def initialize(opts = {})
  @id = opts.fetch(:id, nil)
  @columns = []
end

Instance Attribute Details

#columnsArray<Tab::Column>

Returns:



24
25
26
# File 'lib/term_utils/tab.rb', line 24

def columns
  @columns
end

#idSymbol

Returns:

  • (Symbol)


22
23
24
# File 'lib/term_utils/tab.rb', line 22

def id
  @id
end

Instance Method Details

#define_column(id, opts = {}, &block) ⇒ Tab::Column

Defines a column.

Parameters:

  • id (Symbol)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :width (Integer)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/term_utils/tab.rb', line 36

def define_column(id, opts = {}, &block)
  col = @columns.find { |c| c.id == id }
  if col
    block.call(col) if block
    col.validate
  else
    opts[:id] = id
    opts[:index] = @columns.length
    col = Column.new(opts)
    block.call(col) if block
    col.validate
    @columns << col
  end
  col
end

#find_column(id) ⇒ Tab::Column?

Finds a column.

Parameters:

  • id (Symbol)

Returns:



54
55
56
# File 'lib/term_utils/tab.rb', line 54

def find_column(id)
  @columns.find { |c| c.id == id }
end

Prints a data row.

Parameters:

  • io (IO)
  • values (Array<Object>, Hash<Symbol, Object>)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)

Returns:

  • (nil)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/term_utils/tab.rb', line 103

def print_data(io, values, opts = {})
  vals = values
  if values.is_a? Hash
    vals = []
    @columns.each do |col|
      vals << values[col.id]
    end
  end
  raise "wrong values (not array)" unless vals.is_a? Array
  offset = opts.fetch(:offset, 0)
  column_separator_width = opts.fetch(:column_separator_width, 2)
  sb = StringIO.new
  sb << " " * offset if offset > 0
  @columns.each do |col|
    sb << " " * column_separator_width if col.index > 0
    sb << col.render_data(vals[col.index])
  end
  io.puts sb.string
end

Prints a header row.

Parameters:

  • io (IO)
  • values (Array<Object>, Hash<Symbol, Object>) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)

Returns:

  • (nil)


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

def print_header(io, values = nil, opts = {})
  vals = values
  if values.nil?
    vals = @columns.map { |col| col.id.to_s }
  elsif values.is_a? Hash
    vals = []
    @columns.each do |col|
      vals << values[col.id]
    end
  end
  raise "wrong values (not array)" unless vals.is_a? Array
  offset = opts.fetch(:offset, 0)
  column_separator_width = opts.fetch(:column_separator_width, 2)
  sb = StringIO.new
  sb << " " * offset if offset > 0
  @columns.each do |col|
    sb << " " * column_separator_width if col.index > 0
    sb << col.render_header(vals[col.index])
  end
  io.puts sb.string
end

Prints a separator row.

Parameters:

  • io (IO)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)

Returns:

  • (nil)


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/term_utils/tab.rb', line 128

def print_separator(io, opts = {})
  offset = opts.fetch(:offset, 0)
  column_separator_width = opts.fetch(:column_separator_width, 2)
  sb = StringIO.new
  sb << " " * offset if offset > 0
  @columns.each do |col|
    sb << " " * column_separator_width if col.index > 0
    sb << "-" * col.width
  end
  io.puts sb.string
end

#printer(io, opts = {}, &block) ⇒ Tab::Printer

Creates a new table printer.

Parameters:

  • io (IO)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :offset (Integer)
  • :column_separator_width (Integer)

Returns:



63
64
65
66
67
# File 'lib/term_utils/tab.rb', line 63

def printer(io, opts = {}, &block)
  ptr = Printer.new(self, io, opts)
  block.call(ptr) if block
  ptr
end

#titlesHash<Symbol, String>

Returns column titles.

Returns:

  • (Hash<Symbol, String>)


141
142
143
144
145
146
147
# File 'lib/term_utils/tab.rb', line 141

def titles
  h = {}
  @columns.each do |col|
    h[col.id] = col.id.to_s
  end
  h
end