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.
-
#to_invertible_unit(unit) ⇒ QuantityType
(also: #|)
Convert this QuantityType into another unit.
Methods included from NumericType
#+@, #eql?, #to_d, #to_f, #to_i
Instance Method Details
#-@ ⇒ Object
arithmetic operators
171 |
# File 'lib/openhab/core/types/quantity_type.rb', line 171 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.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/openhab/core/types/quantity_type.rb', line 134 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
163 164 165 166 167 168 |
# File 'lib/openhab/core/types/quantity_type.rb', line 163 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 |
#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 |