Class: TTY::Table::Field Private

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

Overview

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

A class that represents a unique element in a table.

Used internally by Row to define internal structure.

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = TTY::Table::Field.new 'a1'
field.value  # => a1
field = TTY::Table::Field.new value: 'a1'
field.value  # => a1
field = TTY::Table::Field.new value: 'a1', align: :center
field.value  # => a1
field.align  # => :center


57
58
59
60
61
62
63
# File 'lib/tty/table/field.rb', line 57

def initialize(value)
  options  = extract_options(value)
  @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)

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.



39
40
41
# File 'lib/tty/table/field.rb', line 39

def align
  @align
end

#colspanObject (readonly)

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.

Number of columns this field spans. Defaults to 1.



33
34
35
# File 'lib/tty/table/field.rb', line 33

def colspan
  @colspan
end

#nameObject (readonly)

The name for the value



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

def name
  @name
end

#rowspanObject (readonly)

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.

Number of rows this field spans. Defaults to 1.



37
38
39
# File 'lib/tty/table/field.rb', line 37

def rowspan
  @rowspan
end

#valueObject

The value inside the field



16
17
18
# File 'lib/tty/table/field.rb', line 16

def value
  @value
end

#widthObject (readonly)

The field value width



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

def width
  @width
end

Instance Method Details

#charsObject

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.



121
122
123
# File 'lib/tty/table/field.rb', line 121

def chars
  value.chars
end

#extract_options(value) ⇒ Object

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.

Extract options and set value



68
69
70
71
72
73
74
75
76
77
# File 'lib/tty/table/field.rb', line 68

def extract_options(value)
  if value.class <= Hash
    options = value
    @value = options.fetch(:value)
  else
    @value = value
    options = {}
  end
  options
end

#heightInteger

Extract the number of lines this value spans

Returns:

  • (Integer)


117
118
119
# File 'lib/tty/table/field.rb', line 117

def height
  lines.size
end

#lengthInteger

If the string contains unescaped new lines then the longest token deterimines the actual field length.

Returns:

  • (Integer)


108
109
110
# File 'lib/tty/table/field.rb', line 108

def length
  (lines.max_by(&:length) || '').size
end

#linesArray[String]

Return number of lines this value spans.

A distinction is being made between escaped and non-escaped strings.

Returns:

  • (Array[String])


97
98
99
100
# File 'lib/tty/table/field.rb', line 97

def lines
  escaped = value.to_s.scan(/(\\n|\\t|\\r)/)
  escaped.empty? ? value.to_s.split(/\n/, -1) : [value.to_s]
end

#renderObject

Render value inside this field box



128
129
# File 'lib/tty/table/field.rb', line 128

def render
end

#to_sObject

Return field value



134
135
136
# File 'lib/tty/table/field.rb', line 134

def to_s
  value
end

#value_heightObject

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.



86
87
88
# File 'lib/tty/table/field.rb', line 86

def value_height
  @height
end

#value_widthObject

Return the width this field would normally have bar other contraints



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

def value_width
  @width
end