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 ⇒ Hash readonly protected
- #value ⇒ String readonly protected
Class Method Summary collapse
Instance Method Summary collapse
-
#add ⇒ void
Adds the content to the model.
-
#aligned ⇒ String
Aligns the value.
-
#anchor ⇒ Symbol
private
One of :align, :centre, :center, :left, :right, :text.
-
#centre ⇒ String
private
The string padded to width, centralized.
-
#colour ⇒ void
private
If a colour, background or foreground option is set, use them as the colour settings for the new Vedeu::Stream.
-
#content ⇒ Vedeu::Line|Vedeu::Stream
private
Returns either a Vedeu::Line or Vedeu::Stream containing the text value.
-
#defaults ⇒ Hash<Symbol => NilClass, String, Symbol>
private
The default values for a new instance of this class.
-
#initialize(value = '', options = {}) ⇒ Text
constructor
Returns a new instance of Vedeu::Text.
-
#left ⇒ String
private
The string padded to width, left justified.
- #line ⇒ Vedeu::Line private
-
#model ⇒ Vedeu::Interface|Vedeu::Line|Vedeu::Null::Generic|Vedeu::Stream
private
Returns the model option if set.
-
#pad ⇒ String
private
The character to use for padding the string.
-
#parent ⇒ void
private
Returns the parent for the new Vedeu::Stream.
-
#right ⇒ String
private
The string padded to width, right justified.
-
#stream ⇒ void
private
Builds and returns a new Vedeu::Stream.
-
#string ⇒ String
private
The string, coerced.
-
#style ⇒ void
private
Returns the model’s styles.
-
#truncate? ⇒ Boolean
private
Return a boolean indicating that the string is greater than the width.
-
#truncated ⇒ String
private
Return the string truncated to the width.
-
#width ⇒ Fixnum
private
Return the width.
Constructor Details
#initialize(value = '', options = {}) ⇒ Text
Returns a new instance of Vedeu::Text.
28 29 30 31 |
# File 'lib/vedeu/output/text.rb', line 28 def initialize(value = '', = {}) @value = value = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
65 66 67 |
# File 'lib/vedeu/output/text.rb', line 65 def end |
#value ⇒ String (readonly, protected)
61 62 63 |
# File 'lib/vedeu/output/text.rb', line 61 def value @value end |
Class Method Details
.add(value = '', options = {}) ⇒ Object
7 8 9 |
# File 'lib/vedeu/output/text.rb', line 7 def self.add(value = '', = {}) new(value, ).add end |
.with(value = '', options = {}) ⇒ Object
12 13 14 |
# File 'lib/vedeu/output/text.rb', line 12 def self.with(value = '', = {}) new(value, ).aligned end |
Instance Method Details
#add ⇒ void
This method returns an undefined value.
Adds the content to the model.
53 54 55 |
# File 'lib/vedeu/output/text.rb', line 53 def add model.add(content) end |
#aligned ⇒ String
Aligns the value.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vedeu/output/text.rb', line 36 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.
70 71 72 |
# File 'lib/vedeu/output/text.rb', line 70 def anchor [:anchor] end |
#centre ⇒ String (private)
The string padded to width, centralized.
77 78 79 |
# File 'lib/vedeu/output/text.rb', line 77 def centre string.center(width, pad) end |
#colour ⇒ void (private)
This method returns an undefined value.
If a colour, background or foreground option is set, use them as the colour settings for the new Vedeu::Stream.
85 86 87 88 89 90 91 92 93 |
# File 'lib/vedeu/output/text.rb', line 85 def colour if [:colour] || [:background] || [:foreground] Vedeu::Colour.coerce() else model.colour end end |
#content ⇒ Vedeu::Line|Vedeu::Stream (private)
Returns either a Vedeu::Line or Vedeu::Stream containing the text value.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/vedeu/output/text.rb', line 98 def content if model.is_a?(Vedeu::Interface) stream.parent = line line.add(stream) line else stream end end |
#defaults ⇒ Hash<Symbol => NilClass, String, Symbol> (private)
The default values for a new instance of this class.
113 114 115 116 117 118 119 120 121 |
# File 'lib/vedeu/output/text.rb', line 113 def defaults { anchor: :left, colour: nil, model: nil, pad: ' ', width: nil, } end |
#left ⇒ String (private)
The string padded to width, left justified.
126 127 128 |
# File 'lib/vedeu/output/text.rb', line 126 def left string.ljust(width, pad) end |
#line ⇒ Vedeu::Line (private)
131 132 133 |
# File 'lib/vedeu/output/text.rb', line 131 def line @line ||= Vedeu::Line.build(parent: parent) end |
#model ⇒ Vedeu::Interface|Vedeu::Line|Vedeu::Null::Generic|Vedeu::Stream (private)
Returns the model option if set.
138 139 140 |
# File 'lib/vedeu/output/text.rb', line 138 def model @model ||= [:model] || Vedeu::Null::Generic.new end |
#pad ⇒ String (private)
The character to use for padding the string.
145 146 147 |
# File 'lib/vedeu/output/text.rb', line 145 def pad [:pad] end |
#parent ⇒ void (private)
This method returns an undefined value.
Returns the parent for the new Vedeu::Stream.
152 153 154 |
# File 'lib/vedeu/output/text.rb', line 152 def parent model.is_a?(Vedeu::Stream) ? model.parent : model end |
#right ⇒ String (private)
The string padded to width, right justified.
159 160 161 |
# File 'lib/vedeu/output/text.rb', line 159 def right string.rjust(width, pad) end |
#stream ⇒ void (private)
This method returns an undefined value.
Builds and returns a new Vedeu::Stream.
166 167 168 169 170 171 |
# File 'lib/vedeu/output/text.rb', line 166 def stream @stream ||= Vedeu::Stream.build(colour: colour, parent: parent, style: style, value: aligned) end |
#string ⇒ String (private)
The string, coerced.
176 177 178 |
# File 'lib/vedeu/output/text.rb', line 176 def string value.to_s end |
#style ⇒ void (private)
This method returns an undefined value.
Returns the model’s styles.
183 184 185 |
# File 'lib/vedeu/output/text.rb', line 183 def style model.style end |
#truncate? ⇒ Boolean (private)
Return a boolean indicating that the string is greater than the width.
190 191 192 |
# File 'lib/vedeu/output/text.rb', line 190 def truncate? string.size > width end |
#truncated ⇒ String (private)
Return the string truncated to the width.
197 198 199 |
# File 'lib/vedeu/output/text.rb', line 197 def truncated string.slice(0, width) end |
#width ⇒ Fixnum (private)
Return the width.
204 205 206 |
# File 'lib/vedeu/output/text.rb', line 204 def width [:width] end |