Skip to content

Commit

Permalink
Add CapabilityConfig resource methods (#31)
Browse files Browse the repository at this point in the history
Convinient methods to get the CapabilityConfig instance
of a given resource
  • Loading branch information
atd authored Dec 27, 2024
1 parent d74e579 commit ebf79f6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
26 changes: 26 additions & 0 deletions lib/resource_registry/capabilities/capability_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,39 @@ module CapabilityConfig
# Class methods interface for capability configuration
module ClassMethods
extend T::Sig
extend T::Generic
extend T::Helpers
abstract!

has_attached_class!

# The key of the capability, this key will be used to take it from yaml configuration
sig { abstract.returns(Symbol) }
def key
end

sig { params(resource: Resource).returns(T::Boolean) }
def resource_capability?(resource:)
resource.capabilities.key?(key)
end

sig do
params(resource: Resource).returns(
T.nilable(T.attached_class)
)
end
def resource_capability(resource:)
return unless resource_capability?(resource:)

T.cast(resource.capabilities[key], T.attached_class)
end

sig do
params(resource: Resource).returns(T.attached_class)
end
def resource_capability!(resource:)
T.must(resource_capability(resource: ))
end
end

requires_ancestor { Object }
Expand Down
6 changes: 3 additions & 3 deletions lib/resource_registry/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def build_dto(verb, **parameters)
T::Class[T.type_parameter(:CapabilityConfig)],
# Referencing `ClassMethods` here is not ideal but it seems Sorbet
# provides no other mechanism to do this
Capabilities::CapabilityConfig::ClassMethods,
Capabilities::CapabilityConfig::ClassMethods[T.untyped],
T::Class[Capabilities::CapabilityConfig]
)
)
Expand All @@ -161,7 +161,7 @@ def capability_by_key(key)
params(
feature:
T.all(
Capabilities::CapabilityConfig::ClassMethods,
Capabilities::CapabilityConfig::ClassMethods[T.untyped],
T::Class[Capabilities::CapabilityConfig]
)
).returns(T::Boolean)
Expand All @@ -178,7 +178,7 @@ def capability?(feature)
T::Class[T.type_parameter(:CapabilityConfig)],
# Referencing ClassMethods here is not ideal but it seems Sorbet
# provides no other mechanism to do this
Capabilities::CapabilityConfig::ClassMethods,
Capabilities::CapabilityConfig::ClassMethods[T.untyped],
T::Class[Capabilities::CapabilityConfig]
)
)
Expand Down
55 changes: 55 additions & 0 deletions spec/resource_registry/capabilities/capability_config_spec.rb
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit ebf79f6

Please sign in to comment.