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
-
#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.
Constructor Details
#initialize(value = '', options = {}) ⇒ Vedeu::Output::Text
Returns a new instance of Vedeu::Output::Text.
32 33 34 35 |
# File 'lib/vedeu/output/text.rb', line 32 def initialize(value = '', = {}) @value = value @options = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
69 70 71 |
# File 'lib/vedeu/output/text.rb', line 69 def @options end |
#value ⇒ String (readonly, protected)
65 66 67 |
# File 'lib/vedeu/output/text.rb', line 65 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.
57 58 59 |
# File 'lib/vedeu/output/text.rb', line 57 def add model.add(content) end |
#aligned ⇒ String
Aligns the value.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/vedeu/output/text.rb', line 40 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.
75 76 77 |
# File 'lib/vedeu/output/text.rb', line 75 def anchor [:anchor] end |
#centre ⇒ String (private)
The string padded to width, centralized.
82 83 84 |
# File 'lib/vedeu/output/text.rb', line 82 def centre string.center(width, pad) end |
#client ⇒ Object (private)
87 88 89 |
# File 'lib/vedeu/output/text.rb', line 87 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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/vedeu/output/text.rb', line 95 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.
109 110 111 112 113 114 115 |
# File 'lib/vedeu/output/text.rb', line 109 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.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/vedeu/output/text.rb', line 120 def defaults { client: nil, anchor: :left, colour: nil, model: nil, pad: ' ', width: nil, } end |
#left ⇒ String (private)
The string padded to width, left justified.
134 135 136 |
# File 'lib/vedeu/output/text.rb', line 134 def left string.ljust(width, pad) end |
#line ⇒ Vedeu::Views::Line (private)
139 140 141 |
# File 'lib/vedeu/output/text.rb', line 139 def line @line ||= Vedeu::Views::Line.build(parent: parent, client: client) end |
#model ⇒ Vedeu::Views::View| Vedeu::Views::Line| Vedeu::Null::Generic| Vedeu::Views::Stream (private)
Returns the model option when set.
149 150 151 |
# File 'lib/vedeu/output/text.rb', line 149 def model @model ||= [:model] || Vedeu::Null::Generic.new end |
#pad ⇒ String (private)
The character to use for padding the string.
156 157 158 |
# File 'lib/vedeu/output/text.rb', line 156 def pad [:pad] end |
#parent ⇒ void (private)
This method returns an undefined value.
Returns the parent for the new Vedeu::Views::Stream.
163 164 165 |
# File 'lib/vedeu/output/text.rb', line 163 def parent model.is_a?(Vedeu::Views::Stream) ? model.parent : model end |
#right ⇒ String (private)
The string padded to width, right justified.
170 171 172 |
# File 'lib/vedeu/output/text.rb', line 170 def right string.rjust(width, pad) end |
#stream ⇒ void (private)
This method returns an undefined value.
Builds and returns a new Vedeu::Views::Stream.
177 178 179 180 181 182 183 |
# File 'lib/vedeu/output/text.rb', line 177 def stream @stream ||= Vedeu::Views::Stream.build(client: client, colour: colour, parent: parent, style: style, value: aligned) end |
#string ⇒ String (private)
The string, coerced.
188 189 190 |
# File 'lib/vedeu/output/text.rb', line 188 def string value.to_s end |
#style ⇒ void (private)
This method returns an undefined value.
Returns the model’s styles.
195 196 197 |
# File 'lib/vedeu/output/text.rb', line 195 def style model.style end |
#truncate? ⇒ Boolean (private)
Return a boolean indicating that the string is greater than the width.
203 204 205 |
# File 'lib/vedeu/output/text.rb', line 203 def truncate? string.size > width end |
#truncated ⇒ String (private)
Return the string truncated to the width.
210 211 212 |
# File 'lib/vedeu/output/text.rb', line 210 def truncated string.slice(0, width) end |
#width ⇒ Fixnum (private)
Return the width.
217 218 219 |
# File 'lib/vedeu/output/text.rb', line 217 def width [:width] end |