Class: Vedeu::Geometry::Dimension Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vedeu/geometry/dimension.rb

Overview

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

A Dimension is either the height or width of an entity.

Direct Known Subclasses

XDimension, YDimension

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Geometry::Dimension

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 new instance of Vedeu::Geometry::Dimension.

Parameters:

  • attributes (Hash<Symbol => Fixnum, NilClass>) (defaults to: {})

Options Hash (attributes):

  • alignment (Symbol)
  • d (Fixnum|NilClass)

    The starting value (y or x).

  • dn (Fixnum|NilClass)

    The ending value (yn or xn).

  • d_dn (Fixnum|NilClass)

    A width or a height.

  • default (Fixnum|NilClass)

    The terminal width or height.

  • maximised (Boolean)


41
42
43
44
45
# File 'lib/vedeu/geometry/dimension.rb', line 41

def initialize(attributes = {})
  defaults.merge!(attributes).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#dFixnum|NilClass (readonly, protected)

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 The starting value (y or x).

Returns:

  • (Fixnum|NilClass)

    The starting value (y or x).



58
59
60
# File 'lib/vedeu/geometry/dimension.rb', line 58

def d
  @d
end

#d_dnFixnum|NilClass (readonly, protected)

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 width or a height.

Returns:

  • (Fixnum|NilClass)

    A width or a height.



66
67
68
# File 'lib/vedeu/geometry/dimension.rb', line 66

def d_dn
  @d_dn
end

#defaultFixnum|NilClass (readonly, protected)

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 The terminal width or height.

Returns:

  • (Fixnum|NilClass)

    The terminal width or height.



70
71
72
# File 'lib/vedeu/geometry/dimension.rb', line 70

def default
  @default
end

#dnFixnum|NilClass (readonly, protected)

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 The ending value (yn or xn).

Returns:

  • (Fixnum|NilClass)

    The ending value (yn or xn).



62
63
64
# File 'lib/vedeu/geometry/dimension.rb', line 62

def dn
  @dn
end

#maximisedBoolean (readonly, protected) Also known as: maximised?

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:

  • (Boolean)


74
75
76
# File 'lib/vedeu/geometry/dimension.rb', line 74

def maximised
  @maximised
end

Class Method Details

.pair(attributes = {}) ⇒ Array<Fixnum>

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.

Parameters:

  • attributes (Hash<Symbol => Fixnum, NilClass>) (defaults to: {})

Returns:

  • (Array<Fixnum>)


24
25
26
# File 'lib/vedeu/geometry/dimension.rb', line 24

def self.pair(attributes = {})
  new(attributes).pair
end

Instance Method Details

#_dFixnum (private)

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.

Fetch the starting coordinate, or use 1 when not set.

Returns:

  • (Fixnum)


212
213
214
# File 'lib/vedeu/geometry/dimension.rb', line 212

def _d
  d || 1
end

#_dnFixnum (private)

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.

Fetch the ending coordinate.

1) if an end value was given, use that. 2) if a start value wasn’t given, but a width or height was,

use the width or height.

3) if a start value was given and a width or height was given,

add the width or height to the start and minus 1.

4) otherwise, use the terminal default width or height.

Returns:

  • (Fixnum)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/vedeu/geometry/dimension.rb', line 226

def _dn
  if dn
    dn

  elsif d.nil? && d_dn
    d_dn

  elsif d && d_dn
    (d + d_dn) - 1

  elsif default
    default

  else
    1

  end
end

#alignmentVedeu::Geometry::Alignment (private)

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.



80
81
82
# File 'lib/vedeu/geometry/dimension.rb', line 80

def alignment
  Vedeu::Geometry::Alignment.coerce(@alignment)
end

#centred_dFixnum (private)

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.

Ascertains the centred starting coordinate.

Examples:

default = 78 # => 39
length = 24 # => 12
centred_d = 27 # (39 - 12 = 27)

Returns:

  • (Fixnum)


149
150
151
152
# File 'lib/vedeu/geometry/dimension.rb', line 149

def centred_d
  d = (default / 2) - (length / 2)
  d < 1 ? 1 : d
end

#centred_dnFixnum (private)

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.

Ascertains the centred ending coordinate.

Examples:

default = 78 # => 39
length = 24 # => 12
centred_dn = 51 # (39 + 12 = 51)

Returns:

  • (Fixnum)


162
163
164
165
# File 'lib/vedeu/geometry/dimension.rb', line 162

def centred_dn
  dn = (default / 2) + (length / 2)
  dn > default ? default : dn
end

#defaultsHash<Symbol => Boolean,Fixnum,NilClass,Symbol> (private)

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 the default options/attributes for this class.

Returns:

  • (Hash<Symbol => Boolean,Fixnum,NilClass,Symbol>)


248
249
250
251
252
253
254
255
256
# File 'lib/vedeu/geometry/dimension.rb', line 248

def defaults
  {
    alignment: :none,
    d:         nil,
    dn:        nil,
    d_dn:      nil,
    maximised: false,
  }
end

#dimensionArray<Fixnum> (private)

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.

Return the dimension.

1) If maximised, it will be from the first row/line or column/

character to the last row/line or column/character of the
terminal.

2) If centred,

Returns:

  • (Array<Fixnum>)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vedeu/geometry/dimension.rb', line 92

def dimension
  @dimension = if maximised?
                 [1, default]

               elsif bottom_aligned? || right_aligned?
                 [start_coordinate, default]

               elsif centre_aligned? || middle_aligned?
                 [centred_d, centred_dn]

               elsif left_aligned? || top_aligned?
                 [1, end_coordinate]

               else
                 [_d, _dn]

               end
end

#end_coordinateFixnum (private)

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.

Ascertains the ending coordinate for a left or top aligned interface/view.

1) Use the width or height (d_dn), or 2) Use the xn or yn (dn), or 3) Default to the terminal width or height.

Returns:

  • (Fixnum)


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

def end_coordinate
  if d_dn
    (d_dn > default) ? default : d_dn

  elsif dn
    dn

  else
    default

  end
end

#lengthFixnum|NilClass (private)

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.

Provide the number of rows/lines or columns/characters.

1) Use the starting (x or y) and ending (xn or yn) values. 2) Or use the width or height value. 3) Or use the default/current terminal width or height value.

Returns:

  • (Fixnum|NilClass)


128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/vedeu/geometry/dimension.rb', line 128

def length
  if d && dn
    (dn - d) + 1

  elsif d_dn
    d_dn

  else
    default

  end
end

#length?Boolean (private)

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.

TODO:

GL 2015-10-16 Investigate: should this be && or ||.

Return a boolean indicating we know the length if a we know either the terminal width or height, or we can determine a length from the values provided.

Returns:

  • (Boolean)


117
118
119
# File 'lib/vedeu/geometry/dimension.rb', line 117

def length?
  default && length
end

#pairArray<Fixnum>

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.

Fetch the coordinates.

Returns:

  • (Array<Fixnum>)


50
51
52
# File 'lib/vedeu/geometry/dimension.rb', line 50

def pair
  dimension
end

#start_coordinateFixnum (private)

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.

Ascertains the starting coordinate for a right or bottom aligned interface/view.

1) Use the width or height (d_dn), or 2) Use the x or y (d), or 3) Default to 1.

Returns:

  • (Fixnum)


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/vedeu/geometry/dimension.rb', line 196

def start_coordinate
  if d_dn
    (default - d_dn) < 1 ? 1 : (default - d_dn)

  elsif d
    d

  else
    1

  end
end