diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-14 10:47:55 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-14 10:47:55 +0200 |
| commit | 8a641ff347d0584ea1ddc16a6ef81a8c16ab172d (patch) | |
| tree | 095dc715e4df2a970188e3070e7d22464401e1d3 /lib/dslkeywords/resource.rb | |
| parent | 818ed50e2a54b40ccf7a7771bebe0312dc01a8b5 (diff) | |
Use active DSL registry for Resource.find
Diffstat (limited to 'lib/dslkeywords/resource.rb')
| -rw-r--r-- | lib/dslkeywords/resource.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index c1c2554..16b34af 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -1,7 +1,8 @@ -require 'set' +# frozen_string_literal: true require_relative 'keyword' +# rubocop:disable Style/ClassVars module RCM # Concern that wraps side-effecting blocks so they are skipped (and # logged as dry-run) when the --dry option is active. Kept separate @@ -90,7 +91,6 @@ module RCM include ResourceDependencies class NoSuchResourceObject < StandardError; end - @@resource_find_cache = {} # Class-level registry: every subclass is registered here when it is # first loaded (via the inherited hook), so ResourceDependencies can @@ -107,13 +107,16 @@ module RCM def self.subclass_names = @@subclass_names.freeze def self.find(id) - return @@resource_find_cache[id] if @@resource_find_cache.key?(id) + resource_name = id.split(/[( ]/).first.to_sym + unless subclass_names.include?(resource_name) + raise NameError, "uninitialized constant RCM::#{resource_name.capitalize}" + end - klass = Object.const_get("RCM::#{id.split(/[( ]/).first.capitalize}") - resource = ObjectSpace.each_object(klass).find { _1.id == id } - raise NoSuchResourceObject, "Unable to find resource #{id}" if resource.nil? + resource = DSL.object(id) + return resource if resource.is_a?(Resource) - @@resource_find_cache[id] = resource + raise NoSuchResourceObject, "Unable to find resource #{id}" if resource.nil? end end end +# rubocop:enable Style/ClassVars |
