Skip to content

Commit b1cac79

Browse files
committed
Add CapabilityConfig resource methods
Convinient methods to get the CapabilityConfig instance of a given resource
1 parent d74e579 commit b1cac79

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/resource_registry/capabilities/capability_config.rb

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ module ClassMethods
2121
sig { abstract.returns(Symbol) }
2222
def key
2323
end
24+
25+
sig { params(resource: Resource).returns(T::Boolean) }
26+
def resource_capability?(resource:)
27+
resource.capabilities.key?(key)
28+
end
29+
30+
sig { params(resource: Resource).returns(T.nilable(T.attached_class))}
31+
def resource_capability(resource:)
32+
return unless resource_capability?(resource:)
33+
34+
T.cast(resource.capabilities[key], self)
35+
end
36+
37+
sig { params(resource: Resource).returns(T.attached_class)}
38+
def resource_capability!(resource:)
39+
T.must(resource_capability(resource: ))
40+
end
2441
end
2542

2643
requires_ancestor { Object }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# typed: false
2+
3+
require "spec_helper"
4+
require_relative "../../../lib/resource_registry/capabilities/capability_config"
5+
6+
class DummyCapability < T::Struct
7+
include ResourceRegistry::Capabilities::CapabilityConfig
8+
9+
def self.key
10+
:dummy_capability
11+
end
12+
end
13+
14+
RSpec.describe ResourceRegistry::Capabilities::CapabilityConfig do
15+
let(:schema) do
16+
SchemaRegistry::Schema.new(
17+
name: "dummy",
18+
namespace: "dummies",
19+
properties: [
20+
SchemaRegistry::Property.new(
21+
name: "foo",
22+
types: [SchemaRegistry::PropertyType::String],
23+
required: true
24+
)
25+
]
26+
)
27+
end
28+
let(:capabilities) { { dummy_capability: DummyCapability.new } }
29+
let(:resource) do
30+
ResourceRegistry::Resource.new(
31+
repository_raw: DummyRepo.to_s,
32+
capabilities:,
33+
verbs: {
34+
},
35+
schema:
36+
)
37+
end
38+
39+
it "should return resource's capability" do
40+
expect(DummyCapability.resource_capability?(resource:)).to be true
41+
expect(DummyCapability.resource_capability(resource:)).to be_a(DummyCapability)
42+
expect(DummyCapability.resource_capability!(resource:)).to be_a(DummyCapability)
43+
end
44+
45+
context 'without the capability' do
46+
let(:capabilities) { {} }
47+
48+
it "should return resource's capability" do
49+
expect(DummyCapability.resource_capability?(resource:)).to be false
50+
expect(DummyCapability.resource_capability(resource:)).to be_nil
51+
end
52+
end
53+
end
54+
55+

0 commit comments

Comments
 (0)