Skip to content

Commit

Permalink
Add group function #to_s and #inspect (#127)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored Sep 7, 2023
1 parent 5b87c3c commit 6ca1cf7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
37 changes: 37 additions & 0 deletions lib/openhab/core/items/group_function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module OpenHAB
module Core
module Items
java_import org.openhab.core.items.GroupFunction

#
# Adds `#to_s` and `#inspect` to group function.
#
# @example
# # Group:SWITCH:OR(ON,OFF) Switches
# logger.info "The Switches group function is: #{Switches.function}" # => "OR"
# logger.info "The Switches group function: #{Switches.function.inspect}" # => "OR(ON,OFF)"
#
module GroupFunction
#
# Returns the group function as an uppercase string
# @return [String]
#
def to_s
self.class.simple_name.upcase
end

#
# Returns the group function and its parameters as a string
# @return [String]
#
def inspect
params = parameters.map(&:inspect).join(",")
params = "(#{params})" unless params.empty?
"#{self}#{params}"
end
end
end
end
end
5 changes: 1 addition & 4 deletions lib/openhab/core/items/group_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ def format_type(command)
def type_details
r = ""
r += ":#{base_item.type}#{base_item.__send__(:type_details)}" if base_item
if function && (fn = function.class.java_class.simple_name.upcase) != "EQUALITY"
r += ":#{fn}"
r += "(#{function.parameters.map(&:inspect).join(",")})" unless function.parameters.empty?
end
r += ":#{function.inspect}" if function && function.to_s != "EQUALITY"
r
end

Expand Down
19 changes: 18 additions & 1 deletion spec/openhab/core/items/group_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
before do
items.build do
group_item "Sensors" do
group_item "Temperatures"
group_item "Temperatures", type: :number, function: "AVG"
end

group_item "House" do
Expand Down Expand Up @@ -64,6 +64,23 @@
end
end

describe "#function" do
describe "#to_s" do
it "returns the function name in uppercase" do
expect(Temperatures.function.to_s).to eql "AVG"
end
end

describe "#inspect" do
it "includes the parameters" do
items.build do
group_item "Switches", type: :switch, function: "OR(ON,OFF)"
end
expect(Switches.function.inspect).to eql "OR(ON,OFF)"
end
end
end

describe "#command" do
it "propagates to all items" do
GroundFloor.command(60)
Expand Down

0 comments on commit 6ca1cf7

Please sign in to comment.