Class: Vedeu::Geometry::Dimension

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Vedeu::Geometry::Dimension.

Parameters:

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

Options Hash (attributes):

  • 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)
  • centered (Boolean)
  • alignment (Symbol)


29
30
31
32
33
# File 'lib/vedeu/geometry/dimension.rb', line 29

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

Instance Attribute Details

#alignmentSymbol (readonly, protected)

Returns:

  • (Symbol)


86
87
88
# File 'lib/vedeu/geometry/dimension.rb', line 86

def alignment
  @alignment
end

#centredBoolean (readonly, protected) Also known as: centred?

Returns:

  • (Boolean)


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

def centred
  @centred
end

#dFixnum|NilClass (readonly, protected)

Returns The starting value (y or x).

Returns:

  • (Fixnum|NilClass)

    The starting value (y or x).



60
61
62
# File 'lib/vedeu/geometry/dimension.rb', line 60

def d
  @d
end

#d_dnFixnum|NilClass (readonly, protected)

Returns A width or a height.

Returns:

  • (Fixnum|NilClass)

    A width or a height.



68
69
70
# File 'lib/vedeu/geometry/dimension.rb', line 68

def d_dn
  @d_dn
end

#defaultFixnum|NilClass (readonly, protected)

Returns The terminal width or height.

Returns:

  • (Fixnum|NilClass)

    The terminal width or height.



72
73
74
# File 'lib/vedeu/geometry/dimension.rb', line 72

def default
  @default
end

#dnFixnum|NilClass (readonly, protected)

Returns The ending value (yn or xn).

Returns:

  • (Fixnum|NilClass)

    The ending value (yn or xn).



64
65
66
# File 'lib/vedeu/geometry/dimension.rb', line 64

def dn
  @dn
end

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/vedeu/geometry/dimension.rb', line 76

def maximised
  @maximised
end

Class Method Details

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

Parameters:

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

Returns:

  • (Array<Fixnum>)


11
12
13
# File 'lib/vedeu/geometry/dimension.rb', line 11

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

Instance Method Details

#_dFixnum (private)

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

Returns:

  • (Fixnum)


207
208
209
# File 'lib/vedeu/geometry/dimension.rb', line 207

def _d
  d || 1
end

#_dnFixnum (private)

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)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/vedeu/geometry/dimension.rb', line 221

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

#centre_aligned?Boolean (private)

Return a boolean indicating alignment was set to :centre.

Returns:

  • (Boolean)


250
251
252
# File 'lib/vedeu/geometry/dimension.rb', line 250

def centre_aligned?
  alignment == :centre
end

#centred_dFixnum (private)

Ascertains the centred starting coordinate.

Examples:

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

Returns:

  • (Fixnum)


158
159
160
161
# File 'lib/vedeu/geometry/dimension.rb', line 158

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

#centred_dnFixnum (private)

Ascertains the centred ending coordinate.

Examples:

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

Returns:

  • (Fixnum)


171
172
173
174
# File 'lib/vedeu/geometry/dimension.rb', line 171

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

#d1Fixnum

Fetch the starting coordinate.

Returns:

  • (Fixnum)


38
39
40
# File 'lib/vedeu/geometry/dimension.rb', line 38

def d1
  dimension[0] < 1 ? 1 : dimension[0]
end

#d2Fixnum

Fetch the ending coordinate.

Returns:

  • (Fixnum)


45
46
47
# File 'lib/vedeu/geometry/dimension.rb', line 45

def d2
  dimension[-1]
end

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

Returns the default options/attributes for this class.

Returns:

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


264
265
266
267
268
269
270
271
272
273
274
# File 'lib/vedeu/geometry/dimension.rb', line 264

def defaults
  {
    d:         nil,
    dn:        nil,
    d_dn:      nil,
    default:   nil,
    centred:   false,
    maximised: false,
    alignment: Vedeu::Geometry::Alignment.align(:none),
  }
end

#dimensionArray<Fixnum> (private)

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>)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/vedeu/geometry/dimension.rb', line 98

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

               elsif centre_aligned?
                 [centred_d, centred_dn]

               elsif left_aligned?
                 [1, left_dn]

               elsif right_aligned?
                 [right_d, default]

               elsif centred? && length?
                 [centred_d, centred_dn]

               else
                 [_d, _dn]

               end
end

#left_aligned?Boolean (private)

Return a boolean indicating alignment was set to :left.

Returns:

  • (Boolean)


243
244
245
# File 'lib/vedeu/geometry/dimension.rb', line 243

def left_aligned?
  alignment == :left
end

#left_dnFixnum (private)

Returns:

  • (Fixnum)


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

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

  elsif dn
    dn

  else
    default

  end
end

#lengthFixnum|NilClass (private)

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)


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vedeu/geometry/dimension.rb', line 137

def length
  if d && dn
    (d..dn).size

  elsif d_dn
    d_dn

  else
    default

  end
end

#length?Boolean (private)

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)


126
127
128
# File 'lib/vedeu/geometry/dimension.rb', line 126

def length?
  default && length
end

#pairArray<Fixnum>

Fetch the coordinates.

Returns:

  • (Array<Fixnum>)


52
53
54
# File 'lib/vedeu/geometry/dimension.rb', line 52

def pair
  dimension
end

#right_aligned?Boolean (private)

Return a boolean indicating alignment was set to :right.

Returns:

  • (Boolean)


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

def right_aligned?
  alignment == :right
end

#right_dFixnum (private)

Returns:

  • (Fixnum)


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

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

  elsif d
    d

  else
    1

  end
end