File tree 3 files changed +78
-3
lines changed
spec/resource_registry/capabilities
3 files changed +78
-3
lines changed Original file line number Diff line number Diff line change @@ -14,13 +14,33 @@ module CapabilityConfig
14
14
# Class methods interface for capability configuration
15
15
module ClassMethods
16
16
extend T ::Sig
17
+ extend T ::Generic
17
18
extend T ::Helpers
18
19
abstract!
19
20
21
+ has_attached_class!
22
+
20
23
# The key of the capability, this key will be used to take it from yaml configuration
21
24
sig { abstract . returns ( Symbol ) }
22
25
def key
23
26
end
27
+
28
+ sig { params ( resource : Resource ) . returns ( T ::Boolean ) }
29
+ def resource_capability? ( resource :)
30
+ resource . capabilities . key? ( key )
31
+ end
32
+
33
+ sig { params ( resource : Resource ) . returns ( T . nilable ( ClassMethods [ T . attached_class ] ) ) }
34
+ def resource_capability ( resource :)
35
+ return unless resource_capability? ( resource :)
36
+
37
+ T . cast ( resource . capabilities [ key ] , self )
38
+ end
39
+
40
+ sig { params ( resource : Resource ) . returns ( ClassMethods [ T . attached_class ] ) }
41
+ def resource_capability! ( resource :)
42
+ T . must ( resource_capability ( resource : ) )
43
+ end
24
44
end
25
45
26
46
requires_ancestor { Object }
Original file line number Diff line number Diff line change @@ -140,7 +140,7 @@ def build_dto(verb, **parameters)
140
140
T ::Class [ T . type_parameter ( :CapabilityConfig ) ] ,
141
141
# Referencing `ClassMethods` here is not ideal but it seems Sorbet
142
142
# provides no other mechanism to do this
143
- Capabilities ::CapabilityConfig ::ClassMethods ,
143
+ Capabilities ::CapabilityConfig ::ClassMethods [ Capabilities :: CapabilityConfig ] ,
144
144
T ::Class [ Capabilities ::CapabilityConfig ]
145
145
)
146
146
)
@@ -161,7 +161,7 @@ def capability_by_key(key)
161
161
params (
162
162
feature :
163
163
T . all (
164
- Capabilities ::CapabilityConfig ::ClassMethods ,
164
+ Capabilities ::CapabilityConfig ::ClassMethods [ Capabilities :: CapabilityConfig ] ,
165
165
T ::Class [ Capabilities ::CapabilityConfig ]
166
166
)
167
167
) . returns ( T ::Boolean )
@@ -178,7 +178,7 @@ def capability?(feature)
178
178
T ::Class [ T . type_parameter ( :CapabilityConfig ) ] ,
179
179
# Referencing ClassMethods here is not ideal but it seems Sorbet
180
180
# provides no other mechanism to do this
181
- Capabilities ::CapabilityConfig ::ClassMethods ,
181
+ Capabilities ::CapabilityConfig ::ClassMethods [ Capabilities :: CapabilityConfig ] ,
182
182
T ::Class [ Capabilities ::CapabilityConfig ]
183
183
)
184
184
)
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