Class: Vedeu::Output::Wordwrap
- Inherits:
-
Object
- Object
- Vedeu::Output::Wordwrap
- Includes:
- Common
- Defined in:
- lib/vedeu/output/wordwrap.rb
Overview
Wrap or prune text.
Instance Attribute Summary collapse
- #options ⇒ Hash readonly protected
- #text ⇒ String readonly protected
Class Method Summary collapse
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable is nil or empty.
- #content ⇒ Vedeu::Views::Lines
-
#defaults ⇒ Hash<Symbol => NilClass, String, Symbol>
private
Returns the default options/attributes for this class.
-
#demodulize(klass) ⇒ String
included
from Common
private
Removes the module part from the expression in the string.
-
#ellipsis ⇒ String
private
For when using mode ‘:prune`, by default, provides ’…‘.
- #ellipsis_string(string) ⇒ String private
-
#initialize(text, options = {}) ⇒ Vedeu::Output::Wordwrap
constructor
Returns a new instance of Vedeu::Output::Wordwrap.
-
#mode ⇒ Symbol
private
Returns the word wrapping mode.
-
#name ⇒ String|Symbol
private
Returns the value of the :name option.
-
#present?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable has a useful value.
- #prune ⇒ Array<String>|String
-
#prune_string(string) ⇒ String
private
Returns the string pruned.
-
#pruned_width ⇒ Fixnum
private
Returns the width of the string minus the ellipsis.
-
#registered? ⇒ Boolean
private
Returns a boolean indicating the :name option was given and a geometry is registered with that name.
-
#snake_case(name) ⇒ String
included
from Common
private
Converts a class name to a lowercase snake case string.
-
#split_lines ⇒ Array<String>
private
Returns the text as an array of lines, split on ‘n’.
- #to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines private
-
#width ⇒ Fixnum
private
Returns the width to prune or wrap to.
- #wrap ⇒ String
Constructor Details
#initialize(text, options = {}) ⇒ Vedeu::Output::Wordwrap
Returns a new instance of Vedeu::Output::Wordwrap.
25 26 27 28 |
# File 'lib/vedeu/output/wordwrap.rb', line 25 def initialize(text, = {}) @text = text @options = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
86 87 88 |
# File 'lib/vedeu/output/wordwrap.rb', line 86 def @options end |
#text ⇒ String (readonly, protected)
82 83 84 |
# File 'lib/vedeu/output/wordwrap.rb', line 82 def text @text end |
Class Method Details
.for(text, options = {}) ⇒ Object
12 13 14 |
# File 'lib/vedeu/output/wordwrap.rb', line 12 def self.for(text, = {}) new(text, ).content end |
Instance Method Details
#absent?(variable) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether a variable is nil or empty.
#content ⇒ Vedeu::Views::Lines
31 32 33 34 35 36 37 38 |
# File 'lib/vedeu/output/wordwrap.rb', line 31 def content case mode when :prune then to_line_objects(prune) when :wrap then to_line_objects(wrap) else to_line_objects(split_lines) end end |
#defaults ⇒ Hash<Symbol => NilClass, String, Symbol> (private)
Returns the default options/attributes for this class.
188 189 190 191 192 193 194 195 |
# File 'lib/vedeu/output/wordwrap.rb', line 188 def defaults { ellipsis: '...', mode: :default, name: nil, width: nil, } end |
#demodulize(klass) ⇒ String Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes the module part from the expression in the string.
#ellipsis ⇒ String (private)
For when using mode ‘:prune`, by default, provides ’…‘.
136 137 138 |
# File 'lib/vedeu/output/wordwrap.rb', line 136 def ellipsis [:ellipsis] end |
#ellipsis_string(string) ⇒ String (private)
112 113 114 115 116 |
# File 'lib/vedeu/output/wordwrap.rb', line 112 def ellipsis_string(string) return prune_string(string) if string.size < ellipsis.size "#{prune_string(string)}#{ellipsis}".freeze end |
#mode ⇒ Symbol (private)
Returns the word wrapping mode. One of :default, :prune or
:wrap;
:default = Renders the content as is.
:prune = Discards the remainder of the content line
after width.
:wrap = Forces the content on to a new line after
width.
150 151 152 |
# File 'lib/vedeu/output/wordwrap.rb', line 150 def mode [:mode] end |
#name ⇒ String|Symbol (private)
Returns the value of the :name option. When given, and a geometry is registered with this name, the width of the geometry is used if the :width option was not given.
159 160 161 |
# File 'lib/vedeu/output/wordwrap.rb', line 159 def name [:name] end |
#present?(variable) ⇒ Boolean Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a boolean indicating whether a variable has a useful value.
#prune ⇒ Array<String>|String
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vedeu/output/wordwrap.rb', line 41 def prune return text if text.size <= pruned_width if split_lines.size > 1 split_lines.reduce([]) { |a, e| a << ellipsis_string(e) } else ellipsis_string(text) end end |
#prune_string(string) ⇒ String (private)
Returns the string pruned.
122 123 124 |
# File 'lib/vedeu/output/wordwrap.rb', line 122 def prune_string(string) string.chomp.slice(0..pruned_width) end |
#pruned_width ⇒ Fixnum (private)
Returns the width of the string minus the ellipsis.
129 130 131 |
# File 'lib/vedeu/output/wordwrap.rb', line 129 def pruned_width width - ellipsis.size end |
#registered? ⇒ Boolean (private)
Returns a boolean indicating the :name option was given and a geometry is registered with that name.
167 168 169 |
# File 'lib/vedeu/output/wordwrap.rb', line 167 def registered? present?(name) && Vedeu.geometries.registered?(name) end |
#snake_case(name) ⇒ String Originally defined in module Common
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts a class name to a lowercase snake case string.
#split_lines ⇒ Array<String> (private)
Returns the text as an array of lines, split on ‘n’.
106 107 108 |
# File 'lib/vedeu/output/wordwrap.rb', line 106 def split_lines text.split(/\n/) end |
#to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines (private)
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vedeu/output/wordwrap.rb', line 92 def to_line_objects(text_as_lines) line_objects = Array(text_as_lines).map do |text_line| stream = Vedeu::Views::Stream.new(value: text_line) line = Vedeu::Views::Line.new stream.parent = line line.add(stream) line end Vedeu::Views::Lines.new(line_objects) end |
#width ⇒ Fixnum (private)
Returns the width to prune or wrap to.
175 176 177 178 179 180 181 182 183 |
# File 'lib/vedeu/output/wordwrap.rb', line 175 def width return [:width] if present?([:width]) return Vedeu.geometries.by_name(name).width if registered? fail Vedeu::Error::MissingRequired, 'The text provided cannot be wrapped or pruned because a :width ' \ 'option was not given, or a :name option was either not given ' \ 'or there is no geometry registered with that name.' end |
#wrap ⇒ String
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vedeu/output/wordwrap.rb', line 54 def wrap processed = [] split_lines.map do |unprocessed| line_length = 0 reformatted = [] unprocessed.split(/\s/).map do |word| word_length = word.length + 1 if (line_length += word_length) >= width line_length = word_length processed << reformatted reformatted = [] end reformatted << word end processed << reformatted end processed.reduce([]) { |a, e| a << e.join(' ') } end |