Class: Verse::Alignment
- Inherits:
-
Object
- Object
- Verse::Alignment
- Defined in:
- lib/verse/alignment.rb
Overview
A class responsible for text alignment
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#fill ⇒ Object
readonly
Returns the value of attribute fill.
Class Method Summary collapse
-
.align(text, width, direction, options) ⇒ Object
Align a text to a given direction with the width.
Instance Method Summary collapse
-
#align(width, direction = :left, options = {}) ⇒ Object
Aligns text within the width.
-
#center(width, options = {}) ⇒ String
Centers text within the width.
-
#initialize(text, options = {}) ⇒ Alignment
constructor
Initialize an Alignment.
-
#left(width, options = {}) ⇒ String
Aligns text to the left.
-
#right(width, options = {}) ⇒ String
Aligns text to the right.
Constructor Details
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
8 9 10 |
# File 'lib/verse/alignment.rb', line 8 def direction @direction end |
#fill ⇒ Object (readonly)
Returns the value of attribute fill.
6 7 8 |
# File 'lib/verse/alignment.rb', line 6 def fill @fill end |
Class Method Details
.align(text, width, direction, options) ⇒ Object
Align a text to a given direction with the width
51 52 53 |
# File 'lib/verse/alignment.rb', line 51 def self.align(text, width, direction, ) new(text, ).align(width, direction, ) end |
Instance Method Details
#align(width, direction = :left, options = {}) ⇒ Object
Aligns text within the width.
If the text is greater than the width then unmodified string is returned.
73 74 75 76 77 78 79 |
# File 'lib/verse/alignment.rb', line 73 def align(width, direction = :left, = {}) return text unless width filler = .fetch(:fill) { fill } method = convert_to_method(direction) process_lines { |line| send(method, line, width, filler) } end |
#center(width, options = {}) ⇒ String
Centers text within the width
33 34 35 |
# File 'lib/verse/alignment.rb', line 33 def center(width, = {}) align(width, :center, ) end |
#left(width, options = {}) ⇒ String
Aligns text to the left
24 25 26 |
# File 'lib/verse/alignment.rb', line 24 def left(width, = {}) align(width, :left, ) end |
#right(width, options = {}) ⇒ String
Aligns text to the right
42 43 44 |
# File 'lib/verse/alignment.rb', line 42 def right(width, = {}) align(width, :right, ) end |