Class: TTY::Table::Operation::Alignment Private
- Inherits:
-
Object
- Object
- TTY::Table::Operation::Alignment
- Includes:
- Coercion
- Defined in:
- lib/tty/table/operation/alignment.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 representing an alignment of cell data.
Constant Summary collapse
- LEFT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
:left.freeze
- RIGHT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
:right.freeze
- CENTER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
:center.freeze
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
private
Hold the type of alignment.
Instance Method Summary collapse
-
#assert_valid_type ⇒ undefined
private
Assert the type is valid.
-
#format(field, column_width, space = '') ⇒ String
Format field with a given alignment.
-
#initialize(type = nil) ⇒ self
constructor
private
Initialize an Alignment.
-
#supported ⇒ Array
private
List supported alignment types.
Methods included from Coercion
Constructor Details
#initialize(type = nil) ⇒ self
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 an Alignment
31 32 33 34 |
# File 'lib/tty/table/operation/alignment.rb', line 31 def initialize(type = nil) @type = coerce_to((type || LEFT), Symbol, :to_sym) assert_valid_type end |
Instance Attribute Details
#type ⇒ Object (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.
Hold the type of alignment
21 22 23 |
# File 'lib/tty/table/operation/alignment.rb', line 21 def type @type end |
Instance Method Details
#assert_valid_type ⇒ undefined
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.
Assert the type is valid
44 45 46 47 |
# File 'lib/tty/table/operation/alignment.rb', line 44 def assert_valid_type return if supported.include?(type) fail TypeError, "Alignment must be one of: #{supported.join(' ')}" end |
#format(field, column_width, space = '') ⇒ String
Format field with a given alignment
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tty/table/operation/alignment.rb', line 70 def format(field, column_width, space = '') case type when :left "%-#{column_width}s#{space}" % field.to_s when :right "%#{column_width}s#{space}" % field.to_s when :center center_align field, column_width, space end end |
#supported ⇒ Array
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.
List supported alignment types
55 56 57 |
# File 'lib/tty/table/operation/alignment.rb', line 55 def supported [LEFT, RIGHT, CENTER] end |