Class: Vedeu::Output::Wordwrap

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/vedeu/output/wordwrap.rb

Overview

Wrap or prune text.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Vedeu::Output::Wordwrap

Returns a new instance of Vedeu::Output::Wordwrap.

Parameters:

  • text (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • ellipsis (String)

    See #ellipsis.

  • mode (Symbol)

    See #mode.

  • name (String|Symbol)

    See #name.

  • width (Fixnum)

    See #width.



25
26
27
28
# File 'lib/vedeu/output/wordwrap.rb', line 25

def initialize(text, options = {})
  @text    = text
  @options = defaults.merge!(options)
end

Instance Attribute Details

#optionsHash (readonly, protected)

Returns:

  • (Hash)


86
87
88
# File 'lib/vedeu/output/wordwrap.rb', line 86

def options
  @options
end

#textString (readonly, protected)

Returns:

  • (String)


82
83
84
# File 'lib/vedeu/output/wordwrap.rb', line 82

def text
  @text
end

Class Method Details

.for(text, options = {}) ⇒ Object

See Also:



12
13
14
# File 'lib/vedeu/output/wordwrap.rb', line 12

def self.for(text, options = {})
  new(text, options).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.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#contentVedeu::Views::Lines

Returns:



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

#defaultsHash<Symbol => NilClass, String, Symbol> (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash<Symbol => NilClass, String, Symbol>)


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.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#ellipsisString (private)

For when using mode ‘:prune`, by default, provides ’…‘.

Returns:

  • (String)


136
137
138
# File 'lib/vedeu/output/wordwrap.rb', line 136

def ellipsis
  options[:ellipsis]
end

#ellipsis_string(string) ⇒ String (private)

Parameters:

  • string (String)

Returns:

  • (String)


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

#modeSymbol (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.

Returns:

  • (Symbol)


150
151
152
# File 'lib/vedeu/output/wordwrap.rb', line 150

def mode
  options[:mode]
end

#nameString|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.

Returns:

  • (String|Symbol)


159
160
161
# File 'lib/vedeu/output/wordwrap.rb', line 159

def name
  options[: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.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#pruneArray<String>|String

Returns:

  • (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.

Parameters:

  • string (String)

Returns:

  • (String)


122
123
124
# File 'lib/vedeu/output/wordwrap.rb', line 122

def prune_string(string)
  string.chomp.slice(0..pruned_width)
end

#pruned_widthFixnum (private)

Returns the width of the string minus the ellipsis.

Returns:

  • (Fixnum)


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.

Returns:

  • (Boolean)


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.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#split_linesArray<String> (private)

Returns the text as an array of lines, split on ‘n’.

Returns:

  • (Array<String>)


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)

Parameters:

  • text_as_lines (Array<String>)

Returns:



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

#widthFixnum (private)

Returns the width to prune or wrap to.

Returns:

  • (Fixnum)

Raises:



175
176
177
178
179
180
181
182
183
# File 'lib/vedeu/output/wordwrap.rb', line 175

def width
  return options[:width] if present?(options[: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

#wrapString

Returns:

  • (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