Class: Vedeu::Borders::Title Private

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/borders/title.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.

When a Border has a title, truncate it if the title is longer than the interface is wide, and pad with a space either side.

The title is displayed within the top border of the interface/ view.

Direct Known Subclasses

Caption

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = '', horizontal = []) ⇒ Vedeu::Borders::Title|Vedeu::Borders::Caption

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::Borders::Title or Vedeu::Borders::Caption.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

  • value (String|Vedeu::Borders::Title|) (defaults to: '')

    alue [String|Vedeu::Borders::Title|

  • horizontal (Array<Vedeu::Cells::Horizontal>) (defaults to: [])

    Vedeu::Borders::Caption]



32
33
34
35
36
# File 'lib/vedeu/borders/title.rb', line 32

def initialize(name, value = '', horizontal = [])
  @name       = name
  @value      = value
  @horizontal = horizontal
end

Instance Attribute Details

#horizontalArray<Vedeu::Cells::Horizontal> (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 An array of border characters.

Returns:



82
83
84
# File 'lib/vedeu/borders/title.rb', line 82

def horizontal
  @horizontal
end

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

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

Returns:

  • (NilClass|Symbol|String)

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



86
87
88
# File 'lib/vedeu/borders/title.rb', line 86

def name
  @name
end

Class Method Details

.render(name, value = '', horizontal = []) ⇒ Array<Vedeu::Cells::Horizontal|Vedeu::Cells::Char>

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:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

  • value (String|Vedeu::Borders::Title|) (defaults to: '')

    alue [String|Vedeu::Borders::Title|

  • horizontal (Array<Vedeu::Cells::Horizontal>) (defaults to: [])

    Vedeu::Borders::Caption]

Returns:



20
21
22
# File 'lib/vedeu/borders/title.rb', line 20

def self.render(name, value = '', horizontal = [])
  new(name, value, horizontal).render
end

Instance Method Details

#attributes(char, x) ⇒ 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.

Parameters:

  • char (String)
  • x (Fixnum)

Returns:

  • (Hash<Symbol => void>)


93
94
95
96
# File 'lib/vedeu/borders/title.rb', line 93

def attributes(char, x)
  border.attributes.merge(position: Vedeu::Geometries::Position.new(y, x),
                          value:    char)
end

#borderVedeu::Borders::Border (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 named border from the borders repository.



99
100
101
# File 'lib/vedeu/borders/title.rb', line 99

def border
  @_border ||= Vedeu.borders.by_name(name)
end

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

Return the padded, truncated value as an Array of String.

Returns:

  • (Array<String>)


113
114
115
# File 'lib/vedeu/borders/title.rb', line 113

def characters
  pad.chars
end

#charsArray<Vedeu::Cells::Horizontal|Vedeu::Cells::Char> (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.



104
105
106
107
108
# File 'lib/vedeu/borders/title.rb', line 104

def chars
  characters.each_with_index.map do |char, index|
    Vedeu::Cells::Char.new(attributes(char, x + index))
  end
end

#empty?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.

Return boolean indicating whether the value is empty.

Returns:



120
121
122
# File 'lib/vedeu/borders/title.rb', line 120

def empty?
  value.empty?
end

#end_indexFixnum (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:

  • (Fixnum)


125
126
127
# File 'lib/vedeu/borders/title.rb', line 125

def end_index
  start_index + (size - 1)
end

#eql?(other) ⇒ Boolean Also known as: ==

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.

An object is equal when its values are the same.

Parameters:

Returns:



42
43
44
# File 'lib/vedeu/borders/title.rb', line 42

def eql?(other)
  self.class.equal?(other.class) && value == other.value
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.

Returns the named geometry from the geometries repository.



130
131
132
# File 'lib/vedeu/borders/title.rb', line 130

def geometry
  @_geometry ||= Vedeu.geometries.by_name(name)
end

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

Pads the value with a single whitespace either side.

Examples:

value = 'Truncated!'
width = 20
# => ' Truncated! '

width = 10
# => ' Trunca '

Returns:

  • (String)

See Also:



146
147
148
# File 'lib/vedeu/borders/title.rb', line 146

def pad
  truncate.center(truncate.size + 2)
end

#renderArray<Vedeu::Cells::Horizontal|Vedeu::Cells::Char>

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.

Overwrite the border from Border#build_horizontal on the top border to include the title if given.



52
53
54
55
56
57
58
# File 'lib/vedeu/borders/title.rb', line 52

def render
  return horizontal if empty?

  horizontal[start_index..end_index] = chars

  horizontal
end

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

Return the size of the padded, truncated value.

Returns:

  • (Fixnum)


153
154
155
# File 'lib/vedeu/borders/title.rb', line 153

def size
  pad.size
end

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

Provides the index on the horizontal border for where the title (or caption) should start. The title will appear top-left, whilst the caption will be justified bottom-right.

Returns:

  • (Fixnum)


162
163
164
# File 'lib/vedeu/borders/title.rb', line 162

def start_index
  1
end

#to_sString Also known as: to_str

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.

Convert the value to a string.

Returns:

  • (String)


63
64
65
# File 'lib/vedeu/borders/title.rb', line 63

def to_s
  value.to_s
end

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

Truncates the value to the width of the interface, minus characters needed to ensure there is at least a single character of horizontal border and a whitespace on either side of the value.

Examples:

value = 'Truncated!'
width = 20
# => 'Truncated!'

width = 10
# => 'Trunca'

Returns:

  • (String)


180
181
182
# File 'lib/vedeu/borders/title.rb', line 180

def truncate
  value.chomp.slice(0...(width - 4))
end

#valueString Also known as: title, caption

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.

Return the value (a title or a caption) or an empty string.

Returns:

  • (String)


71
72
73
# File 'lib/vedeu/borders/title.rb', line 71

def value
  @value || ''
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.

Return the size of the horizontal border given.

Returns:

  • (Fixnum)


187
188
189
# File 'lib/vedeu/borders/title.rb', line 187

def width
  horizontal.size
end

#xFixnum (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:

  • (Fixnum)


192
193
194
# File 'lib/vedeu/borders/title.rb', line 192

def x
  geometry.bx + start_index
end

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

Return the vertical position for the title (or caption).

Returns:

  • (Fixnum)


199
200
201
# File 'lib/vedeu/borders/title.rb', line 199

def y
  geometry.y
end