Class: TTY::Table::Operation::Alignment
- Inherits:
-
Object
- Object
- TTY::Table::Operation::Alignment
- Includes:
- Coercion
- Defined in:
- lib/tty/table/operation/alignment.rb
Overview
A class representing an alignment of cell data.
Constant Summary collapse
- LEFT =
:left.freeze
- RIGHT =
:right.freeze
- CENTER =
: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
30 31 32 33 |
# File 'lib/tty/table/operation/alignment.rb', line 30 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
20 21 22 |
# File 'lib/tty/table/operation/alignment.rb', line 20 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
43 44 45 46 47 |
# File 'lib/tty/table/operation/alignment.rb', line 43 def assert_valid_type unless supported.include? type raise TypeError, "Alignment must be one of: #{supported.join(' ')}" end 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 |