Skip to content

Commit

Permalink
Split Metadata::Hash#provider to provider and provider! (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer authored Sep 29, 2023
1 parent 6d31db8 commit 0f0a1f7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ RSpec/NestedGroups:
Enabled: false
RSpec/NoExpectationExample:
Enabled: false
RSpec/SubjectStub:
Enabled: false

Style/CaseEquality:
Exclude: # we're explicitly testing this operator
Expand Down
2 changes: 1 addition & 1 deletion lib/openhab/core/items/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def inspect
"#{s}>"
end

# @return [org.openhab.core.common.registry.Provider]
# @return [org.openhab.core.common.registry.Provider, nil]
def provider
Provider.registry.provider_for(self)
end
Expand Down
17 changes: 11 additions & 6 deletions lib/openhab/core/items/metadata/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ def commit
return unless attached?

javaify
provider.update(@metadata)
provider!.update(@metadata)
end

# @!visibility private
def create_or_update
return unless attached?

javaify
(p = provider).get(uid) ? p.update(@metadata) : p.add(@metadata)
(p = provider!).get(uid) ? p.update(@metadata) : p.add(@metadata)
end

# @!visibility private
def remove
provider.remove(uid)
provider!.remove(uid)
end

# @!visibility private
Expand Down Expand Up @@ -220,23 +220,28 @@ def inspect
[value, to_h].inspect
end

# @return [org.openhab.core.common.registry.Provider, nil]
def provider
Provider.registry.provider_for(uid)
end

#
# @raise [FrozenError] if the provider is not a
# {org.openhab.core.common.registry.ManagedProvider ManagedProvider} that can be updated.
# @return [org.openhab.core.common.registry.ManagedProvider]
#
def provider
def provider!
preferred_provider = Provider.current(
Thread.current[:openhab_providers]&.dig(:metadata_items, uid.item_name) ||
Thread.current[:openhab_providers]&.dig(:metadata_namespaces, uid.namespace),
self
)

if attached?
provider = Provider.registry.provider_for(uid)
provider = self.provider
return preferred_provider unless provider

unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
unless provider.is_a?(ManagedProvider)
raise FrozenError, "Cannot modify metadata from provider #{provider.inspect} for #{uid}."
end

Expand Down
22 changes: 22 additions & 0 deletions spec/openhab/core/items/metadata/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,26 @@
expect(namespace.to_h).to eql({ "qux" => "quux" })
end
end

describe "#provider" do
it "returns nil for unattached hashes" do
expect(OpenHAB::Core::Items::Metadata::Hash.from_value("namespace", "value").provider).to be_nil
end
end

describe "#provider!" do
it "returns the current provider for unattached hashes" do
expect(OpenHAB::Core::Items::Metadata::Hash.from_value("namespace", "value").provider!)
.to eq OpenHAB::Core::Items::Metadata::Provider.current
end

it "raises FrozenError if the provider is a generic provider" do
generic_provider = OpenHAB::Core::Items::Metadata::Provider.registry
.providers
.grep_v(OpenHAB::Core::ManagedProvider)
.first
allow(namespace).to receive(:provider).and_return(generic_provider)
expect { namespace.provider! }.to raise_error(FrozenError)
end
end
end
1 change: 0 additions & 1 deletion spec/openhab/rspec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
automation/filea.sl20.rb
automation/sl30/fileb.rb])

# rubocop:disable RSpec/SubjectStub
expect(subject).to receive(:load).with("automation/filea.sl20.rb").ordered
expect(subject).to receive(:load).with("automation/sl30/fileb.rb").ordered
expect(subject).to receive(:load).with("automation/sl30/filec.rb").ordered
Expand Down

0 comments on commit 0f0a1f7

Please sign in to comment.