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

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

#background(value = '') ⇒ String Also known as: bg, bgcolor, background=, bg=, bgcolor= Originally defined in module Presentation

Note:

The last defined background colour for a particular interface, line or stream overrides previously defined entries in the same block.

Define the background colour for an interface, line, or a stream. When called with a block, will create a new stream with the background colour specified. When the block terminates, the background will return to that of the parent.

Examples:

interface 'my_interface' do
  background '#0022ff' # /or/ (blue)
  bgcolor    '#22ff00' # /or/ (blue is overridden to green)
  bg         '#ff0022' #      (green is overridden to red)
  # ...

  lines do
    background '#2200ff'
    # ...

    stream do
      background '#22ff00'
      # ...
    end
  end
end

Parameters:

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

    A HTML/CSS value.

Returns:

  • (String)

#become(klass, attributes) ⇒ Class Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts one class into another.

Parameters:

  • klass (Class)

    The class to become an instance of.

  • attributes (Hash)

    The attributes of klass.

Returns:

  • (Class)

    Returns a new instance of klass.

#boolean(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating the value was a boolean.

Parameters:

  • value (void)

Returns:

#boolean?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value is a Boolean.

Parameters:

Returns:

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



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

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

  text(value, opts)
end

#colour(attrs = {}) ⇒ Vedeu::Colours::Colour Also known as: colour= Originally defined in module Presentation

Note:

Rejects invalid keys and empty/nil attributes. Also, the last defined colour for a particular interface, line or stream overrides previously defined entries in the same block.

Define either or both foreground and background colours for an interface, line or a stream. At least one attribute is required.

Examples:

interface 'my_interface' do
  colour background: '#ff00ff', foreground: '#ffff00'
  # ...

  lines do
    colour background: '#000000', foreground: '#ffffff'
    # ...

    stream do
      colour background: '#000000', foreground: '#ffffff'
      # ...
    end
  end
end

Parameters:

Returns:

#colour_attributesHash<Symbol => String> (private) Originally defined in module Presentation

Returns:

  • (Hash<Symbol => String>)

#escape?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value is an escape sequence object (e.g. Vedeu::Cells::Escape.)

Returns:

#falsy?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value should be considered false.

Parameters:

  • value (void)

Returns:

#foreground(value = '') ⇒ Object Also known as: fg, fgcolor, foreground=, fg=, fgcolor= Originally defined in module Presentation

See Also:

#hash?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value is a Hash.

Parameters:

  • value (Hash|void)

Returns:

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



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

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:



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
    fail Vedeu::Error::Fatal,
         "Cannot add line to '#{model.class.name}' model."

  end
end

#line_model?Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating the model is a Views::Line.

Returns:

#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

#numeric?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value is a Fixnum.

Parameters:

  • value (Fixnum|void)

Returns:

#present?(variable) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

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

Parameters:

  • block (Proc)

Returns:

  • (NilClass)

Raises:



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

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

#requires_model!NilClass (private)

Returns:

  • (NilClass)

Raises:



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

def requires_model!
  fail 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.



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

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

  text(value, opts)
end

#snake_case(klass) ⇒ String Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

snake_case('MyClassName') # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • klass (Module|Class|String)

Returns:

  • (String)

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



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
186
# 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
    fail Vedeu::Error::Fatal,
         "Cannot add stream to '#{model.class.name}' model."

  end
end

#stream_model?Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating the model is a Views::Stream.

Returns:

#string?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value is a Fixnum.

Parameters:

  • value (String|void)

Returns:

#style(value) ⇒ Vedeu::Presentation::Style Also known as: style=, styles, styles= Originally defined in module Presentation

Define a style or styles for an interface, line or a stream.

Examples:

interface 'my_interface' do
  style 'normal'
  # ...
end

lines do
  style ['bold', 'underline']
  # ...
end

stream do
  style 'blink'
  # ...
end

Parameters:

  • value (Array<Symbol>|Array<String>|Symbol|String)

Returns:

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



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

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
    fail Vedeu::Error::Fatal,
         "Cannot add text to '#{model.class.name}' model."

  end
end

#truthy?(value) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the value should be considered true.

Parameters:

  • value (void)

Returns:

#view_model?Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating the model is a Views::View.

Returns: