From 21c96e516044cf59a3b89c3b40c7efb279b0767c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 18 Feb 2025 19:02:29 +0200 Subject: refactor file DSL --- lib/dslkeywords/file.rb | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) (limited to 'lib') 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 -- cgit v1.2.3