diff options
| author | Paul Buetow <paul@buetow.org> | 2025-02-18 19:02:29 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-02-18 19:02:29 +0200 |
| commit | 21c96e516044cf59a3b89c3b40c7efb279b0767c (patch) | |
| tree | 30ccc58ee47ff0b5798ca55f4ea021cbd0e21401 /lib/dslkeywords/file.rb | |
| parent | de93bdd4a9897a4dbe1e54a8e13944cce62b7ac9 (diff) | |
refactor file DSL
Diffstat (limited to 'lib/dslkeywords/file.rb')
| -rw-r--r-- | lib/dslkeywords/file.rb | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index b3f5be4..e17cfeb 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -38,34 +38,23 @@ module RCM @content = text.instance_of?(Array) ? text.join("\n") : text end - def manage(what) - what = what.to_sym - raise UnsupportedOperation, "Unsupported 'manage' operation #{what}" unless %i[directory].include?(what) - - @manage_directory = true - end - - def directory = :directory - - def from_sourcefile = @from_sourcefile = true - def from_template = @from_template = true - def without_backup = @without_backup = true # TODO: Rename to 'without backup' def line(line) = @ensure_line = line + def path(file_path = nil) = file_path.nil? ? @file_path : @file_path = file_path - def path(file_path = nil) - return @file_path if file_path.nil? + # TODO: Replace :is with current method name dynamically + def is(what) = @is = validate_op(:is, what, present, absent) + def present = :present + def absent = :absent - @file_path = file_path - end + def manage(what) = @manage_directory = validate_op(:is, what, directory) == directory + def directory = :directory - def is(what) - @is = what.to_sym - raise UnsupportedOperation, "Unsupported 'is' operation #{@is}" unless %i[present absent].include?(@is) - end + def without(what) = @without_backup = validate_op(:without, what, backup) == backup + def backup = :backup - # To allow 'is present' - def present = :present - def absent = :absent + def from(what) = @from = validate_op(:from, what, sourcefile, template) + def sourcefile = :sourcefile + def template = :template def evaluate! return unless super @@ -77,6 +66,13 @@ module RCM private + def validate_op(matter, what, *valids) + what = what.to_sym + raise UnsupportedOperation, "Unsupported '#{matter}' operation #{what}" unless valids.include?(what) + + what + end + def evaluate_ensure_line! return evaluate_ensure_line_absent! if %i[absent].include?(@is) return write!(@ensure_line) unless ::File.file?(@file_path) @@ -142,8 +138,8 @@ module RCM end def real_content - text = @from_sourcefile ? ::File.read(@content) : @content - @from_template ? ERB.new(text).result : text + text = @from == sourcefile ? ::File.read(@content) : @content + @from == template ? ERB.new(text).result : text end end |
