Class: TTY::Table::Field Private
- Inherits:
-
Object
- Object
- TTY::Table::Field
- Defined in:
- lib/tty/table/field.rb
Overview
Instance Attribute Summary collapse
-
#alignment ⇒ Object
readonly
The field alignment.
-
#colspan ⇒ Object
readonly
Number of columns this field spans.
-
#content ⇒ Object
The formatted value inside the field used for display.
-
#rowspan ⇒ Object
readonly
Number of rows this field spans.
-
#value ⇒ Object
The value inside the field.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare fields for equivalence of value attribute.
- #chars ⇒ Object private
-
#eql?(other) ⇒ Boolean
Compare fields for equality of value attribute.
-
#extract_options(value) ⇒ Object
private
Extract options and set value.
-
#hash ⇒ Numeric
Hash for this instance and its attributes.
-
#height ⇒ Integer
Extract the number of lines this value spans.
-
#initialize(value) ⇒ Field
constructor
private
Initialize a Field.
-
#inspect ⇒ String
Inspect this instance attributes.
-
#length ⇒ Integer
If the string contains unescaped new lines then the longest token deterimines the actual field length.
-
#lines ⇒ Array[String]
Return number of lines this value spans.
-
#reset! ⇒ Object
Reset to original value.
-
#to_s ⇒ String
Return field content.
-
#width ⇒ Object
The content width.
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
55 56 57 58 59 60 61 62 |
# File 'lib/tty/table/field.rb', line 55 def initialize(value) @value, = (value) @content = @value.to_s @width = [:width] @alignment = .fetch(:alignment, nil) @colspan = .fetch(:colspan, 1) @rowspan = .fetch(:rowspan, 1) end |
Instance Attribute Details
#alignment ⇒ Object (readonly)
The field alignment
37 38 39 |
# File 'lib/tty/table/field.rb', line 37 def alignment @alignment end |
#colspan ⇒ Object (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 |
#content ⇒ Object
The formatted value inside the field used for display
22 23 24 |
# File 'lib/tty/table/field.rb', line 22 def content @content end |
#rowspan ⇒ Object (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 |
#value ⇒ Object
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
151 152 153 |
# File 'lib/tty/table/field.rb', line 151 def ==(other) other.is_a?(self.class) && value == other.value end |
#chars ⇒ 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.
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
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 (value) if value.is_a?(Hash) = value value = .fetch(:value) else = {} end [value, ] end |
#hash ⇒ Numeric
Hash for this instance and its attributes
170 171 172 |
# File 'lib/tty/table/field.rb', line 170 def hash [self.class, value].hash end |
#height ⇒ Integer
Extract the number of lines this value spans
120 121 122 |
# File 'lib/tty/table/field.rb', line 120 def height lines.size end |
#inspect ⇒ String
Inspect this instance attributes
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 |
#length ⇒ Integer
If the string contains unescaped new lines then the longest token deterimines the actual field length.
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 |
#lines ⇒ Array[String]
Return number of lines this value spans.
A distinction is being made between escaped and non-escaped strings.
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_s ⇒ String
Return field content
133 134 135 |
# File 'lib/tty/table/field.rb', line 133 def to_s content end |
#width ⇒ Object
The content width
87 88 89 |
# File 'lib/tty/table/field.rb', line 87 def width @width || Strings::Align.display_width(@content) end |