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
-
#absent?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable is nil or empty.
-
#become(klass, attributes) ⇒ Class
included
from Common
private
Converts one class into another.
-
#boolean(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating the value was a boolean.
-
#boolean?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Boolean.
- #content ⇒ Vedeu::Views::Lines private
-
#defaults ⇒ Hash<Symbol => NilClass, String, Symbol>
private
private
Returns the default options/attributes for this class.
-
#ellipsis ⇒ String
private
private
For when using mode ‘:prune`, by default, provides ’…‘.
- #ellipsis_string(string) ⇒ String private private
-
#escape?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is an escape sequence object (e.g. Cells::Escape.).
-
#falsy?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value should be considered false.
- #geometry ⇒ Vedeu::Geometries::Geometry private private
-
#hash?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Hash.
-
#initialize(text, options = {}) ⇒ Vedeu::DSL::Wordwrap
constructor
private
Returns a new instance of Vedeu::DSL::Wordwrap.
-
#line_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::Line.
-
#mode ⇒ Symbol
private
private
Returns the word wrapping mode.
-
#name ⇒ String|Symbol
private
private
Returns the value of the :name option.
-
#numeric?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Fixnum.
-
#present?(variable) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether a variable has a useful value.
- #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.
-
#snake_case(klass) ⇒ String
included
from Common
private
Converts a class name to a lowercase snake case string.
-
#split_lines ⇒ Array<String>
private
private
Returns the text as an array of lines, split on ‘n’.
-
#stream_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::Stream.
-
#string?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value is a Fixnum.
- #to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines private private
-
#truthy?(value) ⇒ Boolean
included
from Common
private
Returns a boolean indicating whether the value should be considered true.
-
#view_model? ⇒ Boolean
included
from Common
private
Returns a boolean indicating the model is a Views::View.
-
#width ⇒ Fixnum
private
private
Returns the width to prune or wrap to.
- #wrap ⇒ String private
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
#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.
#become(klass, attributes) ⇒ Class 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 one class into another.
#boolean(value) ⇒ 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 the value was a boolean.
#boolean?(value) ⇒ 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 the value is a Boolean.
#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 => NilClass, String, 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 default options/attributes for this class.
199 200 201 202 203 204 205 206 |
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 199 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 |
#escape?(value) ⇒ 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 the value is an escape sequence object (e.g. Vedeu::Cells::Escape.)
#falsy?(value) ⇒ 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 the value should be considered false.
#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 |
#hash?(value) ⇒ 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 the value is a Hash.
#line_model? ⇒ 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 the model is a Views::Line.
#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 ⇒ String|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 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 |
#numeric?(value) ⇒ 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 the value is a Fixnum.
#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
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 |
#snake_case(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.
Converts a class name to a lowercase snake case string.
#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 |
#stream_model? ⇒ 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 the model is a Views::Stream.
#string?(value) ⇒ 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 the value is a Fixnum.
#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 |
#truthy?(value) ⇒ 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 the value should be considered true.
#view_model? ⇒ 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 the model is a Views::View.
#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? 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
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 |