Skip to content

Commit

Permalink
Add CapabilityConfig resource methods
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 committed Dec 24, 2024
1 parent 9eaac54 commit f878536
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/resource_registry/capabilities/capability_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ module ClassMethods
def key
end

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

sig { params(resource: Resource).returns(T.nilable(T.self_type))}
def resource_capability(resource:)
return unless resource.capabilities.key?(key)
return unless resource_capability?(resource:)

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

sig { params(resource: Resource).returns(T.self_type)}
def resource_capability!(resource:)
T.must(resource_capability(resource: ))
end
end

requires_ancestor { Object }
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 f878536

Please sign in to comment.