File tree 2 files changed +72
-0
lines changed
lib/resource_registry/capabilities
spec/resource_registry/capabilities
2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,23 @@ module ClassMethods
21
21
sig { abstract . returns ( Symbol ) }
22
22
def key
23
23
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 . self_type ) ) }
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 . self_type ) }
38
+ def resource_capability! ( resource :)
39
+ T . must ( resource_capability ( resource : ) )
40
+ end
24
41
end
25
42
26
43
requires_ancestor { Object }
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments