Class: TTY::Table::Field

Inherits:
Object
  • Object
show all
Includes:
Equatable
Defined in:
lib/tty/table/field.rb

Overview

A class that represents a unique element in a table.

Instance Attribute Summary collapse

Attributes included from Equatable

#comparison_attrs

Instance Method Summary collapse

Methods included from Equatable

#attr_reader, included, #inherited

Constructor Details

#initialize(value) ⇒ Field

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Field

Examples:

field = new TTY::Table::Field 'a1'
field.value  # => a1

field = new TTY::Table::Field {:value => 'a1'}
field.value  # => a1

field = new TTY::Table::Field {:value => 'a1', :align => :center}
field.value  # => a1
field.align  # => :center


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tty/table/field.rb', line 47

def initialize(value)
  if value.class <= Hash
    options = value
    @value = options.fetch(:value)
  else
    @value = value
    options = {}
  end
  @width   = options.fetch(:width) { @value.to_s.size }
  @align   = options.fetch(:align) { nil }
  @colspan = options.fetch(:colspan) { 1 }
  @rowspan = options.fetch(:rowspan) { 1 }
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



31
32
33
# File 'lib/tty/table/field.rb', line 31

def align
  @align
end

#colspanObject (readonly)

Number of columns this field spans. Defaults to 1.



25
26
27
# File 'lib/tty/table/field.rb', line 25

def colspan
  @colspan
end

#rowspanObject (readonly)

Number of rows this field spans. Defaults to 1.



29
30
31
# File 'lib/tty/table/field.rb', line 29

def rowspan
  @rowspan
end

#valueObject

The value inside the field



13
14
15
# File 'lib/tty/table/field.rb', line 13

def value
  @value
end

#widthObject (readonly)

The field value width



21
22
23
# File 'lib/tty/table/field.rb', line 21

def width
  @width
end

Instance Method Details

#heightObject



72
73
74
# File 'lib/tty/table/field.rb', line 72

def height
  lines.size
end

#linesObject

Return number of lines this value spans



78
79
80
# File 'lib/tty/table/field.rb', line 78

def lines
  value.to_s.split(/\n/)
end

#longest_lineObject



82
83
84
# File 'lib/tty/table/field.rb', line 82

def longest_line
  lines.max_by(&:length).size
end

#renderObject

Render value inside this field box



89
90
# File 'lib/tty/table/field.rb', line 89

def render
end

#to_sObject

Return field value



95
96
97
# File 'lib/tty/table/field.rb', line 95

def to_s
  value
end

#value_heightObject



68
69
70
# File 'lib/tty/table/field.rb', line 68

def value_height
  @height
end

#value_widthObject

Return the width this field would normally have bar other contraints



64
65
66
# File 'lib/tty/table/field.rb', line 64

def value_width
  @width
end