Class: Vedeu::Text
- Inherits:
-
Object
- Object
- Vedeu::Text
- Defined in:
- lib/vedeu/output/text.rb
Overview
Present a string (or object responding to ‘to_s`).
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
private
Returns the value of attribute options.
-
#value ⇒ Object
readonly
private
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#aligned ⇒ String
Aligns the value.
-
#anchor ⇒ Symbol
private
One of :align, :centre, :center, :left, :right, :text.
- #centre ⇒ String private
-
#defaults ⇒ Hash
private
The default values for a new instance of this class.
-
#initialize(value = '', options = {}) ⇒ Text
constructor
Returns a new instance of Text.
- #left ⇒ String private
- #pad ⇒ String private
- #right ⇒ String private
- #string ⇒ String private
- #truncate? ⇒ Boolean private
- #truncated ⇒ String private
- #width ⇒ Fixnum private
Constructor Details
#initialize(value = '', options = {}) ⇒ Text
Returns a new instance of Text.
20 21 22 23 |
# File 'lib/vedeu/output/text.rb', line 20 def initialize(value = '', = {}) @value = value @options = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Object (readonly, private)
Returns the value of attribute options.
44 45 46 |
# File 'lib/vedeu/output/text.rb', line 44 def @options end |
#value ⇒ Object (readonly, private)
Returns the value of attribute value.
44 45 46 |
# File 'lib/vedeu/output/text.rb', line 44 def value @value end |
Class Method Details
.with(value = '', options = {}) ⇒ Object
8 9 10 |
# File 'lib/vedeu/output/text.rb', line 8 def self.with(value = '', = {}) new(value, ).aligned end |
Instance Method Details
#aligned ⇒ String
Aligns the value.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vedeu/output/text.rb', line 28 def aligned return string unless width return truncated if truncate? case anchor when :align, :left, :text then left when :centre, :center then centre when :right then right else left end end |
#anchor ⇒ Symbol (private)
Returns One of :align, :centre, :center, :left, :right, :text.
47 48 49 |
# File 'lib/vedeu/output/text.rb', line 47 def anchor [:anchor] end |
#centre ⇒ String (private)
52 53 54 |
# File 'lib/vedeu/output/text.rb', line 52 def centre string.center(width, pad) end |
#defaults ⇒ Hash (private)
The default values for a new instance of this class.
59 60 61 62 63 64 65 |
# File 'lib/vedeu/output/text.rb', line 59 def defaults { anchor: :left, pad: ' ', width: nil, } end |
#left ⇒ String (private)
68 69 70 |
# File 'lib/vedeu/output/text.rb', line 68 def left string.ljust(width, pad) end |
#pad ⇒ String (private)
73 74 75 |
# File 'lib/vedeu/output/text.rb', line 73 def pad [:pad] end |
#right ⇒ String (private)
78 79 80 |
# File 'lib/vedeu/output/text.rb', line 78 def right string.rjust(width, pad) end |
#string ⇒ String (private)
83 84 85 |
# File 'lib/vedeu/output/text.rb', line 83 def string value.to_s end |
#truncate? ⇒ Boolean (private)
88 89 90 |
# File 'lib/vedeu/output/text.rb', line 88 def truncate? string.size > width end |
#truncated ⇒ String (private)
93 94 95 |
# File 'lib/vedeu/output/text.rb', line 93 def truncated string.slice(0, width) end |
#width ⇒ Fixnum (private)
98 99 100 |
# File 'lib/vedeu/output/text.rb', line 98 def width [:width] end |