diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-01 23:21:20 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-01 23:21:20 +0200 |
| commit | fba26f6d9f18600dc313b6d4ade65d536e9762e9 (patch) | |
| tree | d10d582416c5a1e831de7eae220d16cc280919ce /lib/dslkeywords/file.rb | |
| parent | 85f1805bea38d5f1558c92ff354f2d5bf832f0e6 (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/file.rb')
| -rw-r--r-- | lib/dslkeywords/file.rb | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index 273968c..dc6f4d0 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -9,12 +9,18 @@ require_relative 'file_backup' module RCM # Base class shared by all file-system resources (files, symlinks, # touch, directories). Manages path, state (:present/:absent/:purged), - # permissions (mode/owner/group), parent-directory lifecycle, and the - # FileBackup mixin. + # permissions (mode/owner/group), and parent-directory lifecycle. + # Does NOT include content/templating — those belong in BaseFile so + # Touch and Directory (which have no file content) don't inherit them. class BasicFile < Resource include Chained include FileBackup + # Raised by validate when an unsupported DSL option is used. + # Defined here so BasicFile#validate can raise it even when the + # concrete class does not extend BaseFile. + class UnsupportedOperation < StandardError; end + def initialize(file_path) super(file_path) @file_path = file_path @@ -37,14 +43,6 @@ module RCM true end - def content(text = nil) - if text.nil? - text = @from == :sourcefile ? ::File.read(@content) : @content - return @from == :template ? ERB.new(text).result : text - end - @content = text.instance_of?(Array) ? text.join("\n") : text - end - protected def permissions!(file_path = path) @@ -63,6 +61,18 @@ module RCM "Unsupported '#{method}' operation #{what} (#{what.class})" end + # Delete the resource and optionally remove orphaned parent directories. + # Used by File, Symlink, and Touch; Directory overrides this. + def evaluate_absent! + if ::File.exist?(@file_path) + do? "Deleting #{@file_path}" do + backup!(@file_path) + ::File.delete(@file_path) if ::File.file?(@file_path) + end + end + cleanup_parent_directory! if @manage_directory + end + def create_parent_directory! dirname = ::File.dirname(@file_path) return if ::File.directory?(dirname) @@ -109,23 +119,22 @@ module RCM end end - # Intermediate base for resources that have file content and support - # :sourcefile / :template sourcing, and absent-state deletion. + # Intermediate base for resources that carry file content: regular files + # and symlinks. Adds content storage with optional ERB templating or + # sourcefile reading. Touch and Directory extend BasicFile directly so + # they are not burdened with content/from (ISP). class BaseFile < BasicFile - class UnsupportedOperation < StandardError; end - def from(what) = @from = validate(__method__, what.to_sym, :sourcefile, :template) - protected - - def evaluate_absent! - if ::File.exist?(@file_path) - do? "Deleting #{@file_path}" do - backup!(@file_path) - ::File.delete(@file_path) if ::File.file?(@file_path) - end + # Return or set the resource's content. + # Getter: resolves ERB templates or reads sourcefile on demand. + # Setter: stores plain text or joins an array with newlines. + def content(text = nil) + if text.nil? + text = @from == :sourcefile ? ::File.read(@content) : @content + return @from == :template ? ERB.new(text).result : text end - cleanup_parent_directory! if @manage_directory + @content = text.instance_of?(Array) ? text.join("\n") : text end end |
