Module: Vedeu::DSL::Text

Included in:
Interface, Line, Stream
Defined in:
lib/vedeu/dsl/shared/text.rb

Overview

Provides helper methods for building views.

Instance Method Summary collapse

Instance Method Details

#stream_attributes(attrs) ⇒ Hash (private)

Parameters:

  • attrs (Hash)

Returns:

  • (Hash)


94
95
96
97
98
99
100
# File 'lib/vedeu/dsl/shared/text.rb', line 94

def stream_attributes(attrs)
  {
    colour: model.colour,
    parent: model,
    style:  model.style,
  }.merge!(attrs)
end

#stream_builder(attrs) ⇒ Vedeu::Stream (private)

Parameters:

  • attrs (Hash)

Returns:



88
89
90
# File 'lib/vedeu/dsl/shared/text.rb', line 88

def stream_builder(attrs)
  Vedeu::Stream.build(stream_attributes(attrs))
end

#text(value = '', options = {}) ⇒ String Also known as: align, center, centre, left, right

right ‘This will be right aligned.’, width: 35, anchor: centre

# => '        This will be right aligned.'

text 'This will be truncated here. More text here.', width: 28
# => 'This will be truncated here.'

text 'Padded with hyphens.', width: 25, pad: '-', anchor: :right
# => '-----Padded with hyphens.'

Parameters:

  • value (String|Object) (defaults to: '')

    A string or object that responds to ‘to_s`.

  • options (Hash) (defaults to: {})

    Text options.

Options Hash (options):

  • :anchor (Symbol)

    One of ‘:left`, `:centre`/`:center`, or `:right`.

  • :width (Integer|NilClass)

    The width of the text stream to add. If the ‘string` provided is longer than this value, the string will be truncated. If no width is provided in the context of ’lines’, then the interface width is used. If no width is provided in the context of a ‘stream’, then no alignment will occur.

  • :pad (String)

    The character to use to pad the width, by default uses an empty space (0x20). Only when the string is shorter than the specified width.

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vedeu/dsl/shared/text.rb', line 58

def text(value = '', options = {})
  output = Vedeu::Text.with(value, options.merge!({ anchor: __callee__ }))

  content = if model.is_a?(Vedeu::Interface)
    stream = stream_builder({ value: output })
    Vedeu::Line.build({ streams: [stream], parent: model })

  elsif model.is_a?(Vedeu::Line)
    stream_builder({ value: output })

  elsif model.is_a?(Vedeu::Stream)
    stream_builder({ value: output, parent: model.parent })

  else
    # should never get here

  end

  model.add(content)
end