Class: Vedeu::DSL::Wordwrap Private
- Inherits:
-
Object
- Object
- Vedeu::DSL::Wordwrap
- Includes:
- Common
- Defined in:
- lib/vedeu/dsl/helpers/wordwrap.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Wrap or prune a text value based on given options when building views.
Instance Attribute Summary collapse
- #options ⇒ Hash readonly protected private
- #text ⇒ String readonly protected private
Class Method Summary collapse
Instance Method Summary collapse
- #content ⇒ Vedeu::Views::Lines private
-
#defaults ⇒ Hash<Symbol => void>
private
private
The default options/attributes for a new instance of this class.
-
#ellipsis ⇒ String
private
private
For when using mode ‘:prune`, by default, provides ’…‘.
- #ellipsis_string(string) ⇒ String private private
- #geometry ⇒ Vedeu::Geometries::Geometry private private
-
#initialize(text, options = {}) ⇒ Vedeu::DSL::Wordwrap
constructor
private
Returns a new instance of Vedeu::DSL::Wordwrap.
-
#mode ⇒ Symbol
private
private
Returns the word wrapping mode.
-
#name ⇒ NilClass|Symbol|String
private
private
Returns the value of the :name option.
- #prune ⇒ Array<String>|String private
-
#prune_string(string) ⇒ String
private
private
Returns the string pruned.
-
#pruned_width ⇒ Fixnum
private
private
Returns the width of the string minus the ellipsis.
-
#registered? ⇒ Boolean
private
private
Returns a boolean indicating the :name option was given and a geometry is registered with that name.
-
#split_lines ⇒ Array<String>
private
private
Returns the text as an array of lines, split on ‘n’.
- #to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines private private
-
#width ⇒ Fixnum
private
private
Returns the width to prune or wrap to.
- #wrap ⇒ String private
Methods included from Common
#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?
Constructor Details
#initialize(text, options = {}) ⇒ Vedeu::DSL::Wordwrap
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 new instance of Vedeu::DSL::Wordwrap.
31 32 33 34 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 31 def initialize(text, = {}) @text = text @options = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
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.
92 93 94 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 92 def @options end |
#text ⇒ String (readonly, protected)
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.
88 89 90 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 88 def text @text end |
Class Method Details
.for(text, options = {}) ⇒ Vedeu::Views::Lines
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.
18 19 20 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 18 def self.for(text, = {}) new(text, ).content end |
Instance Method Details
#content ⇒ Vedeu::Views::Lines
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.
37 38 39 40 41 42 43 44 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 37 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 => void> (private)
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.
The default options/attributes for a new instance of this class.
197 198 199 200 201 202 203 204 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 197 def defaults { ellipsis: '...', mode: :default, name: nil, width: nil, } end |
#ellipsis ⇒ String (private)
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.
For when using mode ‘:prune`, by default, provides ’…‘.
142 143 144 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 142 def ellipsis [:ellipsis] end |
#ellipsis_string(string) ⇒ String (private)
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.
118 119 120 121 122 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 118 def ellipsis_string(string) return prune_string(string) if string.size < ellipsis.size "#{prune_string(string)}#{ellipsis}" end |
#geometry ⇒ Vedeu::Geometries::Geometry (private)
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.
147 148 149 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 147 def geometry Vedeu.geometry.by_name(name) end |
#mode ⇒ Symbol (private)
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 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.
161 162 163 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 161 def mode [:mode] end |
#name ⇒ NilClass|Symbol|String (private)
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 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.
170 171 172 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 170 def name [:name] end |
#prune ⇒ Array<String>|String
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.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 47 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)
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 the string pruned.
128 129 130 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 128 def prune_string(string) string.chomp.slice(0..pruned_width) end |
#pruned_width ⇒ Fixnum (private)
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 the width of the string minus the ellipsis.
135 136 137 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 135 def pruned_width width - ellipsis.size end |
#registered? ⇒ Boolean (private)
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 the :name option was given and a geometry is registered with that name.
178 179 180 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 178 def registered? present?(name) && Vedeu.geometries.registered?(name) end |
#split_lines ⇒ Array<String> (private)
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 the text as an array of lines, split on ‘n’.
112 113 114 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 112 def split_lines text.split(/\n/) end |
#to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines (private)
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.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 98 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)
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 the width to prune or wrap to.
186 187 188 189 190 191 192 193 194 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 186 def width return [:width] if present?([:width]) return geometry.width if registered? raise 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
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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 60 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 |