Class: Vedeu::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 = {}) ⇒ Text

Returns a new instance of Vedeu::Text.

Parameters:

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

Options Hash (options):



28
29
30
31
# File 'lib/vedeu/output/text.rb', line 28

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

Instance Attribute Details

#optionsHash (readonly, protected)

Returns:

  • (Hash)


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

def options
  @options
end

#valueString (readonly, protected)

Returns:

  • (String)


61
62
63
# File 'lib/vedeu/output/text.rb', line 61

def value
  @value
end

Class Method Details

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

See Also:



7
8
9
# File 'lib/vedeu/output/text.rb', line 7

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

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

See Also:



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

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.



53
54
55
# File 'lib/vedeu/output/text.rb', line 53

def add
  model.add(content)
end

#alignedString

Aligns the value.

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vedeu/output/text.rb', line 36

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



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

def anchor
  options[:anchor]
end

#centreString (private)

The string padded to width, centralized.

Returns:

  • (String)


77
78
79
# File 'lib/vedeu/output/text.rb', line 77

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::Stream.



85
86
87
88
89
90
91
92
93
# File 'lib/vedeu/output/text.rb', line 85

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

  else
    model.colour

  end
end

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

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



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vedeu/output/text.rb', line 98

def content
  if model.is_a?(Vedeu::Interface)
    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>)


113
114
115
116
117
118
119
120
121
# File 'lib/vedeu/output/text.rb', line 113

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

#leftString (private)

The string padded to width, left justified.

Returns:

  • (String)


126
127
128
# File 'lib/vedeu/output/text.rb', line 126

def left
  string.ljust(width, pad)
end

#lineVedeu::Line (private)

Returns:



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

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

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

Returns the model option if set.



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

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

#padString (private)

The character to use for padding the string.

Returns:

  • (String)


145
146
147
# File 'lib/vedeu/output/text.rb', line 145

def pad
  options[:pad]
end

#parentvoid (private)

This method returns an undefined value.

Returns the parent for the new Vedeu::Stream.



152
153
154
# File 'lib/vedeu/output/text.rb', line 152

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

#rightString (private)

The string padded to width, right justified.

Returns:

  • (String)


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

def right
  string.rjust(width, pad)
end

#streamvoid (private)

This method returns an undefined value.

Builds and returns a new Vedeu::Stream.



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

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

#stringString (private)

The string, coerced.

Returns:

  • (String)


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

def string
  value.to_s
end

#stylevoid (private)

This method returns an undefined value.

Returns the model’s styles.



183
184
185
# File 'lib/vedeu/output/text.rb', line 183

def style
  model.style
end

#truncate?Boolean (private)

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

Returns:

  • (Boolean)


190
191
192
# File 'lib/vedeu/output/text.rb', line 190

def truncate?
  string.size > width
end

#truncatedString (private)

Return the string truncated to the width.

Returns:

  • (String)


197
198
199
# File 'lib/vedeu/output/text.rb', line 197

def truncated
  string.slice(0, width)
end

#widthFixnum (private)

Return the width.

Returns:

  • (Fixnum)


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

def width
  options[:width]
end