Class: Vedeu::Output::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/output/text.rb

Overview

Present a string (or object responding to ‘to_s`).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', options = {}) ⇒ Vedeu::Output::Text

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

Parameters:

  • value (String) (defaults to: '')
  • options (Hash) (defaults to: {})

Options Hash (options):

  • anchor (Symbol)

    See #anchor

  • background (String)
  • colour (Hash|NilClass)
  • foreground (String)
  • model (Object)
    Vedeu::Views::View|Vedeu::Views::Line|Vedeu::Views::Stream
  • pad (String)
  • width (Integer)


32
33
34
35
# File 'lib/vedeu/output/text.rb', line 32

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

Instance Attribute Details

#optionsHash (readonly, protected)

Returns:

  • (Hash)


69
70
71
# File 'lib/vedeu/output/text.rb', line 69

def options
  @options
end

#valueString (readonly, protected)

Returns:

  • (String)


65
66
67
# File 'lib/vedeu/output/text.rb', line 65

def value
  @value
end

Class Method Details

.add(value = '', options = {}) ⇒ Object

See Also:



10
11
12
# File 'lib/vedeu/output/text.rb', line 10

def self.add(value = '', options = {})
  new(value, options).add
end

.with(value = '', options = {}) ⇒ Object

See Also:



15
16
17
# File 'lib/vedeu/output/text.rb', line 15

def self.with(value = '', options = {})
  new(value, options).aligned
end

Instance Method Details

#addvoid

This method returns an undefined value.

Adds the content to the model.



57
58
59
# File 'lib/vedeu/output/text.rb', line 57

def add
  model.add(content)
end

#alignedString

Aligns the value.

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vedeu/output/text.rb', line 40

def aligned
  return string unless width

  return truncated if truncate?

  case anchor
  when :align, :left, :text then left
  when :centre, :center     then centre
  when :right               then right
  else
    left
  end
end

#anchorSymbol (private)

Returns One of :align, :centre, :center, :left, :right, :text.

Returns:

  • (Symbol)

    One of :align, :centre, :center, :left, :right, :text



75
76
77
# File 'lib/vedeu/output/text.rb', line 75

def anchor
  options[:anchor]
end

#centreString (private)

The string padded to width, centralized.

Returns:

  • (String)


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

def centre
  string.center(width, pad)
end

#colourvoid (private)

This method returns an undefined value.

If a colour, background or foreground option is set, use them as the colour settings for the new Vedeu::Views::Stream.



90
91
92
93
94
95
96
97
98
# File 'lib/vedeu/output/text.rb', line 90

def colour
  if options[:colour] || options[:background] || options[:foreground]
    Vedeu::Colours::Colour.coerce(options)

  else
    model.colour

  end
end

#contentVedeu::Views::Line|Vedeu::Views::Stream (private)

Returns either a Vedeu::Views::Line or Vedeu::Views::Stream containing the text value.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vedeu/output/text.rb', line 104

def content
  if model.is_a?(Vedeu::Views::View)
    stream.parent = line
    line.add(stream)
    line

  else
    stream

  end
end

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

The default values for a new instance of this class.

Returns:

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


119
120
121
122
123
124
125
126
127
# File 'lib/vedeu/output/text.rb', line 119

def defaults
  {
    anchor: :left,
    colour: nil,
    model:  nil,
    pad:    ' ',
    width:  nil,
  }
end

#leftString (private)

The string padded to width, left justified.

Returns:

  • (String)


132
133
134
# File 'lib/vedeu/output/text.rb', line 132

def left
  string.ljust(width, pad)
end

#lineVedeu::Views::Line (private)

Returns:



137
138
139
# File 'lib/vedeu/output/text.rb', line 137

def line
  @line ||= Vedeu::Views::Line.build(parent: parent)
end

#modelVedeu::Views::View| Vedeu::Views::Line| Vedeu::Null::Generic| Vedeu::Views::Stream (private)

Returns the model option when set.



147
148
149
# File 'lib/vedeu/output/text.rb', line 147

def model
  @model ||= options[:model] || Vedeu::Null::Generic.new
end

#padString (private)

The character to use for padding the string.

Returns:

  • (String)


154
155
156
# File 'lib/vedeu/output/text.rb', line 154

def pad
  options[:pad]
end

#parentvoid (private)

This method returns an undefined value.

Returns the parent for the new Vedeu::Views::Stream.



161
162
163
# File 'lib/vedeu/output/text.rb', line 161

def parent
  model.is_a?(Vedeu::Views::Stream) ? model.parent : model
end

#rightString (private)

The string padded to width, right justified.

Returns:

  • (String)


168
169
170
# File 'lib/vedeu/output/text.rb', line 168

def right
  string.rjust(width, pad)
end

#streamvoid (private)

This method returns an undefined value.

Builds and returns a new Vedeu::Views::Stream.



175
176
177
178
179
180
# File 'lib/vedeu/output/text.rb', line 175

def stream
  @stream ||= Vedeu::Views::Stream.build(colour: colour,
                                         parent: parent,
                                         style:  style,
                                         value:  aligned)
end

#stringString (private)

The string, coerced.

Returns:

  • (String)


185
186
187
# File 'lib/vedeu/output/text.rb', line 185

def string
  value.to_s
end

#stylevoid (private)

This method returns an undefined value.

Returns the model’s styles.



192
193
194
# File 'lib/vedeu/output/text.rb', line 192

def style
  model.style
end

#truncate?Boolean (private)

Return a boolean indicating that the string is greater than the width.

Returns:

  • (Boolean)


200
201
202
# File 'lib/vedeu/output/text.rb', line 200

def truncate?
  string.size > width
end

#truncatedString (private)

Return the string truncated to the width.

Returns:

  • (String)


207
208
209
# File 'lib/vedeu/output/text.rb', line 207

def truncated
  string.slice(0, width)
end

#widthFixnum (private)

Return the width.

Returns:

  • (Fixnum)


214
215
216
# File 'lib/vedeu/output/text.rb', line 214

def width
  options[:width]
end