From f8785368562e4c83c4074ff3e41eb74fdf80519f Mon Sep 17 00:00:00 2001 From: Antonio Tapiador del Dujo Date: Tue, 24 Dec 2024 14:01:00 +0100 Subject: [PATCH] Add CapabilityConfig resource methods Convinient methods to get the CapabilityConfig instance of a given resource --- .../capabilities/capability_config.rb | 14 ++++- .../capabilities/capability_config_spec.rb | 55 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 spec/resource_registry/capabilities/capability_config_spec.rb diff --git a/lib/resource_registry/capabilities/capability_config.rb b/lib/resource_registry/capabilities/capability_config.rb index 1815610..6433412 100644 --- a/lib/resource_registry/capabilities/capability_config.rb +++ b/lib/resource_registry/capabilities/capability_config.rb @@ -22,12 +22,22 @@ module ClassMethods def key end - sig { params(resource: Resource).returns(T.nilable(self))} + sig { params(resource: Resource).returns(T::Boolean) } + def resource_capability?(resource:) + resource.capabilities.key?(key) + end + + sig { params(resource: Resource).returns(T.nilable(T.self_type))} def resource_capability(resource:) - return unless resource.capabilities.key?(key) + return unless resource_capability?(resource:) T.cast(resource.capabilities[key], self) end + + sig { params(resource: Resource).returns(T.self_type)} + def resource_capability!(resource:) + T.must(resource_capability(resource: )) + end end requires_ancestor { Object } diff --git a/spec/resource_registry/capabilities/capability_config_spec.rb b/spec/resource_registry/capabilities/capability_config_spec.rb new file mode 100644 index 0000000..2cc8bfa --- /dev/null +++ b/spec/resource_registry/capabilities/capability_config_spec.rb @@ -0,0 +1,55 @@ +# typed: false + +require "spec_helper" +require_relative "../../../lib/resource_registry/capabilities/capability_config" + +class DummyCapability < T::Struct + include ResourceRegistry::Capabilities::CapabilityConfig + + def self.key + :dummy_capability + end +end + +RSpec.describe ResourceRegistry::Capabilities::CapabilityConfig do + let(:schema) do + SchemaRegistry::Schema.new( + name: "dummy", + namespace: "dummies", + properties: [ + SchemaRegistry::Property.new( + name: "foo", + types: [SchemaRegistry::PropertyType::String], + required: true + ) + ] + ) + end + let(:capabilities) { { dummy_capability: DummyCapability.new } } + let(:resource) do + ResourceRegistry::Resource.new( + repository_raw: DummyRepo.to_s, + capabilities:, + verbs: { + }, + schema: + ) + end + + it "should return resource's capability" do + expect(DummyCapability.resource_capability?(resource:)).to be true + expect(DummyCapability.resource_capability(resource:)).to be_a(DummyCapability) + expect(DummyCapability.resource_capability!(resource:)).to be_a(DummyCapability) + end + + context 'without the capability' do + let(:capabilities) { {} } + + it "should return resource's capability" do + expect(DummyCapability.resource_capability?(resource:)).to be false + expect(DummyCapability.resource_capability(resource:)).to be_nil + end + end +end + +