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)
  • :offset (Integer)
  • :column_defaults (Hash)


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/term_utils/tab.rb', line 120

def initialize(opts = {})
  opts = TermUtils::Tab.init_table_props.merge(opts)
  @id = opts.fetch(:id, nil)
  @offset = opts.fetch(:offset)
  @column_separator_width = opts.fetch(:column_separator_width)
  if opts.has_key? :column_defaults
    @column_defaults = opts[:column_defaults].dup
  else
    @column_defaults = TermUtils::Tab.default_column_props
  end
  @columns = []
end

Instance Attribute Details

#column_defaultsHash

Returns ‘:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.

Returns:

  • (Hash)

    ‘:width`, `:align`, `:fixed`, `:ellipsis`, `:format`.



113
114
115
# File 'lib/term_utils/tab.rb', line 113

def column_defaults
  @column_defaults
end

#column_separator_widthInteger

Returns:

  • (Integer)


111
112
113
# File 'lib/term_utils/tab.rb', line 111

def column_separator_width
  @column_separator_width
end

#columnsArray<Tab::Column>

Returns:



115
116
117
# File 'lib/term_utils/tab.rb', line 115

def columns
  @columns
end

#idSymbol

Returns:

  • (Symbol)


107
108
109
# File 'lib/term_utils/tab.rb', line 107

def id
  @id
end

#offsetInteger

Returns:

  • (Integer)


109
110
111
# File 'lib/term_utils/tab.rb', line 109

def offset
  @offset
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)
  • :align (Symbol)
  • :fixed (Boolean)
  • :ellipsis (String)
  • :format (Proc, String, nil)

Returns:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/term_utils/tab.rb', line 156

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(@column_defaults.merge(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:



174
175
176
# File 'lib/term_utils/tab.rb', line 174

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

Prints a data row.

Parameters:

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

Options Hash (opts):

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

Returns:

  • (nil)

Raises:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/term_utils/tab.rb', line 225

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 TermUtils::Tab::TableError, "wrong values (not array)" unless vals.is_a? Array
  offset = opts.fetch(:offset)
  column_separator_width = opts.fetch(:column_separator_width)
  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 (#puts)
  • values (Array<Object>, Hash<Symbol, Object>) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (nil)

Raises:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/term_utils/tab.rb', line 196

def print_header(io, values = nil, opts = {})
  vals = values
  if values.nil?
    vals = @columns.map { |col| col.header.title }
  elsif values.is_a? Hash
    vals = []
    @columns.each do |col|
      vals << values[col.id]
    end
  end
  raise TermUtils::Tab::TableError, "wrong values (not array)" unless vals.is_a? Array
  offset = opts.fetch(:offset)
  column_separator_width = opts.fetch(:column_separator_width)
  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 (#puts)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:

  • (nil)


250
251
252
253
254
255
256
257
258
259
260
# File 'lib/term_utils/tab.rb', line 250

def print_separator(io, opts = {})
  offset = opts.fetch(:offset)
  column_separator_width = opts.fetch(:column_separator_width)
  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 (#puts)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

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

Returns:



183
184
185
186
187
# File 'lib/term_utils/tab.rb', line 183

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

#propsHash

Returns the properties of this one.

Returns:

  • (Hash)


134
135
136
# File 'lib/term_utils/tab.rb', line 134

def props
  {:offset => @offset, :column_separator_width => @column_separator_width}
end

#set_column_defaults(opts = {}) ⇒ Object

Sets column default properties.

Parameters:

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

Options Hash (opts):

  • :width (Integer)
  • :align (Symbol)
  • :fixed (Boolean)
  • :ellipsis (String)
  • :format (Proc, String, nil)


144
145
146
# File 'lib/term_utils/tab.rb', line 144

def set_column_defaults(opts = {})
  TermUtils::Tab.assign_column_props(@column_defaults, opts)
end

#titlesHash<Symbol, String>

Returns column titles.

Returns:

  • (Hash<Symbol, String>)


263
264
265
266
267
268
269
# File 'lib/term_utils/tab.rb', line 263

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