Class: Vedeu::Output::Text
- Inherits:
-
Object
- Object
- Vedeu::Output::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.
- #client ⇒ Object private
-
#colour ⇒ void
private
If a colour, background or foreground option is set, use them as the colour settings for the new Vedeu::Views::Stream.
-
#content ⇒ Vedeu::Views::Line|Vedeu::Views::Stream
private
Returns either a Vedeu::Views::Line or Vedeu::Views::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 = {}) ⇒ Vedeu::Output::Text
constructor
Returns a new instance of Vedeu::Output::Text.
-
#left ⇒ String
private
The string padded to width, left justified.
- #line ⇒ Vedeu::Views::Line private
- #mode ⇒ Symbol private
-
#model ⇒ Vedeu::Views::View| Vedeu::Views::Line| Vedeu::Null::Generic| Vedeu::Views::Stream
private
Returns the model option when set.
-
#pad ⇒ String
private
The character to use for padding the string.
-
#parent ⇒ void
private
Returns the parent for the new Vedeu::Views::Stream.
-
#right ⇒ String
private
The string padded to width, right justified.
-
#stream ⇒ void
private
Builds and returns a new Vedeu::Views::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.
-
#wrap? ⇒ Boolean
private
Return a boolean indicating whether the string should be wrapped.
-
#wrapped ⇒ Vedeu::Views::Lines
private
Return the content as wrapped lines.
Constructor Details
#initialize(value = '', options = {}) ⇒ Vedeu::Output::Text
Returns a new instance of Vedeu::Output::Text.
33 34 35 36 |
# File 'lib/vedeu/output/text.rb', line 33 def initialize(value = '', = {}) @value = value = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
76 77 78 |
# File 'lib/vedeu/output/text.rb', line 76 def end |
#value ⇒ String (readonly, protected)
72 73 74 |
# File 'lib/vedeu/output/text.rb', line 72 def value @value end |
Class Method Details
.add(value = '', options = {}) ⇒ Object
10 11 12 |
# File 'lib/vedeu/output/text.rb', line 10 def self.add(value = '', = {}) new(value, ).add end |
.with(value = '', options = {}) ⇒ Object
15 16 17 |
# File 'lib/vedeu/output/text.rb', line 15 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.
58 59 60 61 62 63 64 65 66 |
# File 'lib/vedeu/output/text.rb', line 58 def add if wrap? model.add(wrapped) else model.add(content) end end |
#aligned ⇒ String
Aligns the value.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vedeu/output/text.rb', line 41 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.
82 83 84 |
# File 'lib/vedeu/output/text.rb', line 82 def anchor [:anchor] end |
#centre ⇒ String (private)
The string padded to width, centralized.
89 90 91 |
# File 'lib/vedeu/output/text.rb', line 89 def centre string.center(width, pad) end |
#client ⇒ Object (private)
94 95 96 |
# File 'lib/vedeu/output/text.rb', line 94 def client [:client] 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::Views::Stream.
102 103 104 105 106 107 108 109 110 |
# File 'lib/vedeu/output/text.rb', line 102 def colour if [:colour] || [:background] || [:foreground] Vedeu::Colours::Colour.coerce() else model.colour end end |
#content ⇒ Vedeu::Views::Line|Vedeu::Views::Stream (private)
Returns either a Vedeu::Views::Line or Vedeu::Views::Stream containing the text value.
116 117 118 119 120 121 122 |
# File 'lib/vedeu/output/text.rb', line 116 def content return stream unless model.is_a?(Vedeu::Views::View) stream.parent = line line.add(stream) line end |
#defaults ⇒ Hash<Symbol => NilClass, String, Symbol> (private)
The default values for a new instance of this class.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/vedeu/output/text.rb', line 127 def defaults { client: nil, anchor: :left, colour: nil, model: nil, mode: :default, pad: ' ', width: nil, } end |
#left ⇒ String (private)
The string padded to width, left justified.
142 143 144 |
# File 'lib/vedeu/output/text.rb', line 142 def left string.ljust(width, pad) end |
#line ⇒ Vedeu::Views::Line (private)
147 148 149 |
# File 'lib/vedeu/output/text.rb', line 147 def line @line ||= Vedeu::Views::Line.build(parent: parent, client: client) end |
#mode ⇒ Symbol (private)
163 164 165 |
# File 'lib/vedeu/output/text.rb', line 163 def mode [:mode] end |
#model ⇒ Vedeu::Views::View| Vedeu::Views::Line| Vedeu::Null::Generic| Vedeu::Views::Stream (private)
Returns the model option when set.
157 158 159 |
# File 'lib/vedeu/output/text.rb', line 157 def model @model ||= [:model] || Vedeu::Null::Generic.new end |
#pad ⇒ String (private)
The character to use for padding the string.
170 171 172 |
# File 'lib/vedeu/output/text.rb', line 170 def pad [:pad] end |
#parent ⇒ void (private)
This method returns an undefined value.
Returns the parent for the new Vedeu::Views::Stream.
177 178 179 |
# File 'lib/vedeu/output/text.rb', line 177 def parent model.is_a?(Vedeu::Views::Stream) ? model.parent : model end |
#right ⇒ String (private)
The string padded to width, right justified.
184 185 186 |
# File 'lib/vedeu/output/text.rb', line 184 def right string.rjust(width, pad) end |
#stream ⇒ void (private)
This method returns an undefined value.
Builds and returns a new Vedeu::Views::Stream.
191 192 193 194 195 196 197 |
# File 'lib/vedeu/output/text.rb', line 191 def stream @stream ||= Vedeu::Views::Stream.build(client: client, colour: colour, parent: parent, style: style, value: aligned) end |
#string ⇒ String (private)
The string, coerced.
202 203 204 |
# File 'lib/vedeu/output/text.rb', line 202 def string value.to_s end |
#style ⇒ void (private)
This method returns an undefined value.
Returns the model’s styles.
209 210 211 |
# File 'lib/vedeu/output/text.rb', line 209 def style model.style end |
#truncate? ⇒ Boolean (private)
Return a boolean indicating that the string is greater than the width.
217 218 219 |
# File 'lib/vedeu/output/text.rb', line 217 def truncate? string.size > width end |
#truncated ⇒ String (private)
Return the string truncated to the width.
224 225 226 |
# File 'lib/vedeu/output/text.rb', line 224 def truncated string.slice(0, width) end |
#width ⇒ Fixnum (private)
Return the width.
231 232 233 |
# File 'lib/vedeu/output/text.rb', line 231 def width [:width] end |
#wrap? ⇒ Boolean (private)
Return a boolean indicating whether the string should be wrapped.
239 240 241 |
# File 'lib/vedeu/output/text.rb', line 239 def wrap? [:mode] == :wrap end |
#wrapped ⇒ Vedeu::Views::Lines (private)
Return the content as wrapped lines.
246 247 248 |
# File 'lib/vedeu/output/text.rb', line 246 def wrapped Vedeu::Output::Wordwrap.for(string, ) end |