Class: OpenHAB::Core::Types::QuantityType
- Inherits:
-
Object
- Object
- OpenHAB::Core::Types::QuantityType
- Includes:
- NumericType
- Defined in:
- lib/openhab/core/types/quantity_type.rb
Overview
QuantityType extends DecimalType to handle physical unit measurement.
QuantityType is part of the Units of Measurement framework in openHAB. It is represented as a decimal number with a unit. You can construct a QuantityType object by using the pipe operator with any Numeric.
Instance Method Summary collapse
-
#-@ ⇒ Object
arithmetic operators.
-
#<=>(other) ⇒ Integer?
Comparison.
-
#coerce(other) ⇒ Array<(QuantityType, QuantityType)>?
Type Coercion.
-
#eql?(other) ⇒ true, false
Check equality without unit inversion.
-
#to_invertible_unit(unit) ⇒ QuantityType
(also: #|)
Convert this QuantityType into another unit.
-
#to_temporal_amount ⇒ Duration
Convert this QuantityType into a Duration if the unit is time-based.
Methods included from NumericType
Instance Method Details
#-@ ⇒ Object
arithmetic operators
184 |
# File 'lib/openhab/core/types/quantity_type.rb', line 184 alias_method :-@, :negate |
#<=>(other) ⇒ Integer?
Comparison
Comparisons against Numeric and DecimalType are allowed only within a unit block to avoid unit ambiguities. Comparisons against other types may be done if supported by that type's coercion.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/openhab/core/types/quantity_type.rb', line 147 def <=>(other) logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" } case other when self.class return unitize(other.unit).compare_to(other) if unit == Units::ONE return compare_to(other.unitize(unit)) if other.unit == Units::ONE return compare_to(other) when Numeric, DecimalType if (unit = OpenHAB::DSL.unit(dimension)) return compare_to(QuantityType.new(other, unit)) end return nil # don't allow comparison with numeric outside a unit block end return nil unless other.respond_to?(:coerce) other.coerce(self)&.then { |lhs, rhs| lhs <=> rhs } end |
#coerce(other) ⇒ Array<(QuantityType, QuantityType)>?
Type Coercion
Coerce object to a OpenHAB::Core::Types::QuantityType
176 177 178 179 180 181 |
# File 'lib/openhab/core/types/quantity_type.rb', line 176 def coerce(other) logger.trace { "Coercing #{self} as a request from #{other.class}" } return unless other.respond_to?(:to_d) [QuantityType.new(other.to_d.to_java, Units::ONE), self] end |
#eql?(other) ⇒ true, false
Check equality without unit inversion
124 125 126 127 128 129 130 |
# File 'lib/openhab/core/types/quantity_type.rb', line 124 def eql?(other) return false unless other.instance_of?(self.class) # compare_to in OH5 will throw an IAE if the units are not compatible return false unless unit.compatible?(other.unit) super end |
#to_invertible_unit(unit) ⇒ QuantityType Also known as: |
Convert this OpenHAB::Core::Types::QuantityType into another unit.
10 |
# File 'lib/openhab/core/types/quantity_type.rb', line 10 def to_invertible_unit(unit); end |
#to_temporal_amount ⇒ Duration
Convert this OpenHAB::Core::Types::QuantityType into a Duration if the unit is time-based.
304 305 306 307 308 |
# File 'lib/openhab/core/types/quantity_type.rb', line 304 def to_temporal_amount return Duration.of_nanos(to_unit("ns").to_i) if unit.compatible?(Units::SECOND) raise TypeError, "#{self} is not a time-based Quantity" end |