Class: TermUtils::Tab::Column

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

Overview

Represents a table column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Column

Returns a new instance of Column.

Parameters:

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

Options Hash (opts):

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


314
315
316
317
318
319
320
321
322
323
# File 'lib/term_utils/tab.rb', line 314

def initialize(opts = {})
  @id = opts.fetch(:id)
  @index = opts.fetch(:index)
  @width = opts.fetch(:width, 8)
  @align = opts.fetch(:align, :left)
  @fixed = opts.fetch(:fixed, false)
  @ellipsis = opts.fetch(:ellipsis, '?')
  @format = opts.fetch(:format, nil)
  @header = TermUtils::Tab::Header.new(title: @id.to_s, align: @align)
end

Instance Attribute Details

#alignSymbol

Returns `:left`, `:right`.

Returns:

  • (Symbol)

    `:left`, `:right`.



296
297
298
# File 'lib/term_utils/tab.rb', line 296

def align
  @align
end

#ellipsisString

Returns:

  • (String)


300
301
302
# File 'lib/term_utils/tab.rb', line 300

def ellipsis
  @ellipsis
end

#fixedBoolean

Returns:

  • (Boolean)


298
299
300
# File 'lib/term_utils/tab.rb', line 298

def fixed
  @fixed
end

#formatProc, ...

Returns:

  • (Proc, String, nil)


302
303
304
# File 'lib/term_utils/tab.rb', line 302

def format
  @format
end

#headerTermUtils::Tab::Header



304
305
306
# File 'lib/term_utils/tab.rb', line 304

def header
  @header
end

#idSymbol

Returns:

  • (Symbol)


290
291
292
# File 'lib/term_utils/tab.rb', line 290

def id
  @id
end

#indexInteger

Returns:

  • (Integer)


292
293
294
# File 'lib/term_utils/tab.rb', line 292

def index
  @index
end

#widthInteger

Returns:

  • (Integer)


294
295
296
# File 'lib/term_utils/tab.rb', line 294

def width
  @width
end

Instance Method Details

#render_data(val) ⇒ Object

Renders a given value. return [String]

Parameters:

  • val (Object)


352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/term_utils/tab.rb', line 352

def render_data(val)
  src = val
  if val
    if @format.is_a? Proc
      src = @format.call(val)
    elsif @format.is_a? String
      src = @format % val
    end
  end
  src = src.is_a?(String) ? src : src.to_s
  TermUtils::Tab.align_cut(src, @align, @fixed, @width, @ellipsis)
end

#render_header(val) ⇒ Object

Renders a given header. return [String]

Parameters:

  • val (Object)


344
345
346
347
# File 'lib/term_utils/tab.rb', line 344

def render_header(val)
  src = val.is_a?(String) ? val : val.to_s
  TermUtils::Tab.align_cut(src, @header.align, @fixed, @width, @ellipsis)
end

#validatenil

Validates the column represented by this one.

Returns:

  • (nil)

Raises:



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/term_utils/tab.rb', line 328

def validate
  raise TermUtils::Tab::TableError, 'missing column id (nil)' if @id.nil?
  raise TermUtils::Tab::TableError, 'missing column index (nil)' if @index.nil?
  raise TermUtils::Tab::TableError, 'wrong column index (not integer)' unless @index.is_a? Integer
  raise TermUtils::Tab::TableError, 'wrong column index (not >= 0)' if @index.negative?
  raise TermUtils::Tab::TableError, 'missing column width (nil)' if @width.nil?
  raise TermUtils::Tab::TableError, 'wrong column width (not integer)' unless @width.is_a? Integer
  raise TermUtils::Tab::TableError, 'wrong column width (not > 0)' if @width <= 0
  raise TermUtils::Tab::TableError, 'wrong column align (not :left or :right)' unless %i[left right].index(@align)

  @header.validate
end