class TermUtils::Tab::Column

Represents a table column.

Attributes

align[RW]

@return [Symbol] `:left`, `:right`.

ellipsis[RW]

@return [String]

fixed[RW]

@return [Boolean]

format[RW]

@return [Proc, String, nil]

header[RW]

@return [TermUtils::Tab::Header]

id[RW]

@return [Symbol]

index[RW]

@return [Integer]

width[RW]

@return [Integer]

Public Class Methods

new(opts = {}) click to toggle source

@param opts [Hash] @option opts [Symbol] :id @option opts [Integer] :index @option opts [Integer] :width @option opts [Symbol] :align @option opts [Boolean] :fixed @option opts [String] :ellipsis @option opts [Proc, String, nil] :format

# 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

Public Instance Methods

render_data(val) click to toggle source

Renders a given value. @param val [Object] return [String]

# 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) click to toggle source

Renders a given header. @param val [Object] return [String]

# 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
validate() click to toggle source

Validates the column represented by this one. @return [nil] @raise [TermUtils::Tab::TableError]

# 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