Class: AUOM::Unit
- Inherits:
-
Object
- Object
- AUOM::Unit
- Includes:
- Algebra, Equalization, Inspection, Relational
- Defined in:
- lib/auom/unit.rb
Overview
A scalar with units
Constant Summary collapse
- UNITS =
These constants can easily be changed by an application specific subclass that overrides AUOM::Unit.units with an own hash!
{ item: [1, :item], liter: [1, :liter], pack: [1, :pack], can: [1, :can], kilogramm: [1, :kilogramm], euro: [1, :euro], meter: [1, :meter], kilometer: [1000, :meter] }.freeze
Constants included from Inspection
Inspection::INSPECT_FORMAT, Inspection::SCALAR_FORMAT, Inspection::UNIT_FORMAT
Instance Attribute Summary collapse
-
#denominators ⇒ Rational
readonly
Return denominators.
-
#numerators ⇒ Rational
readonly
Return numerators.
-
#scalar ⇒ Rational
readonly
Return scalar.
-
#unit ⇒ Array
readonly
Return unit descriptor.
Class Method Summary collapse
-
.convert(operand) ⇒ Unit
private
Return converted operand or raise error.
-
.new(scalar, numerators = nil, denominators = nil) ⇒ Unit
Instantiate a new unit.
-
.try_convert(operand) ⇒ Unit?
private
Return converted operand or nil.
-
.units ⇒ Hash
private
Return built-in units symbols.
Instance Method Summary collapse
-
#assert_same_unit(other) ⇒ self
private
Assert units are the same.
-
#same_unit?(other) ⇒ true, false
Test if units are the same.
-
#unitless? ⇒ true, false
Check for unitless unit.
Methods included from Equalization
Methods included from Relational
Methods included from Inspection
Methods included from Algebra
#add, #divide, #multiply, #subtract
Instance Attribute Details
#denominators ⇒ Rational (readonly)
Return denominators
52 53 54 |
# File 'lib/auom/unit.rb', line 52 def denominators @denominators end |
#numerators ⇒ Rational (readonly)
Return numerators
38 39 40 |
# File 'lib/auom/unit.rb', line 38 def numerators @numerators end |
#scalar ⇒ Rational (readonly)
Return scalar
24 25 26 |
# File 'lib/auom/unit.rb', line 24 def scalar @scalar end |
#unit ⇒ Array (readonly)
Return unit descriptor
65 66 67 |
# File 'lib/auom/unit.rb', line 65 def unit @unit end |
Class Method Details
.convert(operand) ⇒ Unit
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 converted operand or raise error
206 207 208 209 210 211 212 213 |
# File 'lib/auom/unit.rb', line 206 def self.convert(operand) converted = try_convert(operand) unless converted fail ArgumentError, "Cannot convert #{operand.inspect} to #{self}" end converted end |
.new(scalar, numerators = nil, denominators = nil) ⇒ Unit
Instantiate a new unit
TODO: Move defaults coercions etc to .build method
172 173 174 175 176 177 178 179 |
# File 'lib/auom/unit.rb', line 172 def self.new(scalar, numerators = nil, denominators = nil) scalar = rational(scalar) scalar, numerators = resolve([*numerators], scalar, :*) scalar, denominators = resolve([*denominators], scalar, :/) super(scalar, *[numerators, denominators].map(&:sort)).freeze end |
.try_convert(operand) ⇒ Unit?
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 converted operand or nil
227 228 229 230 231 232 233 234 |
# File 'lib/auom/unit.rb', line 227 def self.try_convert(operand) case operand when self operand when Integer, Rational new(operand) end end |
.units ⇒ Hash
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 built-in units symbols
87 88 89 |
# File 'lib/auom/unit.rb', line 87 def self.units UNITS end |
Instance Method Details
#assert_same_unit(other) ⇒ self
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.
Assert units are the same
189 190 191 192 193 |
# File 'lib/auom/unit.rb', line 189 def assert_same_unit(other) fail ArgumentError, 'Incompatible units' unless same_unit?(other) self end |
#same_unit?(other) ⇒ true, false
Test if units are the same
131 132 133 |
# File 'lib/auom/unit.rb', line 131 def same_unit?(other) other.unit.eql?(unit) end |
#unitless? ⇒ true, false
Check for unitless unit
106 107 108 |
# File 'lib/auom/unit.rb', line 106 def unitless? numerators.empty? && denominators.empty? end |