Module: Sumi::Function::File
- Included in:
- Sumi::Function
- Defined in:
- function/lib/sumi/function/file.rb
Overview
File-system methods promoted to Sumi::F. This module organizes the facade implementation. It is not a consumer mixin contract.
Instance Method Summary collapse
-
#changed?(file, content) {|content| ... } ⇒ Boolean
Checks whether a file differs from the supplied content.
-
#existing(file, extensions) ⇒ String?
Finds an existing file by exact path or by trying default extensions.
-
#existing!(file, extensions) ⇒ String
Finds an existing file or raises.
-
#existing_map(*files, extensions: []) ⇒ Hash{String => String, nil}
Maps each non-nil input file to an existing path lookup result.
-
#existing_map!(*files, extensions: []) ⇒ Hash{String => String}
Maps each non-nil input file to an existing path lookup result or raises.
-
#out(content, *paths) {|content| ... } ⇒ String?
Writes content to a file when it changed, or prints to stdout without a path.
-
#output_path(value, default:, context: "Output") ⇒ String
Resolves an output path, using the default when value is nil or an existing directory.
-
#path(value, context: "Path") ⇒ String
Converts a non-blank path to an expanded String without requiring it to exist.
-
#qualify(file, default_extension) ⇒ String
Adds a default extension when a path has no extension.
-
#subext(ext, *paths) ⇒ String
Replaces or removes the extension on a path.
-
#touch(*paths) ⇒ String
Creates a file and any missing parent directories.
Instance Method Details
#changed?(file, content) {|content| ... } ⇒ Boolean
Checks whether a file differs from the supplied content.
18 19 20 21 22 23 24 25 |
# File 'function/lib/sumi/function/file.rb', line 18 def changed?(file, content, &filter) return true unless ::File.exist?(file) old_content = ::File.read(file) old_content, content = [old_content, content].map(&filter) if filter old_content != content end |
#existing(file, extensions) ⇒ String?
Finds an existing file by exact path or by trying default extensions.
31 32 33 34 35 36 37 |
# File 'function/lib/sumi/function/file.rb', line 31 def existing(file, extensions) return file if ::File.file?(file) return nil unless ::File.extname(file).empty? return nil if extensions.empty? extensions.map { |ext| "#{file}.#{ext}" }.detect { |file| ::File.file?(file) } end |
#existing!(file, extensions) ⇒ String
Finds an existing file or raises.
44 45 46 47 48 |
# File 'function/lib/sumi/function/file.rb', line 44 def existing!(file, extensions) existing(file, extensions).tap do |found| ArgumentError.("No matching file(s) found: #{file}") unless found end end |
#existing_map(*files, extensions: []) ⇒ Hash{String => String, nil}
Maps each non-nil input file to an existing path lookup result.
54 55 56 57 58 |
# File 'function/lib/sumi/function/file.rb', line 54 def existing_map(*files, extensions: []) {}.tap do |found| files.compact.each { |file| found[file] = existing(file, extensions) } end end |
#existing_map!(*files, extensions: []) ⇒ Hash{String => String}
66 67 68 69 70 71 72 73 |
# File 'function/lib/sumi/function/file.rb', line 66 def existing_map!(...) found = F.existing_map(...) missings = found.select { |_, match| match.nil? }.keys ArgumentError.("No matching file(s) found: #{missings.join(", ")}") unless missings.empty? found end |
#out(content, *paths) {|content| ... } ⇒ String?
Writes content to a file when it changed, or prints to stdout without a path.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'function/lib/sumi/function/file.rb', line 83 def out(content, *paths, &filter) if paths.empty? ::Kernel.puts(content) else file = ::File.(::File.join(*paths)) output = "#{content.chomp}\n" return unless changed?(file, output, &filter) file.tap { ::File.write(file, output) } end end |
#output_path(value, default:, context: "Output") ⇒ String
Resolves an output path, using the default when value is nil or an existing directory. The default is validated only when used. This method does not create files or directories.
103 104 105 106 107 108 109 110 111 |
# File 'function/lib/sumi/function/file.rb', line 103 def output_path(value, default:, context: "Output") return path(default, context: "#{context} default") if value.nil? output = path(value, context: "#{context} path") return output unless ::File.directory?(output) default = path(default, context: "#{context} default") ::File.join(output, ::File.basename(default)) end |
#path(value, context: "Path") ⇒ String
Converts a non-blank path to an expanded String without requiring it to exist.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'function/lib/sumi/function/file.rb', line 118 def path(value, context: "Path") path = value.respond_to?(:to_path) ? value.to_path : value ArgumentError.("#{context} must be a String or path-like object") unless path.is_a?(::String) ArgumentError.("#{context} must be provided") if path.strip.empty? ::File.(path) rescue ::Sumi::ArgumentError raise rescue ::StandardError => e ArgumentError.("#{context} must be a String or path-like object: #{e.}") end |
#qualify(file, default_extension) ⇒ String
Adds a default extension when a path has no extension.
134 135 136 137 138 |
# File 'function/lib/sumi/function/file.rb', line 134 def qualify(file, default_extension) return file unless ::File.extname(file).empty? "#{file}.#{default_extension}" end |
#subext(ext, *paths) ⇒ String
Replaces or removes the extension on a path.
144 145 146 147 148 149 150 151 152 153 |
# File 'function/lib/sumi/function/file.rb', line 144 def subext(ext, *paths) path = ::File.join(*paths) return path if %w[. ..].include?(path) return path unless ext ext = ".#{ext}" unless ext.empty? || ext.start_with?(".") "#{path.delete_suffix(::File.extname(path))}#{ext}" end |