-
-
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.
- Loading branch information
Showing
7 changed files
with
155 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "generic_item" | ||
|
||
module OpenHAB | ||
module Core | ||
module Items | ||
java_import org.openhab.core.library.items.CallItem | ||
|
||
# | ||
# A {CallItem} identifies a telephone call by its origin and destination. | ||
# | ||
# @!attribute [r] state | ||
# @return [StringListType, nil] | ||
# | ||
class CallItem < GenericItem | ||
# @!visibility private | ||
def format_type(command) | ||
return command if command.is_a?(Types::StringListType) | ||
return Types::StringListType.new(command.to_ary.map(&:to_s)) if command.respond_to?(:to_ary) | ||
|
||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# @!parse CallItem = OpenHAB::Core::Items::CallItem |
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "forwardable" | ||
|
||
require_relative "type" | ||
|
||
module OpenHAB | ||
module Core | ||
module Types | ||
StringListType = org.openhab.core.library.types.StringListType | ||
|
||
# {StringListType} can be used for items that are dealing with telephony functionality. | ||
# | ||
# The entries can be accessed like an array. | ||
# | ||
# @example | ||
# string_list = StringListType.new("a", "b", "c") | ||
# logger.info "first entry: #{string_list.first}" # => "a" | ||
# logger.info "second entry: #{string_list[1]}" # => "b" | ||
# logger.info "last entry: #{string_list.last}" # => "c" | ||
# logger.info "length: #{string_list.size}" # => 3 | ||
# | ||
class StringListType | ||
extend Forwardable | ||
|
||
field_reader :typeDetails | ||
|
||
# @!parse include Command, State | ||
|
||
# @!visibility private | ||
def inspect | ||
"#<OpenHAB::Core::Types::StringListType #{to_a.inspect}>" | ||
end | ||
|
||
# @return [Array<String>] the values as an array | ||
def to_a | ||
typeDetails.to_a | ||
end | ||
|
||
# @!visibility private | ||
def ==(other) | ||
return super if other.is_a?(StringListType) | ||
return to_a == other.to_a if other.respond_to?(:to_a) | ||
|
||
super | ||
end | ||
|
||
# any method that exists on Array gets forwarded to states | ||
delegate (Array.instance_methods - instance_methods) => :to_a | ||
end | ||
end | ||
end | ||
end | ||
|
||
# @!parse StringListType = OpenHAB::Core::Types::StringListType |
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe OpenHAB::Core::Items::CallItem do | ||
subject(:item) { CallOne } | ||
|
||
before do | ||
items.build do | ||
group_item "Calls" do | ||
call_item "CallOne" | ||
call_item "CallTwo" | ||
end | ||
end | ||
end | ||
|
||
it "is a call item" do | ||
expect(item).to be_a_call_item | ||
end | ||
|
||
it "is not a group" do | ||
expect(item).not_to be_a_group_item | ||
end | ||
|
||
it "works with grep" do | ||
items.build { switch_item "SwitchOne" } | ||
expect(items.grep(CallItem)).to match_array [CallOne, CallTwo] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe OpenHAB::Core::Types::StringListType do | ||
let(:state) { StringListType.new(%w[a b]) } | ||
|
||
it "is inspectable" do | ||
expect(state.inspect).to eql '#<OpenHAB::Core::Types::StringListType ["a", "b"]>' | ||
end | ||
|
||
it "converts to an array" do | ||
expect(state.to_a).to eql %w[a b] | ||
end | ||
|
||
it "supports array operations" do | ||
expect(state.first).to eql "a" | ||
expect(state.last).to eql "b" | ||
expect(state.size).to be 2 | ||
end | ||
|
||
describe "comparisons" do | ||
let(:state2) { StringListType.new(%w[a b]) } | ||
|
||
specify { expect(state == %w[a b]).to be true } | ||
specify { expect(state == %w[a c]).to be false } | ||
specify { expect(state != %w[a b]).to be false } | ||
specify { expect(state != %w[a c]).to be true } | ||
specify { expect(state == state2).to be true } | ||
specify { expect(state != state2).to be false } | ||
end | ||
|
||
describe "#eql?" do | ||
it "works" do | ||
expect(state).to eql StringListType.new(%w[a b]) | ||
end | ||
|
||
it "returns false when compared against a plain array" do | ||
expect(state).not_to eql %w[a b] | ||
end | ||
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