From 1217524955c7ea891ea85cb17dfd13f2b7b1fc3d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 1 Mar 2026 23:27:15 +0200 Subject: 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 --- lib/dsl.rb | 21 +++++++++++++++++++++ lib/dslkeywords/directory.rb | 10 +--------- lib/dslkeywords/file.rb | 9 +-------- lib/dslkeywords/symlink.rb | 8 +------- lib/dslkeywords/touch.rb | 8 +------- 5 files changed, 25 insertions(+), 31 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 diff --git a/lib/dslkeywords/directory.rb b/lib/dslkeywords/directory.rb index 6449d74..072c88a 100644 --- a/lib/dslkeywords/directory.rb +++ b/lib/dslkeywords/directory.rb @@ -100,15 +100,7 @@ module RCM class DSL def directory(file_path = nil, &block) - return :directory if file_path.nil? - return unless @conds_met - - d = Directory.new(file_path) - # Use source= for the recursive-copy source path rather than content=, - # keeping Directory's interface clean and purpose-named. - d.source(d.instance_eval(&block)) - self << d - d + register_keyword(Directory, :directory, file_path) { |d| d.source(d.instance_eval(&block)) } end end end diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index dc6f4d0..8e1c772 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -209,15 +209,8 @@ module RCM end class DSL - # Add file keyword to the DSL def file(file_path = nil, &block) - return :file if file_path.nil? - return unless @conds_met - - f = File.new(file_path) - f.content(f.instance_eval(&block)) - self << f - f + register_keyword(File, :file, file_path) { |f| f.content(f.instance_eval(&block)) } end end end diff --git a/lib/dslkeywords/symlink.rb b/lib/dslkeywords/symlink.rb index 160da6f..053e83d 100644 --- a/lib/dslkeywords/symlink.rb +++ b/lib/dslkeywords/symlink.rb @@ -22,13 +22,7 @@ module RCM class DSL def symlink(file_path = nil, &block) - return :symlink if file_path.nil? - return unless @conds_met - - s = Symlink.new(file_path) - s.content(s.instance_eval(&block)) - self << s - s + register_keyword(Symlink, :symlink, file_path) { |s| s.content(s.instance_eval(&block)) } end end end diff --git a/lib/dslkeywords/touch.rb b/lib/dslkeywords/touch.rb index 27e691b..13d63f7 100644 --- a/lib/dslkeywords/touch.rb +++ b/lib/dslkeywords/touch.rb @@ -26,13 +26,7 @@ module RCM class DSL def touch(file_path = nil, &block) - return :touch if file_path.nil? - return unless @conds_met - - t = Touch.new(file_path) - t.instance_eval(&block) if block - self << t - t + register_keyword(Touch, :touch, file_path) { |t| t.instance_eval(&block) if block } end end end -- cgit v1.2.3