Class: TTY::Table::Field Private

Inherits:
Object
  • Object
show all
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 Header and 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", alignment: :center
field.value     # => a1
field.alignment # => :center


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

def initialize(value)
  @value, options = extract_options(value)
  @content = @value.to_s
  @width   = options[:width]
  @alignment = options.fetch(:alignment, nil)
  @colspan = options.fetch(:colspan, 1)
  @rowspan = options.fetch(:rowspan, 1)
end

Instance Attribute Details

#alignmentObject (readonly)

The field alignment



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

def alignment
  @alignment
end

#colspanObject (readonly)

Number of columns this field spans. Defaults to 1.



27
28
29
# File 'lib/tty/table/field.rb', line 27

def colspan
  @colspan
end

#contentObject

The formatted value inside the field used for display



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

def content
  @content
end

#rowspanObject (readonly)

Number of rows this field spans. Defaults to 1.



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

def rowspan
  @rowspan
end

#valueObject

The value inside the field



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Compare fields for equivalence of value attribute

Returns:

  • (Boolean)


151
152
153
# File 'lib/tty/table/field.rb', line 151

def ==(other)
  other.is_a?(self.class) && value == other.value
end

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



124
125
126
# File 'lib/tty/table/field.rb', line 124

def chars
  content.chars
end

#eql?(other) ⇒ Boolean

Compare fields for equality of value attribute

Returns:

  • (Boolean)


142
143
144
# File 'lib/tty/table/field.rb', line 142

def eql?(other)
  instance_of?(other.class) && value.eql?(other.value)
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



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

def extract_options(value)
  if value.is_a?(Hash)
    options = value
    value = options.fetch(:value)
  else
    options = {}
  end
  [value, options]
end

#hashNumeric

Hash for this instance and its attributes

Returns:

  • (Numeric)


170
171
172
# File 'lib/tty/table/field.rb', line 170

def hash
  [self.class, value].hash
end

#heightInteger

Extract the number of lines this value spans

Returns:

  • (Integer)


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

def height
  lines.size
end

#inspectString

Inspect this instance attributes

Returns:

  • (String)


160
161
162
163
# File 'lib/tty/table/field.rb', line 160

def inspect
  "#<#{self.class.name} value=#{value.inspect} " \
    "rowspan=#{rowspan.inspect} colspan=#{colspan.inspect}>"
end

#lengthInteger

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

Returns:

  • (Integer)


109
110
111
112
113
# File 'lib/tty/table/field.rb', line 109

def length
  (lines.map do |line|
    Strings::Align.display_width(line)
  end << 0).max
end

#linesArray[String]

Return number of lines this value spans.

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

Returns:

  • (Array[String])


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

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

#reset!Object

Reset to original value



80
81
82
# File 'lib/tty/table/field.rb', line 80

def reset!
  @content = @value.to_s
end

#to_sString

Return field content

Returns:

  • (String)


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

def to_s
  content
end

#widthObject

The content width



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

def width
  @width || Strings::Align.display_width(@content)
end