summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-16 23:08:46 +0200
committerPaul Buetow <paul@buetow.org>2025-02-16 23:08:46 +0200
commit556d1d13ad6ce056fb52d920e36c08192c7103c5 (patch)
treeeeb07cd50aa32c943113bc00cd7ba892ecbd890c
parent5c2865998aec2db34c42ee092952ec37ebd3cf16 (diff)
change foo[...] to foo(...) syntax for deps
test deps
-rw-r--r--lib/dsl.rb2
-rw-r--r--lib/dslkeywords/keyword.rb2
-rw-r--r--lib/dslkeywords/resource.rb20
-rw-r--r--test/lib/dslkeywords/dependency_test.rb25
4 files changed, 29 insertions, 20 deletions
diff --git a/lib/dsl.rb b/lib/dsl.rb
index caed790..b9062e5 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -25,7 +25,7 @@ module RCM
def initialize(reset)
DSL.reset! if reset
- @id = "dsl[#{@@rcm_counter += 1}]"
+ @id = "dsl(#{@@rcm_counter += 1})"
@conds_met = true
@scheduled = []
yield self if block_given?
diff --git a/lib/dslkeywords/keyword.rb b/lib/dslkeywords/keyword.rb
index 2696e41..2dfff91 100644
--- a/lib/dslkeywords/keyword.rb
+++ b/lib/dslkeywords/keyword.rb
@@ -9,7 +9,7 @@ module RCM
include Options
include Log
- def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '').downcase}['#{name}']"
+ def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '').downcase}('#{name}')"
def to_s = @id
end
end
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb
index c9098d4..18239fc 100644
--- a/lib/dslkeywords/resource.rb
+++ b/lib/dslkeywords/resource.rb
@@ -13,31 +13,27 @@ module RCM
end
end
- # Only to have the resourcename[id] syntax available in the DSL
- class SyntaxSugar
- def initialize(name) = @name = name
- def [](other) = "#{@name}['#{other}']"
- end
-
class NoSuchResourceType < StandardError; end
- def method_missing(method_name)
+ def method_missing(method_name, *args)
raise NoSuchResourceType, "No such resource type: #{method_name}" unless @valid_resources.include?(method_name)
- SyntaxSugar.new(method_name)
+ args.map { |arg| "#{method_name}('#{arg}')" }
end
def respond_to_missing? = true
def depends_on(*others)
- @dependencies = {} if @dependencies.nil?
- others.each do |other|
+ @depends_on = {} if @depends_on.nil?
+ return @depends_on if others.empty?
+
+ others.flatten.each do |other|
info "Registered dependency on #{other}"
- @dependencies[other] = {}
+ @depends_on[other] = {}
end
end
- def dependencies = @dependencies.nil ? [] : @dependencies
+ def depends_on?(other) = @depends_on.nil? ? false : @depends_on.key?(other)
end
# A resource is something concrete to be managed, e.g. a file, or a CRON job.
diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb
index 324a756..393552f 100644
--- a/test/lib/dslkeywords/dependency_test.rb
+++ b/test/lib/dslkeywords/dependency_test.rb
@@ -5,25 +5,38 @@ require_relative '../../../lib/dsl'
class RCMDependencyTest < Minitest::Test
def test_depends_on
+ foo = nil
+ bar = nil
+ baz = nil
+
configure_from_scratch do
- notify 'foo' do
- depends_on notify['bar'], notify['baz']
+ foo = notify 'foo' do
+ depends_on notify 'bar', 'baz'
:foo_message
end
- notify 'bar'
+ bar = notify 'bar'
- notify 'baz' do
- depends_on notify['bar']
+ baz = notify 'baz' do
+ depends_on notify 'bar'
:baz_message
end
end
+
+ assert_equal 2, foo.depends_on.keys.length
+ assert foo.depends_on?("notify('bar')")
+ assert foo.depends_on?("notify('baz')")
+
+ assert_equal 0, bar.depends_on.keys.length
+
+ assert_equal 1, baz.depends_on.keys.length
+ assert baz.depends_on?("notify('bar')")
end
def test_depends_on_invalid_resource
assert_raises(RCM::ResourceDependencies::NoSuchResourceType) do
configure_from_scratch do
- notify { depends_on invalid['baz'] }
+ notify { depends_on invalid('baz') }
end
end
end