Class: TermUtils::Tab::Column
- Inherits:
-
Object
- Object
- TermUtils::Tab::Column
- Defined in:
- lib/term_utils/tab.rb
Overview
Represents a table column.
Instance Attribute Summary collapse
-
#align ⇒ Symbol
`:left`, `:right`.
- #ellipsis ⇒ String
- #fixed ⇒ Boolean
- #format ⇒ Proc, ...
- #header ⇒ TermUtils::Tab::Header
- #id ⇒ Symbol
- #index ⇒ Integer
- #width ⇒ Integer
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Column
constructor
A new instance of Column.
-
#render_data(val) ⇒ Object
Renders a given value.
-
#render_header(val) ⇒ Object
Renders a given header.
-
#validate ⇒ nil
Validates the column represented by this one.
Constructor Details
#initialize(opts = {}) ⇒ Column
Returns a new instance of Column.
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
#align ⇒ Symbol
Returns `:left`, `:right`.
296 297 298 |
# File 'lib/term_utils/tab.rb', line 296 def align @align end |
#ellipsis ⇒ String
300 301 302 |
# File 'lib/term_utils/tab.rb', line 300 def ellipsis @ellipsis end |
#fixed ⇒ Boolean
298 299 300 |
# File 'lib/term_utils/tab.rb', line 298 def fixed @fixed end |
#format ⇒ Proc, ...
302 303 304 |
# File 'lib/term_utils/tab.rb', line 302 def format @format end |
#header ⇒ TermUtils::Tab::Header
304 305 306 |
# File 'lib/term_utils/tab.rb', line 304 def header @header end |
#id ⇒ Symbol
290 291 292 |
# File 'lib/term_utils/tab.rb', line 290 def id @id end |
#index ⇒ Integer
292 293 294 |
# File 'lib/term_utils/tab.rb', line 292 def index @index end |
#width ⇒ 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]
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]
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 |
#validate ⇒ nil
Validates the column represented by this one.
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 |