Class: OpenHAB::CoreExt::Java::Duration
- Inherits:
-
Object
- Object
- OpenHAB::CoreExt::Java::Duration
- Includes:
- Between, TemporalAmount
- Defined in:
- lib/openhab/core_ext/java/duration.rb
Overview
Extensions to Java Duration
Ruby's Integer and Float classes are extended to allow convenient creation of Duration instances.
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric, ...
Comparisons against other types may be done if supported by that type's coercion.
-
#coerce(other) ⇒ Array?
Converts
other
to Duration, if possible. -
#positive? ⇒ true, false
Returns true if the duration is greater than zero.
-
#to_f ⇒ Float
Convert to number of seconds.
-
#to_i ⇒ Integer
Convert to integer number of seconds.
-
#zero? ⇒ true, false
Returns true if the duration is zero length.
Methods included from TemporalAmount
#-@, #ago, #from_now, #inspect, #to_temporal_amount
Methods included from Between
Instance Method Details
#<=>(other) ⇒ Numeric, ...
Comparisons against other types may be done if supported by that type's coercion.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openhab/core_ext/java/duration.rb', line 63 def <=>(other) logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" } case other when Duration then super when Numeric then to_f <=> other when QuantityType then self <=> other.to_temporal_amount else if other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self)) lhs <=> rhs else super end end rescue TypeError nil end |
#coerce(other) ⇒ Array?
Converts other
to OpenHAB::CoreExt::Java::Duration, if possible.
86 87 88 89 90 91 92 |
# File 'lib/openhab/core_ext/java/duration.rb', line 86 def coerce(other) return [other.seconds, self] if other.is_a?(Numeric) # We want to return the same type as other, e.g. QuantityType + Duration = QuantityType return [other, to_nanos | "ns"] if other.is_a?(QuantityType) && other.unit.compatible?(Units::SECOND) [other.to_i.seconds, self] if other.is_a?(Period) end |
#positive? ⇒ true, false
Returns true if the duration is greater than zero.
42 43 44 |
# File 'lib/openhab/core_ext/java/duration.rb', line 42 def positive? self > 0 # rubocop:disable Style/NumericPredicate end |
#to_f ⇒ Float
Convert to number of seconds
52 53 54 |
# File 'lib/openhab/core_ext/java/duration.rb', line 52 def to_f to_i + (nano / 1_000_000_000.0) end |
#to_i ⇒ Integer
Convert to integer number of seconds
26 |
# File 'lib/openhab/core_ext/java/duration.rb', line 26 alias_method :to_i, :seconds |
#zero? ⇒ true, false
Returns true if the duration is zero length.
|
# File 'lib/openhab/core_ext/java/duration.rb', line 28
|