Class: Vedeu::Geometry::DSL

Inherits:
Object
  • Object
show all
Includes:
Common, DSL, DSL::Use
Defined in:
lib/vedeu/geometry/dsl.rb

Overview

Geometry allows the configuration of the position and size of an interface. Within Vedeu, as the same for ANSI terminals, has the origin at top-left, y = 1, x = 1. The ‘y’ coordinate is deliberately first.

The Geometry DSL can be used within the Interface DSL or standalone. Here are example of declarations for a ‘geometry` block:

A standalone geometry definition:

Vedeu.geometry :some_interface do
  height 5 # Sets the height of the view to 5
  width 20 # Sets the width of the view to 20
  x 3      # Start drawing 3 spaces from left
  y 10     # Start drawing 10 spaces from top
  xn 30    # Stop drawing 30 spaces from the left
  yn 20    # Stop drawing 20 spaces from top
end

An interface including a geometry definition:

Vedeu.interface :some_interface do
  geometry do
    height 5
    width 20
    x 3
    y 10
    xn 30
    yn 20
  end
  # ... some code here
end

If a declaration is omitted for ‘height` or `width` the full remaining space available in the terminal will be used. `x` and `y` both default to 1.

You can also make a geometry declaration dependent on another view:

Vedeu.interface :other_panel do
  # ... some code here
end

Vedeu.interface :main do
  geometry do
    height 10
    y { use(:other_panel).south }
  end
  # ... some code here
end

This view will begin just below “other_panel”.

This crude ASCII diagram represents a Geometry within Vedeu, each of the labels is a value you can access or define.

     x    north    xn           # north:  y - 1
   y +--------------+           # top:    y
     |     top      |           # west:   x - 1
     |              |           # left:   x
west | left   right | east      # right:  xn
     |              |           # east:   xn + 1
     |    bottom    |           # bottom: yn
  yn +--------------+           # south:  yn + 1
          south

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Vedeu::DSL

Instance Attribute Details

#clientObject (readonly, protected) Originally defined in module DSL

Returns The object instance where the DSL is being used.

Returns:

  • (Object)

    The object instance where the DSL is being used.

#modelvoid (readonly, protected) Originally defined in module DSL

This method returns an undefined value.

Returns The new model object which the DSL is constructing.

Class Method Details

.geometry(name, &block) ⇒ Vedeu::Geometry::Geometry

Specify the geometry of an interface or view with a simple DSL.

Vedeu.geometry :some_interface do
  # ...
end

Parameters:

  • name (String|Symbol)

    The name of the interface or view to which this geometry belongs.

  • block (Proc)

Returns:

Raises:



92
93
94
95
96
# File 'lib/vedeu/geometry/dsl.rb', line 92

def self.geometry(name, &block)
  fail Vedeu::Error::RequiresBlock unless block_given?

  Vedeu::Geometry::Geometry.build(name: name, &block).store
end

Instance Method Details

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

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

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#align(vertical, horizontal, width, height) ⇒ Vedeu::Geometry::Geometry

Align the interface/view horizontally or vertically within the terminal.

Parameters:

  • vertical (Symbol)

    One of :bottom, :middle, :none, :top.

  • horizontal (Symbol)

    One of :center, :centre, :left, :none, :right.

  • width (Fixnum)

    The number of characters/columns wide; this is required when the given value for horizontal is any value other than :none.

  • height (Fixnum)

    The number of lines/rows tall; this is required when the given value for vertical is any value other than :none.

Returns:

Raises:

  • (Vedeu::Error::InvalidSyntax)
    • When the vertical is not given.

    • When the horizontal is not given.

    • When the horizontal is given (and not :none) and the width is not given.

    • When the vertical is given (and not :none) and the height is not given.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vedeu/geometry/dsl.rb', line 149

def align(vertical, horizontal, width, height)
  fail Vedeu::Error::InvalidSyntax,
       'No vertical alignment given. Valid values are :bottom, ' \
       ':middle, :none, :top.'.freeze unless present?(vertical)
  fail Vedeu::Error::InvalidSyntax,
       'No horizontal alignment given. Valid values are :center, ' \
       ':centre, :left, :none, :right.'.freeze unless present?(horizontal)

  unless vertical == :none
    fail Vedeu::Error::InvalidSyntax,
         'No height given.'.freeze if absent?(height)
  end

  unless horizontal == :none
    fail Vedeu::Error::InvalidSyntax,
         'No width given.'.freeze if absent?(width)
  end

  horizontal_alignment(horizontal, width)
  vertical_alignment(vertical, height)
end

#align_bottom(height) ⇒ Vedeu::Geometry::Geometry

Note:

Vedeu.width in the example above will set the width to the default width of the terminal, this can be substituted for your own positive integer.

Vertically align the interface/view to the bottom of the terminal.

Vedeu.geometry :some_interface do
  # `height` is a positive integer, e.g. 30
  align_bottom 30

  # this is the same as:
  # vertical_alignment(:bottom, 30)

  # or you can use: (see notes)
  # align(:bottom, :none, Vedeu.width, 30)

  # ... some code
end

Parameters:

  • height (Fixnum)

    The number of lines/rows.

Returns:

Raises:



228
229
230
# File 'lib/vedeu/geometry/dsl.rb', line 228

def align_bottom(height)
  vertical_alignment(:bottom, height)
end

#align_centre(width) ⇒ Vedeu::Geometry::Geometry Also known as: align_center

Note:

Vedeu.height in the example above will set the height to the default height of the terminal, this can be substituted for your own positive integer.

Horizontally align the interface/view centrally.

Vedeu.geometry :some_interface do
  # `width` is a positive integer, e.g. 30
  align_centre 30

  # this is the same as:
  # horizontal_alignment(:centre, 30)

  # or you can use: (see notes)
  # align(:none, :centre, 30, Vedeu.height)

  # ... some code
end

# Also allows `align_center` if preferred.

Parameters:

  • width (Fixnum)

    The number of characters/columns.

Returns:

Raises:



257
258
259
# File 'lib/vedeu/geometry/dsl.rb', line 257

def align_centre(width)
  horizontal_alignment(:centre, width)
end

#align_left(width) ⇒ Vedeu::Geometry::Geometry

Note:

Vedeu.height in the example above will set the height to the default height of the terminal, this can be substituted for your own positive integer.

Horizontally align the interface/view to the left.

Vedeu.geometry :some_interface do
  # `width` is a positive integer, e.g. 30
  align_left 30

  # this is the same as:
  # horizontal_alignment(:left, 30)

  # or you can use: (see notes)
  # align(:none, :left, 30, Vedeu.height)

  # ... some code
end

Parameters:

  • width (Fixnum)

    The number of characters/columns.

Returns:

Raises:



285
286
287
# File 'lib/vedeu/geometry/dsl.rb', line 285

def align_left(width)
  horizontal_alignment(:left, width)
end

#align_middle(height) ⇒ Vedeu::Geometry::Geometry

Note:

Vedeu.width in the example above will set the width to the default width of the terminal, this can be substituted for your own positive integer.

Vertically align the interface/view to the middle of the terminal.

Vedeu.geometry :some_interface do
  # `height` is a positive integer, e.g. 30
  align_middle 30

  # this is the same as:
  # vertical_alignment(:middle, 30)

  # or you can use: (see notes)
  # align(:middle, :none, Vedeu.width, 30)

  # ... some code
end

Parameters:

  • height (Fixnum)

    The number of lines/rows.

Returns:

Raises:



313
314
315
# File 'lib/vedeu/geometry/dsl.rb', line 313

def align_middle(height)
  vertical_alignment(:middle, height)
end

#align_right(width) ⇒ Vedeu::Geometry::Geometry

Note:

Vedeu.height in the example above will set the height to the default height of the terminal, this can be substituted for your own positive integer.

Align the interface/view to the right.

Vedeu.geometry :some_interface do
  # `width` is a positive integer, e.g. 30
  align_right 30

  # this is the same as:
  # horizontal_alignment(:right, 30)

  # or you can use: (see notes)
  # align(:none, :right, 30, Vedeu.height)

  # ... some code
end

Parameters:

  • width (Fixnum)

    The number of characters/columns.

Returns:

Raises:



340
341
342
# File 'lib/vedeu/geometry/dsl.rb', line 340

def align_right(width)
  horizontal_alignment(:right, width)
end

#align_top(height) ⇒ Vedeu::Geometry::Geometry

Note:

Vedeu.width in the example above will set the width to the default width of the terminal, this can be substituted for your own positive integer.

Vertically align the interface/view to the top of the terminal.

Vedeu.geometry :some_interface do
  # `height` is a positive integer, e.g. 30
  align_top 30

  # this is the same as:
  # vertical_alignment(:top, 30)

  # or you can use: (see notes)
  # align(:top, :none, Vedeu.width, 30)

  # ... some code
end

Parameters:

  • height (Fixnum)

    The number of lines/rows.

Returns:

Raises:



368
369
370
# File 'lib/vedeu/geometry/dsl.rb', line 368

def align_top(height)
  vertical_alignment(:top, height)
end

#attributesHash<Symbol => void> (private) Originally defined in module DSL

Note:

Specific DSL classes may be overriding this method.

Returns the default attributes for the new model.

Returns:

  • (Hash<Symbol => void>)

#columns(value) ⇒ Fixnum|Vedeu::Error::OutOfRange

Returns the width in characters for the number of columns specified.

Vedeu.geometry :main_interface do
  # ... some code
  width columns(9) # Vedeu.width # => 92 (for example)
                   # 92 / 12 = 7
                   # 7 * 9 = 63
                   # Therefore, width is 63 characters.
end

Parameters:

  • value (Fixnum)

Returns:

Raises:



387
388
389
# File 'lib/vedeu/geometry/dsl.rb', line 387

def columns(value)
  Vedeu::Geometry::Grid.columns(value)
end

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

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#height(value) ⇒ Fixnum Also known as: height=

Specify the number of characters/rows/lines tall the interface will be. This value will be ignored when ‘y` and `yn` are set.

Vedeu.geometry :some_interface do
  height 8
  # ... some code
end

Parameters:

  • value (Fixnum)

Returns:

  • (Fixnum)


401
402
403
# File 'lib/vedeu/geometry/dsl.rb', line 401

def height(value)
  model.height = proc { value }
end

#horizontal_alignment(value, width) ⇒ Vedeu::Geometry::Geometry

Parameters:

  • value (Symbol)

    One of :center, :centre, :left, :none, :right.

  • width (Fixnum)

    The number of characters/columns.

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/vedeu/geometry/dsl.rb', line 175

def horizontal_alignment(value, width)
  fail Vedeu::Error::InvalidSyntax,
       'No horizontal alignment given. Valid values are :center, ' \
       ':centre, :left, :none, :right.'.freeze unless present?(value)
  fail Vedeu::Error::InvalidSyntax,
       'No width given.'.freeze unless present?(width)

  model.horizontal_alignment = Vedeu::Geometry::HorizontalAlignment
    .coerce(value)
  model.width = width
  model
end

#initialize(model, client = nil) ⇒ void Originally defined in module DSL

Returns an instance of the DSL class including Vedeu::DSL.

Parameters:

  • model (void)

    The model class which the DSL class is wrapping.

  • client (void) (defaults to: nil)

    The class where the DSL methods are being used.

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

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

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#rows(value) ⇒ Fixnum

Returns the height in characters for the number of rows specified.

Vedeu.geometry :main_interface do
  # ... some code
  height rows(3)  # Vedeu.height # => 38 (for example)
                  # 38 / 12 = 3
                  # 3 * 3 = 9
                  # Therefore, height is 9 characters.
end

Parameters:

  • value (Fixnum)

Returns:

  • (Fixnum)

Raises:



421
422
423
# File 'lib/vedeu/geometry/dsl.rb', line 421

def rows(value)
  Vedeu::Geometry::Grid.rows(value)
end

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

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"

Parameters:

  • name (String)

Returns:

  • (String)

#use(name) ⇒ void Originally defined in module DSL::Use

Note:
  • Only models of the same repository can be used in this way.

  • If the stored model cannot be found, a ModelNotFound exception may be raised, or the request for an attribute may raise a NoMethodError exception.

This method returns an undefined value.

Use the attribute of stored model.

This DSL method provides access to a stored model by name. You can then request an attribute of that model for use within the current model. The models which current support this are Border, Geometry and Interface.

Examples:

# Here the character used for :my_border is used in
# :my_other_border.
Vedeu.border :my_other_border do
  top_right use(:my_border).top_right
end

Parameters:

  • name (String|Symbol)

    The name of the model with the value you wish to use.

Raises:

  • Vedeu::Error::ModelNotFound|Vedeu::Error::NoMethodError

    The model or attribute cannot be found.

#vertical_alignment(value, height) ⇒ Vedeu::Geometry::Geometry

Parameters:

  • value (Symbol)

    One of :bottom, :middle, :none, :top.

  • height (Fixnum)

    The number of lines/rows.

Returns:



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/vedeu/geometry/dsl.rb', line 191

def vertical_alignment(value, height)
  fail Vedeu::Error::InvalidSyntax,
       'No vertical alignment given. Valid values are :bottom, ' \
       ':middle, :none, :top.'.freeze unless present?(value)
  fail Vedeu::Error::InvalidSyntax,
       'No height given.'.freeze unless present?(height)

  model.vertical_alignment = Vedeu::Geometry::VerticalAlignment
    .coerce(value)
  model.height = height
  model
end

#width(value) ⇒ Fixnum Also known as: width=

Specify the number of characters/columns wide the interface will be. This value will be ignored when ‘x` and `xn` are set.

Vedeu.geometry :some_interface do
  width 25
  # ... some code
end

Parameters:

  • value (Fixnum)

    The number of characters/columns.

Returns:

  • (Fixnum)


435
436
437
# File 'lib/vedeu/geometry/dsl.rb', line 435

def width(value)
  model.width = proc { value }
end

#x(value = 1, &block) ⇒ Fixnum Also known as: x=

Specify the starting x position (column) of the interface.

Vedeu.geometry :some_interface do
  x 7 # start on column 7.

  # start on column 8, if :other_interface changes position
  # then :some_interface will too.
  x { use(:other_interface).east }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


454
455
456
457
458
# File 'lib/vedeu/geometry/dsl.rb', line 454

def x(value = 1, &block)
  return model.x = block if block_given?

  model.x = value
end

#xn(value = 1, &block) ⇒ Fixnum Also known as: xn=

Specify the ending x position (column) of the interface. This value will override ‘width`.

Vedeu.geometry :some_interface do
  xn 37 # end at column 37.

  # when :other_interface changes position,
  # :some_interface will too.
  xn  { use(:other_interface).right }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


476
477
478
479
480
# File 'lib/vedeu/geometry/dsl.rb', line 476

def xn(value = 1, &block)
  return model.xn = block if block_given?

  model.xn = value
end

#y(value = 1, &block) ⇒ Fixnum Also known as: y=

Specify the starting y position (row/line) of the interface.

Vedeu.geometry :some_interface do
  y  4 # start at row 4

  # start on row/line 3, when :other_interface changes
  # position, :some_interface will too.
  y  { use(:other_interface).north }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


497
498
499
500
501
# File 'lib/vedeu/geometry/dsl.rb', line 497

def y(value = 1, &block)
  return model.y = block if block_given?

  model.y = value
end

#yn(value = 1, &block) ⇒ Fixnum Also known as: yn=

Specify the ending y position (row/line) of the interface. This value will override ‘height`.

Vedeu.geometry :some_interface do
  yn 24 # end at row 24.

  # if :other_interface changes position, :some_interface
  # will too.
  yn { use(:other_interface).bottom }
  # ... some code
end

Parameters:

  • value (Fixnum) (defaults to: 1)
  • block (Proc)

Returns:

  • (Fixnum)


519
520
521
522
523
# File 'lib/vedeu/geometry/dsl.rb', line 519

def yn(value = 1, &block)
  return model.yn = block if block_given?

  model.yn = value
end