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
#initialize(text, options = {}) ⇒ Alignment
Initialize an Alignment
14 15 16 17 18 |
# File 'lib/verse/alignment.rb', line 14 def initialize(text, = {}) @text = text @fill = .fetch(:fill) { SPACE } @direction = .fetch(:direction) { :left } end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
9 10 11 |
# File 'lib/verse/alignment.rb', line 9 def direction @direction end |
#fill ⇒ Object (readonly)
Returns the value of attribute fill.
7 8 9 |
# File 'lib/verse/alignment.rb', line 7 def fill @fill end |
Class Method Details
.align(text, width, direction, options) ⇒ Object
Align a text to a given direction with the width
52 53 54 |
# File 'lib/verse/alignment.rb', line 52 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.
74 75 76 77 78 79 80 |
# File 'lib/verse/alignment.rb', line 74 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
34 35 36 |
# File 'lib/verse/alignment.rb', line 34 def center(width, = {}) align(width, :center, ) end |
#left(width, options = {}) ⇒ String
Aligns text to the left
25 26 27 |
# File 'lib/verse/alignment.rb', line 25 def left(width, = {}) align(width, :left, ) end |
#right(width, options = {}) ⇒ String
Aligns text to the right
43 44 45 |
# File 'lib/verse/alignment.rb', line 43 def right(width, = {}) align(width, :right, ) end |