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

#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.



273
274
275
276
277
# File 'lib/vedeu/dsl/elements.rb', line 273

def centre(value = '', opts = {})
  opts[:align] = :centre

  text(value, opts)
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.



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

def left(value = '', opts = {})
  opts[:align] = :left

  text(value, opts)
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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vedeu/dsl/elements.rb', line 118

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

  attrs = Vedeu::DSL::Attributes.build(self, model, value, opts, &block)

  l = if block_given?
        Vedeu::Views::Line.build(attrs, &block)

      else
        s  = Vedeu::Views::Stream.new(attrs)
        ss = Vedeu::Views::Streams.coerce([s])
        Vedeu::Views::Line.new(attrs.merge!(value: ss))

      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
89
90
91
92
# File 'lib/vedeu/dsl/elements.rb', line 66

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

  attrs = Vedeu::DSL::Attributes.build(self, model, nil, opts, &block)

  if view_model?
    if model.lines?
      l = Vedeu::Views::Line.build(attrs, &block)
      model.add(l)

    else
      l = Vedeu::Views::View.build(attrs, &block)
      model.value = l.value

    end

  elsif line_model?
    l = Vedeu::Views::Line.build(attrs, &block)
    model.value = l.value

  else
    l = Vedeu::Views::View.build(attrs, &block)
    model.value = l.value

  end
end

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

Parameters:

  • block (Proc)

Returns:

  • (NilClass)

Raises:



301
302
303
# File 'lib/vedeu/dsl/elements.rb', line 301

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.



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

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.



290
291
292
293
294
# File 'lib/vedeu/dsl/elements.rb', line 290

def right(value = '', opts = {})
  opts[:align] = :right

  text(value, opts)
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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vedeu/dsl/elements.rb', line 153

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

  attrs = Vedeu::DSL::Attributes.build(self, model, value, opts, &block)

  l = if block_given?
        if view_model?
          Vedeu::Views::Line.build(attrs, &block)

        else
          Vedeu::Views::Stream.build(attrs, &block)

        end

      else
        s  = Vedeu::Views::Stream.new(attrs)
        ss = Vedeu::Views::Streams.coerce([s])
        Vedeu::Views::Line.new(attrs.merge!(value: ss))

      end

  if view_model?
    model.add(l)

  elsif line_model? || stream_model?
    model.add([l])

  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.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/vedeu/dsl/elements.rb', line 246

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

  attrs  = Vedeu::DSL::Attributes.build(self, model, value, opts)
  stream = Vedeu::Views::Stream.new(attrs)

  if view_model?
    ss = Vedeu::Views::Streams.coerce([stream])
    l  = Vedeu::Views::Line.new(attrs.merge!(value: ss))

    model.add(l)

  elsif line_model?
    model.add(stream)

  elsif stream_model?
    model.add(stream.value)

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

  end
end