Class: Philiprehberger::Tar::FilteredWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/tar.rb

Overview

A wrapper around Writer that applies include/exclude/newer_than filters.

Instance Method Summary collapse

Constructor Details

#initialize(writer, include: nil, exclude: nil, newer_than: nil, on_progress: nil) ⇒ FilteredWriter

Returns a new instance of FilteredWriter.

Parameters:

  • writer (Writer)

    the underlying writer

  • include (String, Array<String>, nil) (defaults to: nil)

    glob pattern(s) to include

  • exclude (String, Array<String>, nil) (defaults to: nil)

    glob pattern(s) to exclude

  • newer_than (Time, nil) (defaults to: nil)

    only add files modified after this time

  • on_progress (Proc, nil) (defaults to: nil)

    callback receiving (entry_name, index, total)



203
204
205
206
207
208
209
# File 'lib/philiprehberger/tar.rb', line 203

def initialize(writer, include: nil, exclude: nil, newer_than: nil, on_progress: nil)
  @writer = writer
  @include_patterns = include ? Array(include) : nil
  @exclude_patterns = exclude ? Array(exclude) : nil
  @newer_than = newer_than
  @on_progress = on_progress
end

Instance Method Details

#add_file(path, name: nil, total: nil) ⇒ Object

Add a file from the filesystem, applying filters.



212
213
214
215
216
217
218
# File 'lib/philiprehberger/tar.rb', line 212

def add_file(path, name: nil, total: nil)
  check_name = name || File.basename(path)
  return unless passes_filter?(check_name)
  return if @newer_than && File.mtime(path) <= @newer_than

  @writer.add_file(path, name: name, on_progress: @on_progress, total: total)
end

#add_string(name, content, mode: 0o644, total: nil) ⇒ Object

Add a file from a string, applying include/exclude filters.



221
222
223
224
225
# File 'lib/philiprehberger/tar.rb', line 221

def add_string(name, content, mode: 0o644, total: nil)
  return unless passes_filter?(name)

  @writer.add_string(name, content, mode: mode, on_progress: @on_progress, total: total)
end

Add a symlink, applying include/exclude filters.



228
229
230
231
232
# File 'lib/philiprehberger/tar.rb', line 228

def add_symlink(name, target:, total: nil)
  return unless passes_filter?(name)

  @writer.add_symlink(name, target: target, on_progress: @on_progress, total: total)
end