summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-17 00:20:47 +0200
committerPaul Buetow <paul@buetow.org>2025-02-17 00:20:47 +0200
commite13d1a0a7df840bd86e68f6b9604c764c54cc60f (patch)
treea69b6ec0ceb804652f71f3f5aff68c30cab23601
parenta8d7029846faff9db5a99dbe011b3765a1350ee4 (diff)
use a set
-rw-r--r--lib/dslkeywords/resource.rb10
-rw-r--r--test/lib/dslkeywords/dependency_test.rb6
2 files changed, 8 insertions, 8 deletions
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb
index 29824e9..11df70e 100644
--- a/lib/dslkeywords/resource.rb
+++ b/lib/dslkeywords/resource.rb
@@ -7,7 +7,7 @@ module RCM
module ResourceDependencies
def initialize(...)
super(...)
- @depends_on = {}
+ @depends_on = Set.new
@valid_resources = Set.new
ObjectSpace.each_object(Class).each do |klass|
@valid_resources << klass.to_s.sub('RCM::', '').downcase.to_sym if klass < Resource
@@ -29,11 +29,11 @@ module RCM
others.flatten.each do |other|
info "Registered dependency on #{other}"
- @depends_on[other] = nil
+ @depends_on << other
end
end
- def depends_on?(*others) = others.flatten.none? { |other| !@depends_on&.key?(other) }
+ def depends_on?(*others) = others.flatten.none? { |other| !@depends_on&.include?(other) }
end
# To resolve dependencies
@@ -51,10 +51,10 @@ module RCM
@loop_detection = true
# Try to evaluate all dependencies recursively.
- @depends_on.each_key.map { Resource.find(_1) }.each(&:evaluate!)
+ @depends_on.each.map { Resource.find(_1) }.each(&:evaluate!)
# Raise an exception when there are still unresolved dependencies.
- unresolved = @depends_on.each_key.map { Resource.find(_1) }.reject(&:evaluated)
+ unresolved = @depends_on.each.map { Resource.find(_1) }.reject(&:evaluated)
raise UnresolvedDependency, "Unresolved dependencies: #{unresolved.map(&:id)}" if unresolved.count.positive?
@loop_detection = false
diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb
index f1280eb..aeb2790 100644
--- a/test/lib/dslkeywords/dependency_test.rb
+++ b/test/lib/dslkeywords/dependency_test.rb
@@ -23,13 +23,13 @@ class RCMDependencyTest < Minitest::Test
end
end
- assert_equal 2, foo.depends_on.keys.length
+ assert_equal 2, foo.depends_on.count
assert foo.depends_on?("notify('bar')", "notify('baz')")
- assert_equal 0, bar.depends_on.keys.length
+ assert_equal 0, bar.depends_on.count
refute bar.depends_on?('foo')
- assert_equal 1, baz.depends_on.keys.length
+ assert_equal 1, baz.depends_on.count
assert baz.depends_on?("notify('bar')")
end