Class: Vedeu::Geometry::Dimension
- Inherits:
-
Object
- Object
- Vedeu::Geometry::Dimension
- Extended by:
- Forwardable
- Defined in:
- lib/vedeu/geometry/dimension.rb
Overview
A Dimension is either the height or width of an entity.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#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.
- #alignment ⇒ Object private
-
#centred_d ⇒ Fixnum
private
Ascertains the centred starting coordinate.
-
#centred_dn ⇒ Fixnum
private
Ascertains the centred ending coordinate.
-
#defaults ⇒ Hash<Symbol => NilClass,Boolean,Symbol>
private
Returns the default options/attributes for this class.
-
#dimension ⇒ Array<Fixnum>
private
Return the dimension.
-
#end_coordinate ⇒ Fixnum
private
Ascertains the ending coordinate for a left or top aligned interface/view.
-
#initialize(attributes = {}) ⇒ Vedeu::Geometry::Dimension
constructor
Returns a new instance of Vedeu::Geometry::Dimension.
-
#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.
-
#start_coordinate ⇒ Fixnum
private
Ascertains the starting coordinate for a right or bottom aligned interface/view.
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Geometry::Dimension
Returns a new instance of Vedeu::Geometry::Dimension.
39 40 41 42 43 |
# File 'lib/vedeu/geometry/dimension.rb', line 39 def initialize(attributes = {}) defaults.merge!(attributes).each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#d ⇒ Fixnum|NilClass (readonly, protected)
Returns The starting value (y or x).
56 57 58 |
# File 'lib/vedeu/geometry/dimension.rb', line 56 def d @d end |
#d_dn ⇒ Fixnum|NilClass (readonly, protected)
Returns A width or a height.
64 65 66 |
# File 'lib/vedeu/geometry/dimension.rb', line 64 def d_dn @d_dn end |
#default ⇒ Fixnum|NilClass (readonly, protected)
Returns The terminal width or height.
68 69 70 |
# File 'lib/vedeu/geometry/dimension.rb', line 68 def default @default end |
#dn ⇒ Fixnum|NilClass (readonly, protected)
Returns The ending value (yn or xn).
60 61 62 |
# File 'lib/vedeu/geometry/dimension.rb', line 60 def dn @dn end |
#maximised ⇒ Boolean (readonly, protected) Also known as: maximised?
72 73 74 |
# File 'lib/vedeu/geometry/dimension.rb', line 72 def maximised @maximised end |
Class Method Details
.pair(attributes = {}) ⇒ Array<Fixnum>
22 23 24 |
# File 'lib/vedeu/geometry/dimension.rb', line 22 def self.pair(attributes = {}) new(attributes).pair end |
Instance Method Details
#_d ⇒ Fixnum (private)
Fetch the starting coordinate, or use 1 when not set.
211 212 213 |
# File 'lib/vedeu/geometry/dimension.rb', line 211 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.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/vedeu/geometry/dimension.rb', line 225 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 |
#alignment ⇒ Object (private)
79 80 81 |
# File 'lib/vedeu/geometry/dimension.rb', line 79 def alignment fail Vedeu::Error::NotImplemented, 'Subclasses implement this.'.freeze end |
#centred_d ⇒ Fixnum (private)
Ascertains the centred starting coordinate.
148 149 150 151 |
# File 'lib/vedeu/geometry/dimension.rb', line 148 def centred_d d = (default / 2) - (length / 2) d < 1 ? 1 : d end |
#centred_dn ⇒ Fixnum (private)
Ascertains the centred ending coordinate.
161 162 163 164 |
# File 'lib/vedeu/geometry/dimension.rb', line 161 def centred_dn dn = (default / 2) + (length / 2) dn > default ? default : dn end |
#defaults ⇒ Hash<Symbol => NilClass,Boolean,Symbol> (private)
Returns the default options/attributes for this class.
247 248 249 250 251 252 253 254 |
# File 'lib/vedeu/geometry/dimension.rb', line 247 def defaults { d: nil, dn: nil, d_dn: nil, maximised: false, } 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,
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/vedeu/geometry/dimension.rb', line 91 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_coordinate ⇒ Fixnum (private)
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.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/vedeu/geometry/dimension.rb', line 174 def end_coordinate 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.
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vedeu/geometry/dimension.rb', line 127 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.
116 117 118 |
# File 'lib/vedeu/geometry/dimension.rb', line 116 def length? default && length end |
#pair ⇒ Array<Fixnum>
Fetch the coordinates.
48 49 50 |
# File 'lib/vedeu/geometry/dimension.rb', line 48 def pair dimension end |
#start_coordinate ⇒ Fixnum (private)
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.
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/vedeu/geometry/dimension.rb', line 195 def start_coordinate if d_dn (default - d_dn) < 1 ? 1 : (default - d_dn) elsif d d else 1 end end |