diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-16 23:49:20 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-16 23:49:20 +0200 |
| commit | d64135e44904500d65b029fb31216a52d507a7be (patch) | |
| tree | 094b9804388ea61035d41c24611149940a749127 | |
| parent | a246ade229a2a1578358eb25fe5855bc5cf22732 (diff) | |
can find resource objs
| -rw-r--r-- | lib/dslkeywords/file.rb | 1 | ||||
| -rw-r--r-- | lib/dslkeywords/notify.rb | 6 | ||||
| -rw-r--r-- | lib/dslkeywords/resource.rb | 31 | ||||
| -rw-r--r-- | test/lib/dslkeywords/dependency_test.rb | 9 |
4 files changed, 42 insertions, 5 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 501b7c8..b42f223 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -25,6 +25,7 @@ module RCM def ensure_line(line) = @ensure_line = line def evaluate! + return unless super return evaluate_ensure_line! unless @ensure_line.nil? write_content!(real_content) diff --git a/lib/dslkeywords/notify.rb b/lib/dslkeywords/notify.rb index 602d216..e1d8a3d 100644 --- a/lib/dslkeywords/notify.rb +++ b/lib/dslkeywords/notify.rb @@ -15,7 +15,11 @@ module RCM @message = msg unless msg.nil? end - def evaluate! = puts "#{id} => #{@message}" + def evaluate! + return unless super + + puts "#{id} => #{@message}" + end end # Add notify keyword to the DSL diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index 4c28809..1634061 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -29,19 +29,42 @@ module RCM others.flatten.each do |other| info "Registered dependency on #{other}" - @depends_on[other] = {} + @depends_on[other] = nil end end - def depends_on?(*others) - return false if @depends_on.nil? + def depends_on?(*others) = others.flatten.none? { |other| !@depends_on&.key?(other) } + end + + # To resolve dependencies + module DependencyEvaluator + attr_reader :evaluated + + def evaluate! + return false if @evaluated + + @depends_on = {} if @depends_on.nil? + @depends_on.each_key do |id| + dependency = Resource.find(id) + end - others.flatten.none? { |other| !@depends_on.key?(other) } + @evaluated = true end end # A resource is something concrete to be managed, e.g. a file, or a CRON job. class Resource < Keyword + include DependencyEvaluator include ResourceDependencies + + class NoSuchResourceObject < StandardError; end + + def self.find(id) + klass = Object.const_get("RCM::#{id.split('(').first.capitalize}") + resource = ObjectSpace.each_object(klass).find { |obj| obj.id == id } + raise NoSuchResourceObject, "Unable to find resource #{id}" if resource.nil? + + resource + end end end diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb index 4a71485..e9bc205 100644 --- a/test/lib/dslkeywords/dependency_test.rb +++ b/test/lib/dslkeywords/dependency_test.rb @@ -27,6 +27,7 @@ class RCMDependencyTest < Minitest::Test assert foo.depends_on?("notify('bar')", "notify('baz')") assert_equal 0, bar.depends_on.keys.length + refute bar.depends_on?('foo') assert_equal 1, baz.depends_on.keys.length assert baz.depends_on?("notify('bar')") @@ -39,4 +40,12 @@ class RCMDependencyTest < Minitest::Test end end end + + def test_depends_on_non_existant_dependency + assert_raises(RCM::Resource::NoSuchResourceObject) do + configure_from_scratch do + notify { depends_on notify('nonexistant') } + end + end + end end |
