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)


297
298
299
300
301
302
303
304
305
306
# File 'lib/term_utils/tab.rb', line 297

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`.



280
281
282
# File 'lib/term_utils/tab.rb', line 280

def align
  @align
end

#ellipsisString

Returns:

  • (String)


284
285
286
# File 'lib/term_utils/tab.rb', line 284

def ellipsis
  @ellipsis
end

#fixedBoolean

Returns:

  • (Boolean)


282
283
284
# File 'lib/term_utils/tab.rb', line 282

def fixed
  @fixed
end

#formatProc, ...

Returns:

  • (Proc, String, nil)


286
287
288
# File 'lib/term_utils/tab.rb', line 286

def format
  @format
end

#headerTermUtils::Tab::Header



288
289
290
# File 'lib/term_utils/tab.rb', line 288

def header
  @header
end

#idSymbol

Returns:

  • (Symbol)


274
275
276
# File 'lib/term_utils/tab.rb', line 274

def id
  @id
end

#indexInteger

Returns:

  • (Integer)


276
277
278
# File 'lib/term_utils/tab.rb', line 276

def index
  @index
end

#widthInteger

Returns:

  • (Integer)


278
279
280
# File 'lib/term_utils/tab.rb', line 278

def width
  @width
end

Instance Method Details

#render_data(val) ⇒ Object

Renders a given value. return [String]

Parameters:

  • val (Object)


331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/term_utils/tab.rb', line 331

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)


324
325
326
327
# File 'lib/term_utils/tab.rb', line 324

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:



310
311
312
313
314
315
316
317
318
319
320
# File 'lib/term_utils/tab.rb', line 310

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 < 0
  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