Module: Vedeu::DSL::Elements

Includes:
Common, Presentation
Included in:
Views, Views::Line::DSL, Views::Stream::DSL, Views::View::DSL
Defined in:
lib/vedeu/dsl/elements.rb

Overview

TODO:

This documentation needs editing and is out of date.

(GL: 2015-12-25)

Provides methods to be used to define views.

Vedeu.renders do
  view :my_interface do
    lines do
      background '#000000'
      foreground '#ffffff'
      line 'This is white text on a black background.'
      line 'Next is a blank line:'
      line ''

      streams { stream 'We can define ' }

      streams do
        foreground '#ff0000'
        stream 'parts of a line '
      end

      streams { stream 'independently using ' }

      streams do
        foreground '#00ff00'
        stream 'streams.'
      end
    end
  end
end

Instance Method Summary collapse

Methods included from Presentation

#background, #colour, #colour_attributes, #foreground, #no_wordwrap!, #style, #wordwrap

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Instance Method Details

#build_attributes(value = '', opts = {}, &block) ⇒ Vedeu::DSL::Attributes (private)

Parameters:

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

    The value for the stream.

  • opts (Hash) (defaults to: {})
  • block (Proc)

Options Hash (opts):

  • ... (void)

Returns:



282
283
284
# File 'lib/vedeu/dsl/elements.rb', line 282

def build_attributes(value = '', opts = {}, &block)
  Vedeu::DSL::Attributes.build(self, model, value, opts, &block)
end

#build_line(attrs, &block) ⇒ Vedeu::Views::Line (private)

Parameters:

Returns:



289
290
291
# File 'lib/vedeu/dsl/elements.rb', line 289

def build_line(attrs, &block)
  Vedeu::Views::Line.build(attrs, &block)
end

#build_stream(attrs, &block) ⇒ Vedeu::Views::Stream (private)

Parameters:

Returns:



296
297
298
# File 'lib/vedeu/dsl/elements.rb', line 296

def build_stream(attrs, &block)
  Vedeu::Views::Stream.build(attrs, &block)
end

#build_view(attrs, &block) ⇒ Vedeu::Views::View (private)

Parameters:

Returns:



303
304
305
# File 'lib/vedeu/dsl/elements.rb', line 303

def build_view(attrs, &block)
  Vedeu::Views::View.build(attrs, &block)
end

#centre(value = '', opts = {}) ⇒ void Also known as: center

This method returns an undefined value.

Parameters:

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

    A string or object that responds to ‘to_s`.

  • opts (Hash<Symbol => void>) (defaults to: {})

    Text options.



258
259
260
# File 'lib/vedeu/dsl/elements.rb', line 258

def centre(value = '', opts = {})
  text(value, opts.merge(align: :centre))
end

#left(value = '', opts = {}) ⇒ void

This method returns an undefined value.

Parameters:

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

    A string or object that responds to ‘to_s`.

  • opts (Hash<Symbol => void>) (defaults to: {})

    Text options.



265
266
267
# File 'lib/vedeu/dsl/elements.rb', line 265

def left(value = '', opts = {})
  text(value, opts.merge(align: :left))
end

#line(value = '', opts = {}, &block) ⇒ void

TODO:

This documentation needs editing. (GL: 2015-12-17)

This method returns an undefined value.

Specify a single line in a view.

Vedeu.renders do
  view :my_interface do
    lines do
      line 'some text...'
      # ... some code

      line 'some more text...'
      # ... some code
    end
  end
end

Parameters:

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

    The value for the line. Ignored when a block is given.

  • opts (Hash) (defaults to: {})
  • block (Proc)

Options Hash (opts):

  • ... (void)

Raises:

  • (Vedeu::Error::Fatal)

    When Vedeu does not understand that which the client application is attempting to achieve.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vedeu/dsl/elements.rb', line 114

def line(value = '', opts = {}, &block)
  requires_model!

  attrs = build_attributes(value, opts, &block)

  l = if block_given?
        build_line(attrs, &block)

      else
        new_line(attrs)

      end

  if view_model?
    model.add(l)

  elsif line_model?
    model.add(l.value)

  else
    raise Vedeu::Error::Fatal,
          "Cannot add line to '#{model.class.name}' model."

  end
end

#lines(opts = {}, &block) ⇒ void Also known as: streams

TODO:

This documentation needs editing. (GL: 2015-12-17)

This method returns an undefined value.

Specify multiple lines in a view.

Examples:

Vedeu.view :my_interface do
  lines do
    # ... see {Vedeu::DSL::Line} and {Vedeu::DSL::Stream}
  end
end

Vedeu.view :my_interface do
  line do
    # ... see {Vedeu::DSL::Line} and {Vedeu::DSL::Stream}
  end
end

Parameters:

  • block (Proc)

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vedeu/dsl/elements.rb', line 66

def lines(opts = {}, &block)
  requires_block!(&block)
  requires_model!

  attrs = build_attributes(nil, opts, &block)

  if view_model?
    if model.lines?
      model.add(build_line(attrs, &block))

    else
      model.value = build_view(attrs, &block).value

    end

  elsif line_model?
    model.value = build_line(attrs, &block).value

  else
    model.value = build_view(attrs, &block).value

  end
end

#new_line(attrs) ⇒ Vedeu::Views::Line (private)

Parameters:

Returns:



309
310
311
# File 'lib/vedeu/dsl/elements.rb', line 309

def new_line(attrs)
  Vedeu::Views::Line.new(attrs.merge(value: new_streams(attrs)))
end

#new_stream(attrs) ⇒ Vedeu::Views::Stream (private)

Parameters:

Returns:



315
316
317
# File 'lib/vedeu/dsl/elements.rb', line 315

def new_stream(attrs)
  Vedeu::Views::Stream.new(attrs)
end

#new_streams(attrs) ⇒ Vedeu::Views::Streams (private)

Parameters:

Returns:



321
322
323
# File 'lib/vedeu/dsl/elements.rb', line 321

def new_streams(attrs)
  Vedeu::Views::Streams.coerce([new_stream(attrs)])
end

#requires_block!(&block) ⇒ NilClass (private)

Parameters:

  • block (Proc)

Returns:

  • (NilClass)

Raises:



328
329
330
# File 'lib/vedeu/dsl/elements.rb', line 328

def requires_block!(&block)
  raise Vedeu::Error::RequiresBlock unless block_given?
end

#requires_model!NilClass (private)

Returns:

  • (NilClass)

Raises:

  • (Vedeu::Error::Fatal)

    When Vedeu does not understand that which the client application is attempting to achieve.



334
335
336
337
# File 'lib/vedeu/dsl/elements.rb', line 334

def requires_model!
  raise Vedeu::Error::Fatal,
        'No model, cannot continue.' unless present?(model)
end

#right(value = '', opts = {}) ⇒ void

This method returns an undefined value.

Parameters:

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

    A string or object that responds to ‘to_s`.

  • opts (Hash<Symbol => void>) (defaults to: {})

    Text options.



271
272
273
# File 'lib/vedeu/dsl/elements.rb', line 271

def right(value = '', opts = {})
  text(value, opts.merge(align: :right))
end

#stream(value = '', opts = {}, &block) ⇒ void

This method returns an undefined value.

Parameters:

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

    The value for the stream. Ignored when a block is given.

  • opts (Hash) (defaults to: {})
  • block (Proc)

Options Hash (opts):

  • ... (void)

Raises:

  • (Vedeu::Error::Fatal)

    When Vedeu does not understand that which the client application is attempting to achieve.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vedeu/dsl/elements.rb', line 147

def stream(value = '', opts = {}, &block)
  requires_model!

  attrs = build_attributes(value, opts, &block)

  m = if block_given?
        if view_model?
          build_line(attrs, &block)

        else
          build_stream(attrs, &block)

        end

      else
        new_line(attrs)

      end

  if view_model? || line_model? || stream_model?
    model.add(m)

  else
    raise Vedeu::Error::Fatal,
          "Cannot add stream to '#{model.class.name}' model."

  end
end

#text(value = '', opts = {}) ⇒ void

TODO:

This documentation needs editing. (GL: 2015-12-17)

Note:

If using the convenience methods; left, centre, center or right, then a specified align option will be ignored.

This method returns an undefined value.

Specify the content for a view. Provides the means to align a string (or object responding to ‘to_s`), and add it as a Line or to the Stream.

Examples:

lines do
  centre '...'
end

line do
  right '...'
end

line do
  stream do
    text '...'
  end
end

left 'This will be left aligned.', width: 35
# => 'This will be left aligned.         '

centre 'This will be aligned centrally.', width: 35
# => '  This will be aligned centrally.  '
# centre is also aliased to center

right 'This will be right aligned.', width: 35
# => '        This will be right aligned.'

right 'This will be right aligned.', width: 35,
  align: centre

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

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

Parameters:

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

    A string or object that responds to ‘to_s`.

  • opts (Hash<Symbol => void>) (defaults to: {})

    Text options.

Options Hash (opts):

  • :align (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.

Raises:

  • (Vedeu::Error::Fatal)

    When Vedeu does not understand that which the client application is attempting to achieve.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/vedeu/dsl/elements.rb', line 235

def text(value = '', opts = {})
  requires_model!

  attrs = build_attributes(value, opts)

  if view_model?
    model.add(new_line(attrs))

  elsif line_model?
    model.add(new_stream(attrs))

  elsif stream_model?
    model.add(new_stream(attrs).value)

  else
    raise Vedeu::Error::Fatal,
          "Cannot add text to '#{model.class.name}' model."

  end
end