Class: Unitwise::Scale
- Inherits:
-
Object
- Object
- Unitwise::Scale
- Includes:
- Compatible
- Defined in:
- lib/unitwise/scale.rb
Overview
A Unitwise::Scale represents a value and a unit, sort of like a vector, it has two components. In this case, it’s a value and unit rather than a magnitude and direction. This class should be considered mostly privateish.
Direct Known Subclasses
Instance Method Summary collapse
-
#atoms ⇒ Array
List the atoms associated with this scale’s unit.
-
#depth ⇒ Integer
How far away is this instances unit from the deepest level atom.
-
#eql?(other) ⇒ Boolean
Redefine hash equality to match the hashes.
- #expression ⇒ Object
-
#hash ⇒ Object
Redefine hash for apropriate hash/key lookup.
-
#initialize(value, unit) ⇒ Scale
constructor
A new instance of Scale.
- #inspect ⇒ Object
-
#magnitude(scalar = scalar()) ⇒ Numeric
Get a magnitude based on a linear scale value.
-
#root_terms ⇒ Array
The base terms this scale’s unit is derived from.
-
#scalar(magnitude = value) ⇒ Numeric
Get a scalar value for this scale.
-
#simplified_value ⇒ Numeric
Attempts to coerce the value to the simplest Numeric that fully expresses it’s value.
-
#special? ⇒ true, false
Is this scale’s unit special?.
-
#terms ⇒ Array
List the terms associated with this scale’s unit.
-
#to_s(mode = nil) ⇒ Object
Convert to a simple string representing the scale.
-
#unit=(value) ⇒ Object
Set the unit vector.
- #value=(value) ⇒ Object
Methods included from Compatible
#<=>, #compatible_with?, #composition, #composition_string, #dim, included
Constructor Details
#initialize(value, unit) ⇒ Scale
Returns a new instance of Scale.
9 10 11 12 13 14 15 16 17 |
# File 'lib/unitwise/scale.rb', line 9 def initialize(value, unit) self.value = if value.is_a? self.class value.convert_to(unit).value else value end self.unit = unit freeze end |
Instance Method Details
#atoms ⇒ Array
List the atoms associated with this scale’s unit.
29 30 31 |
# File 'lib/unitwise/scale.rb', line 29 def atoms unit.atoms end |
#depth ⇒ Integer
How far away is this instances unit from the deepest level atom.
87 88 89 |
# File 'lib/unitwise/scale.rb', line 87 def depth unit.depth + 1 end |
#eql?(other) ⇒ Boolean
Redefine hash equality to match the hashes
140 141 142 |
# File 'lib/unitwise/scale.rb', line 140 def eql?(other) hash == other.hash end |
#expression ⇒ Object
112 113 114 |
# File 'lib/unitwise/scale.rb', line 112 def expression unit.expression end |
#hash ⇒ Object
Redefine hash for apropriate hash/key lookup
133 134 135 |
# File 'lib/unitwise/scale.rb', line 133 def hash [value, unit.to_s, self.class].hash end |
#inspect ⇒ Object
127 128 129 |
# File 'lib/unitwise/scale.rb', line 127 def inspect "#<#{self.class} value=#{simplified_value} unit=#{unit}>" end |
#magnitude(scalar = scalar()) ⇒ Numeric
Get a magnitude based on a linear scale value. Only used by scales with special atoms in it’s hierarchy.
68 69 70 71 72 73 74 |
# File 'lib/unitwise/scale.rb', line 68 def magnitude(scalar = scalar()) if special? unit.magnitude(scalar) else value * unit.magnitude end end |
#root_terms ⇒ Array
The base terms this scale’s unit is derived from
79 80 81 |
# File 'lib/unitwise/scale.rb', line 79 def root_terms unit.root_terms end |
#scalar(magnitude = value) ⇒ Numeric
Get a scalar value for this scale.
55 56 57 58 59 60 61 |
# File 'lib/unitwise/scale.rb', line 55 def scalar(magnitude = value) if special? unit.scalar(magnitude) else value * unit.scalar end end |
#simplified_value ⇒ Numeric
Attempts to coerce the value to the simplest Numeric that fully expresses it’s value. For instance a value of 1.0 would return 1, a value of #<BigDecimal:7f9558d559b8,‘0.45E1’,18(18)> would return 4.5.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/unitwise/scale.rb', line 97 def simplified_value if value.is_a?(Integer) value elsif (i = Integer(value)) == value i elsif value.is_a?(Float) || value.is_a?(Rational) value elsif (f = Float(value)) == value f else value end end |
#special? ⇒ true, false
Is this scale’s unit special?
47 48 49 |
# File 'lib/unitwise/scale.rb', line 47 def special? unit.special? end |
#terms ⇒ Array
List the terms associated with this scale’s unit.
40 41 42 |
# File 'lib/unitwise/scale.rb', line 40 def terms unit.terms end |
#to_s(mode = nil) ⇒ Object
Convert to a simple string representing the scale.
118 119 120 121 122 123 124 125 |
# File 'lib/unitwise/scale.rb', line 118 def to_s(mode = nil) unit_string = unit.to_s(mode) if unit_string && unit_string != '1' "#{simplified_value} #{unit_string}" else simplified_value.to_s end end |
#unit=(value) ⇒ Object
Set the unit vector.
22 23 24 |
# File 'lib/unitwise/scale.rb', line 22 def unit=(value) @unit = value.is_a?(Unit) ? value : Unit.new(value) end |
#value=(value) ⇒ Object
33 34 35 |
# File 'lib/unitwise/scale.rb', line 33 def value=(value) @value = BigDecimal(value.to_s) end |