summaryrefslogtreecommitdiff
path: root/lib/dslkeywords/directory.rb
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-01 23:21:20 +0200
committerPaul Buetow <paul@buetow.org>2026-03-01 23:21:20 +0200
commitfba26f6d9f18600dc313b6d4ade65d536e9762e9 (patch)
treed10d582416c5a1e831de7eae220d16cc280919ce /lib/dslkeywords/directory.rb
parent85f1805bea38d5f1558c92ff354f2d5bf832f0e6 (diff)
refactor: narrow BasicFile interface — apply ISP to Touch and Directory
Touch and Directory inherited content/from from BaseFile but had no use for them. Directory worked around this by repurposing content() as a source-directory path store, which was semantically misleading. Changes: - Move content/from down into BaseFile (only File and Symlink need them) - Move evaluate_absent! up into BasicFile (Touch and Directory need it) - Move UnsupportedOperation up into BasicFile (validate raises it there) - Re-parent Touch and Directory to BasicFile directly - Add Directory#source accessor to replace the content() misuse - Update DSL#directory to call d.source(...) instead of d.content(...) All 29 tests continue to pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'lib/dslkeywords/directory.rb')
-rw-r--r--lib/dslkeywords/directory.rb26
1 files changed, 18 insertions, 8 deletions
diff --git a/lib/dslkeywords/directory.rb b/lib/dslkeywords/directory.rb
index 402bead..6449d74 100644
--- a/lib/dslkeywords/directory.rb
+++ b/lib/dslkeywords/directory.rb
@@ -5,9 +5,15 @@ require_relative 'file'
module RCM
# Manages directories: create, delete/purge, or recursively copy from
# a source directory. Backup is performed before destructive operations.
- class Directory < BaseFile
+ # Extends BasicFile directly — Directory has no file content or sourcing,
+ # so it must not inherit content/from from BaseFile (ISP). The source
+ # directory for recursive copy is stored via the separate #source method.
+ class Directory < BasicFile
def recursively = @recursively = true
+ # Set or get the source directory path used for recursive copy.
+ def source(path = nil) = path.nil? ? @source_path : @source_path = path
+
def evaluate!
return unless super
@@ -35,6 +41,8 @@ module RCM
end
end
+ # Override BasicFile#evaluate_absent! with directory-specific behaviour:
+ # optionally recursive removal and backup of the whole directory tree.
def evaluate_absent!
return unless ::File.directory?(@file_path)
@@ -51,20 +59,20 @@ module RCM
end
def evaluate_present_recursively!
- source_path = content
- raise "Source #{source_path} is not a directory!" unless ::File.directory?(source_path)
+ src = source
+ raise "Source #{src} is not a directory!" unless ::File.directory?(src)
if ::File.exist?(@file_path)
raise "Destination #{@file_path} is not a directory!" unless ::File.directory?(@file_path)
- backup_recursively!(source_path, @file_path) unless @without_backup
+ backup_recursively!(src, @file_path) unless @without_backup
end
- do? "Copying #{source_path} -> #{@file_path} recursively" do
+ do? "Copying #{src} -> #{@file_path} recursively" do
if ::File.directory?(@file_path)
- Dir["#{source_path}/*"].each { FileUtils.cp_r(_1, @file_path) }
+ Dir["#{src}/*"].each { FileUtils.cp_r(_1, @file_path) }
else
- FileUtils.cp_r(source_path, @file_path)
+ FileUtils.cp_r(src, @file_path)
end
end
end
@@ -96,7 +104,9 @@ module RCM
return unless @conds_met
d = Directory.new(file_path)
- d.content(d.instance_eval(&block))
+ # 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
end