-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support comparisons between various numeric item/state types
Former-commit-id: 510d6db
- Loading branch information
Showing
10 changed files
with
517 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
AllCops: | ||
TargetRubyVersion: 2.5 | ||
NewCops: enable | ||
SuggestExtensions: false |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'openhab/core/dsl/types/quantity' | ||
|
||
module OpenHAB | ||
module Core | ||
module DSL | ||
module MonkeyPatch | ||
module Ruby | ||
# | ||
# Extend String class | ||
# | ||
module StringExtensions | ||
include OpenHAB::Core | ||
|
||
# | ||
# Compares String to another object | ||
# | ||
# @param [Object] other object to compare to | ||
# | ||
# @return [Boolean] true if the two objects contain the same value, false otherwise | ||
# | ||
def ==(other) | ||
case other | ||
when OpenHAB::Core::DSL::Types::Quantity, QuantityType | ||
other == self | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# | ||
# Prepend String class with comparison extensions | ||
# | ||
class String | ||
prepend OpenHAB::Core::DSL::MonkeyPatch::Ruby::StringExtensions | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'java' | ||
|
||
# | ||
# MonkeyPatching QuantityType | ||
# | ||
# rubocop:disable Style/ClassAndModuleChildren | ||
class Java::OrgOpenhabCoreLibraryTypes::QuantityType | ||
# rubocop:enable Style/ClassAndModuleChildren | ||
|
||
# | ||
# Compare QuantityType to supplied object | ||
# | ||
# @param [Object] other object to compare to | ||
# | ||
# @return [Integer] -1,0,1 or nil depending on value supplied, nil comparison to supplied object is not possible. | ||
# | ||
def <=>(other) | ||
logger.trace("#{self.class} #{self} <=> #{other} (#{other.class})") | ||
case other | ||
when Java::OrgOpenhabCoreTypes::UnDefType then 1 | ||
when String then self <=> Quantity.new(other) | ||
when OpenHAB::Core::DSL::Types::Quantity then self <=> other.quantity | ||
else | ||
other = other.state if other.respond_to? :state | ||
compare_to(other) | ||
end | ||
end | ||
|
||
# | ||
# Coerce objects into a QuantityType | ||
# | ||
# @param [Object] other object to coerce to a QuantityType if possible | ||
# | ||
# @return [Object] Numeric when applicable | ||
# | ||
def coerce(other) | ||
logger.trace("Coercing #{self} as a request from #{other.class}") | ||
case other | ||
when String | ||
[Quantity.new(other), self] | ||
else | ||
[other, self] | ||
end | ||
end | ||
|
||
# | ||
# Compare self to other using the spaceship operator | ||
# | ||
# @param [Object] other object to compare to | ||
# | ||
# @return [Boolean] True if equals | ||
# | ||
def ==(other) | ||
(self <=> other).zero? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters