Class: Vedeu::Geometry::Dimension
- Inherits:
-
Object
- Object
- Vedeu::Geometry::Dimension
- Defined in:
- lib/vedeu/geometry/dimension.rb
Overview
A Dimension is either the height or width of an entity.
Instance Attribute Summary collapse
- #alignment ⇒ Symbol readonly protected
- #centred ⇒ Boolean (also: #centred?) readonly protected
-
#d ⇒ Fixnum|NilClass
readonly
protected
The starting value (y or x).
-
#d_dn ⇒ Fixnum|NilClass
readonly
protected
A width or a height.
-
#default ⇒ Fixnum|NilClass
readonly
protected
The terminal width or height.
-
#dn ⇒ Fixnum|NilClass
readonly
protected
The ending value (yn or xn).
- #maximised ⇒ Boolean (also: #maximised?) readonly protected
Class Method Summary collapse
Instance Method Summary collapse
-
#_d ⇒ Fixnum
private
Fetch the starting coordinate, or use 1 when not set.
-
#_dn ⇒ Fixnum
private
Fetch the ending coordinate.
-
#centre_aligned? ⇒ Boolean
private
Return a boolean indicating alignment was set to :centre.
-
#centred_d ⇒ Fixnum
private
Ascertains the centred starting coordinate.
-
#centred_dn ⇒ Fixnum
private
Ascertains the centred ending coordinate.
-
#d1 ⇒ Fixnum
Fetch the starting coordinate.
-
#d2 ⇒ Fixnum
Fetch the ending coordinate.
-
#defaults ⇒ Hash<Symbol => NilClass,Boolean>
private
Returns the default options/attributes for this class.
-
#dimension ⇒ Array<Fixnum>
private
Return the dimension.
-
#initialize(attributes = {}) ⇒ Vedeu::Geometry::Dimension
constructor
Returns a new instance of Vedeu::Geometry::Dimension.
-
#left_aligned? ⇒ Boolean
private
Return a boolean indicating alignment was set to :left.
- #left_dn ⇒ Fixnum private
-
#length ⇒ Fixnum|NilClass
private
Provide the number of rows/lines or columns/characters.
-
#length? ⇒ Boolean
private
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.
-
#pair ⇒ Array<Fixnum>
Fetch the coordinates.
-
#right_aligned? ⇒ Boolean
private
Return a boolean indicating alignment was set to :right.
- #right_d ⇒ Fixnum private
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Geometry::Dimension
Returns a new instance of Vedeu::Geometry::Dimension.
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
#alignment ⇒ Symbol (readonly, protected)
86 87 88 |
# File 'lib/vedeu/geometry/dimension.rb', line 86 def alignment @alignment end |
#centred ⇒ Boolean (readonly, protected) Also known as: centred?
81 82 83 |
# File 'lib/vedeu/geometry/dimension.rb', line 81 def centred @centred end |
#d ⇒ Fixnum|NilClass (readonly, protected)
Returns The starting value (y or x).
60 61 62 |
# File 'lib/vedeu/geometry/dimension.rb', line 60 def d @d end |
#d_dn ⇒ Fixnum|NilClass (readonly, protected)
Returns A width or a height.
68 69 70 |
# File 'lib/vedeu/geometry/dimension.rb', line 68 def d_dn @d_dn end |
#default ⇒ Fixnum|NilClass (readonly, protected)
Returns The terminal width or height.
72 73 74 |
# File 'lib/vedeu/geometry/dimension.rb', line 72 def default @default end |
#dn ⇒ Fixnum|NilClass (readonly, protected)
Returns The ending value (yn or xn).
64 65 66 |
# File 'lib/vedeu/geometry/dimension.rb', line 64 def dn @dn end |
#maximised ⇒ Boolean (readonly, protected) Also known as: maximised?
76 77 78 |
# File 'lib/vedeu/geometry/dimension.rb', line 76 def maximised @maximised end |
Class Method Details
.pair(attributes = {}) ⇒ Array<Fixnum>
11 12 13 |
# File 'lib/vedeu/geometry/dimension.rb', line 11 def self.pair(attributes = {}) new(attributes).pair end |
Instance Method Details
#_d ⇒ Fixnum (private)
Fetch the starting coordinate, or use 1 when not set.
207 208 209 |
# File 'lib/vedeu/geometry/dimension.rb', line 207 def _d d || 1 end |
#_dn ⇒ Fixnum (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.
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.
250 251 252 |
# File 'lib/vedeu/geometry/dimension.rb', line 250 def centre_aligned? alignment == :centre end |
#centred_d ⇒ Fixnum (private)
Ascertains the centred starting coordinate.
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_dn ⇒ Fixnum (private)
Ascertains the centred ending coordinate.
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 |
#d1 ⇒ Fixnum
Fetch the starting coordinate.
38 39 40 |
# File 'lib/vedeu/geometry/dimension.rb', line 38 def d1 dimension[0] < 1 ? 1 : dimension[0] end |
#d2 ⇒ Fixnum
Fetch the ending coordinate.
45 46 47 |
# File 'lib/vedeu/geometry/dimension.rb', line 45 def d2 dimension[-1] end |
#defaults ⇒ Hash<Symbol => NilClass,Boolean> (private)
Returns the default options/attributes for this class.
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 |
#dimension ⇒ Array<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,
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.
243 244 245 |
# File 'lib/vedeu/geometry/dimension.rb', line 243 def left_aligned? alignment == :left end |
#left_dn ⇒ Fixnum (private)
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 |
#length ⇒ Fixnum|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.
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)
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.
126 127 128 |
# File 'lib/vedeu/geometry/dimension.rb', line 126 def length? default && length end |
#pair ⇒ Array<Fixnum>
Fetch the coordinates.
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.
257 258 259 |
# File 'lib/vedeu/geometry/dimension.rb', line 257 def right_aligned? alignment == :right end |
#right_d ⇒ Fixnum (private)
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 |