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
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
#align ⇒ Symbol
Returns `:left`, `:right`.
280 281 282 |
# File 'lib/term_utils/tab.rb', line 280 def align @align end |
#ellipsis ⇒ String
284 285 286 |
# File 'lib/term_utils/tab.rb', line 284 def ellipsis @ellipsis end |
#fixed ⇒ Boolean
282 283 284 |
# File 'lib/term_utils/tab.rb', line 282 def fixed @fixed end |
#format ⇒ Proc, ...
286 287 288 |
# File 'lib/term_utils/tab.rb', line 286 def format @format end |
#header ⇒ TermUtils::Tab::Header
288 289 290 |
# File 'lib/term_utils/tab.rb', line 288 def header @header end |
#id ⇒ Symbol
274 275 276 |
# File 'lib/term_utils/tab.rb', line 274 def id @id end |
#index ⇒ Integer
276 277 278 |
# File 'lib/term_utils/tab.rb', line 276 def index @index end |
#width ⇒ 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]
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]
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 |
#validate ⇒ nil
Validates the column represented by this one.
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 |