summaryrefslogtreecommitdiff
path: root/lib/dsl.rb
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-01 23:27:15 +0200
committerPaul Buetow <paul@buetow.org>2026-03-01 23:27:15 +0200
commit1217524955c7ea891ea85cb17dfd13f2b7b1fc3d (patch)
treef1ed556cc9308ed153c32ce7a9f812af40742a34 /lib/dsl.rb
parentfba26f6d9f18600dc313b6d4ade65d536e9762e9 (diff)
refactor: extract register_keyword helper to eliminate 4x DSL duplication
Each of the file/symlink/touch/directory DSL methods repeated the same four-step pattern: nil-path identity return, @conds_met guard, create- configure-register, return object. Add a private register_keyword helper to DSL in dsl.rb and collapse each method to a one-liner. No behaviour changed; all 29 tests continue to pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'lib/dsl.rb')
-rw-r--r--lib/dsl.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/dsl.rb b/lib/dsl.rb
index f4e34ac..d80c701 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -46,6 +46,27 @@ module RCM
@scheduled << @@objs[obj.id] = obj
end
+
+ private
+
+ # Shared helper for all file-system keyword registrations.
+ # Returns the keyword symbol when called without a path (used by the
+ # Chained DSL to identify resource types without creating an object).
+ # Otherwise guards on @conds_met, instantiates klass, lets the caller
+ # configure the object, registers it, and returns it.
+ #
+ # The block is always yielded — callers that accept an optional DSL
+ # block must guard for nil themselves inside the closure, e.g.
+ # register_keyword(Touch, :touch, path) { |t| t.instance_eval(&block) if block }
+ def register_keyword(klass, name, path)
+ return name if path.nil?
+ return unless @conds_met
+
+ obj = klass.new(path)
+ yield obj
+ self << obj
+ obj
+ end
end
end