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 d74e579 commit b1cac79
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/resource_registry/capabilities/capability_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ module ClassMethods
sig { abstract.returns(Symbol) }
def key
end

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

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

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

sig { params(resource: Resource).returns(T.attached_class)}
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 b1cac79

Please sign in to comment.