summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-14 10:47:55 +0200
committerPaul Buetow <paul@buetow.org>2026-03-14 10:47:55 +0200
commit8a641ff347d0584ea1ddc16a6ef81a8c16ab172d (patch)
tree095dc715e4df2a970188e3070e7d22464401e1d3
parent818ed50e2a54b40ccf7a7771bebe0312dc01a8b5 (diff)
Use active DSL registry for Resource.find
-rw-r--r--lib/dsl.rb2
-rw-r--r--lib/dslkeywords/resource.rb17
-rw-r--r--test/lib/dslkeywords/requires_test.rb25
3 files changed, 37 insertions, 7 deletions
diff --git a/lib/dsl.rb b/lib/dsl.rb
index 57bbef3..0414050 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -26,6 +26,8 @@ module RCM
@@objs = {}
end
+ def self.object(id) = @@objs[id]
+
reset!
include Config
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
diff --git a/test/lib/dslkeywords/requires_test.rb b/test/lib/dslkeywords/requires_test.rb
index 63e91ec..29c6850 100644
--- a/test/lib/dslkeywords/requires_test.rb
+++ b/test/lib/dslkeywords/requires_test.rb
@@ -1,3 +1,6 @@
+# frozen_string_literal: true
+
+# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
require 'minitest/autorun'
require 'fileutils'
@@ -71,4 +74,26 @@ class RCMRequiresTest < Minitest::Test
end
end
end
+
+ def test_configure_from_scratch_uses_current_resource_registry
+ first = second = nil
+
+ configure_from_scratch do
+ first = notify same do
+ first_run
+ end
+ end
+
+ configure_from_scratch do
+ second = notify same do
+ second_run
+ end
+ end
+
+ found = RCM::Resource.find("notify('same')")
+ refute_same first, found
+ assert_same second, found
+ end
end
+
+# rubocop:enable Metrics/AbcSize, Metrics/MethodLength