summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/dslkeywords/file.rb26
-rw-r--r--lib/dslkeywords/resource.rb14
-rw-r--r--playground/Rakefile5
-rw-r--r--test/lib/dslkeywords/dependency_test.rb32
-rw-r--r--test/lib/dslkeywords/file_test.rb4
5 files changed, 40 insertions, 41 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index e17cfeb..eeff82f 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -41,20 +41,20 @@ module RCM
def line(line) = @ensure_line = line
def path(file_path = nil) = file_path.nil? ? @file_path : @file_path = file_path
- # TODO: Replace :is with current method name dynamically
- def is(what) = @is = validate_op(:is, what, present, absent)
- def present = :present
- def absent = :absent
-
- def manage(what) = @manage_directory = validate_op(:is, what, directory) == directory
- def directory = :directory
-
- def without(what) = @without_backup = validate_op(:without, what, backup) == backup
- def backup = :backup
+ def is(what) = @is = validate_op(__method__, what, present, absent)
+ def manage(what) = @manage_directory = validate_op(__method__, what, directory) == directory
+ def without(what) = @without_backup = validate_op(__method__, what, backup) == backup
+ def from(what) = @from = validate_op(__method__, what, sourcefile, template)
+
+ def method_missing(method_name, *args)
+ if %i[present absent directory backup sourcefile template].include?(method_name)
+ method_name
+ else
+ super
+ end
+ end
- def from(what) = @from = validate_op(:from, what, sourcefile, template)
- def sourcefile = :sourcefile
- def template = :template
+ def respond_to_missing? = true
def evaluate!
return unless super
diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb
index be67b0a..3aff069 100644
--- a/lib/dslkeywords/resource.rb
+++ b/lib/dslkeywords/resource.rb
@@ -7,7 +7,7 @@ module RCM
module ResourceDependencies
def initialize(...)
super(...)
- @depends_on = Set.new
+ @requires = 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
@@ -22,16 +22,16 @@ module RCM
def respond_to_missing? = true
- def depends_on(*others)
- return @depends_on if others.empty?
+ def requires(*others)
+ return @requires if others.empty?
others.flatten.each do |other|
info "Registered dependency on #{other}"
- @depends_on << other
+ @requires << other
end
end
- def depends_on?(*others) = others.flatten.none? { |other| !@depends_on&.include?(other) }
+ def requires?(*others) = others.flatten.none? { |other| !@requires&.include?(other) }
end
# To resolve dependencies
@@ -49,10 +49,10 @@ module RCM
@loop_detection = true
# Try to evaluate all dependencies recursively.
- @depends_on.each.map { Resource.find(_1) }.each(&:evaluate!)
+ @requires.each.map { Resource.find(_1) }.each(&:evaluate!)
# Raise an exception when there are still unresolved dependencies.
- unresolved = @depends_on.each.map { Resource.find(_1) }.reject(&:evaluated)
+ unresolved = @requires.each.map { Resource.find(_1) }.reject(&:evaluated)
raise UnresolvedDependency, "Unresolved dependencies: #{unresolved.map(&:id)}" if unresolved.count.positive?
@loop_detection = false
diff --git a/playground/Rakefile b/playground/Rakefile
index d5c8fcc..ed08f26 100644
--- a/playground/Rakefile
+++ b/playground/Rakefile
@@ -38,9 +38,8 @@ desc 'foo'
task :foo do
configure do
file '/tmp/foo.txt' do
- line ensure_line 'foo'
- # TODO: Rename to requires
- depends_on file '/tmp/bar.txt'
+ line 'foo'
+ requires file '/tmp/bar.txt'
end
file '/tmp/bar.txt' do
diff --git a/test/lib/dslkeywords/dependency_test.rb b/test/lib/dslkeywords/dependency_test.rb
index 48a3e1a..aa1be78 100644
--- a/test/lib/dslkeywords/dependency_test.rb
+++ b/test/lib/dslkeywords/dependency_test.rb
@@ -4,47 +4,47 @@ require 'fileutils'
require_relative '../../../lib/dsl'
class RCMDependencyTest < Minitest::Test
- def test_depends_on
+ def test_requires
foo = nil
bar = nil
baz = nil
configure_from_scratch do
foo = notify 'foo' do
- depends_on notify 'bar', 'baz'
+ requires notify 'bar', 'baz'
:foo_message
end
bar = notify 'bar'
baz = notify 'baz' do
- depends_on notify 'bar'
+ requires notify 'bar'
:baz_message
end
end
- assert_equal 2, foo.depends_on.count
- assert foo.depends_on?("notify('bar')", "notify('baz')")
+ assert_equal 2, foo.requires.count
+ assert foo.requires?("notify('bar')", "notify('baz')")
- assert_equal 0, bar.depends_on.count
- refute bar.depends_on?('foo')
+ assert_equal 0, bar.requires.count
+ refute bar.requires?('foo')
- assert_equal 1, baz.depends_on.count
- assert baz.depends_on?("notify('bar')")
+ assert_equal 1, baz.requires.count
+ assert baz.requires?("notify('bar')")
end
- def test_depends_on_invalid_resource
+ def test_requires_invalid_resource
assert_raises(RCM::Keyword::KeywordError) do
configure_from_scratch do
- notify { depends_on invalid('baz') }
+ notify { requires invalid('baz') }
end
end
end
- def test_depends_on_non_existant_dependency
+ def test_requires_non_existant_dependency
assert_raises(RCM::Resource::NoSuchResourceObject) do
configure_from_scratch do
- notify { depends_on notify('nonexistant') }
+ notify { requires notify('nonexistant') }
end
end
end
@@ -52,7 +52,7 @@ class RCMDependencyTest < Minitest::Test
def test_dependency_loop
assert_raises(RCM::DependencyEvaluator::DependencyLoop) do
configure_from_scratch do
- notify('loop') { depends_on notify('loop') }
+ notify('loop') { requires notify('loop') }
end
end
end
@@ -60,8 +60,8 @@ class RCMDependencyTest < Minitest::Test
def test_dependency_loop_indirect
assert_raises(RCM::DependencyEvaluator::DependencyLoop) do
configure_from_scratch do
- notify('loop') { depends_on notify('pool') }
- notify('pool') { depends_on notify('loop') }
+ notify('loop') { requires notify('pool') }
+ notify('pool') { requires notify('loop') }
end
end
end
diff --git a/test/lib/dslkeywords/file_test.rb b/test/lib/dslkeywords/file_test.rb
index f414cf1..4353471 100644
--- a/test/lib/dslkeywords/file_test.rb
+++ b/test/lib/dslkeywords/file_test.rb
@@ -55,7 +55,7 @@ class RCMFileTest < Minitest::Test
path file_path
is absent
manage directory
- depends_on file :create_file_empty_directory_test
+ requires file :create_file_empty_directory_test
end
end
@@ -144,7 +144,7 @@ class RCMFileTest < Minitest::Test
file :new do
path file_path
manage directory
- depends_on file(:original)
+ requires file(:original)
:new_content
end
end