Skip to content

Commit f878536

Browse files
committed
Add CapabilityConfig resource methods
Convinient methods to get the CapabilityConfig instance of a given resource
1 parent 9eaac54 commit f878536

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lib/resource_registry/capabilities/capability_config.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ module ClassMethods
2222
def key
2323
end
2424

25-
sig { params(resource: Resource).returns(T.nilable(self))}
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.self_type))}
2631
def resource_capability(resource:)
27-
return unless resource.capabilities.key?(key)
32+
return unless resource_capability?(resource:)
2833

2934
T.cast(resource.capabilities[key], self)
3035
end
36+
37+
sig { params(resource: Resource).returns(T.self_type)}
38+
def resource_capability!(resource:)
39+
T.must(resource_capability(resource: ))
40+
end
3141
end
3242

3343
requires_ancestor { Object }
Lines changed: 55 additions & 0 deletions
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)