Class: Vedeu::DSL::Wordwrap Private

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

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.



31
32
33
34
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 31

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

Instance Attribute Details

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

Returns:

  • (Hash)


92
93
94
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 92

def options
  @options
end

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

Returns:

  • (String)


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.

Parameters:

  • model (void)

    The model class which the DSL class is wrapping.

  • client (void)

    The class where the DSL methods are being used.

Returns:



18
19
20
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 18

def self.for(text, options = {})
  new(text, options).content
end

Instance Method Details

#contentVedeu::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.

Returns:



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

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

Returns:

  • (Hash<Symbol => void>)


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

#ellipsisString (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 ’…‘.

Returns:

  • (String)


142
143
144
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 142

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

Parameters:

  • string (String)

Returns:

  • (String)


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

#geometryVedeu::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

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

Returns:

  • (Symbol)


161
162
163
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 161

def mode
  options[:mode]
end

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

Returns:

  • (NilClass|Symbol|String)

    The name of the model, the target model or the name of the associated model.



170
171
172
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 170

def name
  options[:name]
end

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

Returns:

  • (Array<String>|String)


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.

Parameters:

  • string (String)

Returns:

  • (String)


128
129
130
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 128

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

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

Returns:

  • (Fixnum)


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.

Returns:



178
179
180
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 178

def registered?
  present?(name) && Vedeu.geometries.registered?(name)
end

#split_linesArray<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’.

Returns:

  • (Array<String>)


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.

Parameters:

  • text_as_lines (Array<String>)

Returns:



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

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

Returns:

  • (Fixnum)

Raises:



186
187
188
189
190
191
192
193
194
# File 'lib/vedeu/dsl/helpers/wordwrap.rb', line 186

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

#wrapString

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:

  • (String)


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