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

#clientObject (private)

Returns:

  • (Object)


87
88
89
# File 'lib/vedeu/output/text.rb', line 87

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



95
96
97
98
99
100
101
102
103
# File 'lib/vedeu/output/text.rb', line 95

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.



109
110
111
112
113
114
115
# File 'lib/vedeu/output/text.rb', line 109

def content
  return stream unless model.is_a?(Vedeu::Views::View)

  stream.parent = line
  line.add(stream)
  line
end

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

The default values for a new instance of this class.

Returns:

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


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

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

#leftString (private)

The string padded to width, left justified.

Returns:

  • (String)


134
135
136
# File 'lib/vedeu/output/text.rb', line 134

def left
  string.ljust(width, pad)
end

#lineVedeu::Views::Line (private)

Returns:



139
140
141
# File 'lib/vedeu/output/text.rb', line 139

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

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

Returns the model option when set.



149
150
151
# File 'lib/vedeu/output/text.rb', line 149

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

#padString (private)

The character to use for padding the string.

Returns:

  • (String)


156
157
158
# File 'lib/vedeu/output/text.rb', line 156

def pad
  options[:pad]
end

#parentvoid (private)

This method returns an undefined value.

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



163
164
165
# File 'lib/vedeu/output/text.rb', line 163

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

#rightString (private)

The string padded to width, right justified.

Returns:

  • (String)


170
171
172
# File 'lib/vedeu/output/text.rb', line 170

def right
  string.rjust(width, pad)
end

#streamvoid (private)

This method returns an undefined value.

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



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

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

#stringString (private)

The string, coerced.

Returns:

  • (String)


188
189
190
# File 'lib/vedeu/output/text.rb', line 188

def string
  value.to_s
end

#stylevoid (private)

This method returns an undefined value.

Returns the model’s styles.



195
196
197
# File 'lib/vedeu/output/text.rb', line 195

def style
  model.style
end

#truncate?Boolean (private)

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

Returns:

  • (Boolean)


203
204
205
# File 'lib/vedeu/output/text.rb', line 203

def truncate?
  string.size > width
end

#truncatedString (private)

Return the string truncated to the width.

Returns:

  • (String)


210
211
212
# File 'lib/vedeu/output/text.rb', line 210

def truncated
  string.slice(0, width)
end

#widthFixnum (private)

Return the width.

Returns:

  • (Fixnum)


217
218
219
# File 'lib/vedeu/output/text.rb', line 217

def width
  options[:width]
end