Class: Micdrop::FilesSource

Inherits:
Object
  • Object
show all
Defined in:
lib/micdrop/files_source.rb

Overview

Takes a list of files, directory, or glob pattern as a source

Records in a file source will have the following items available to take:

  • :content (The full content of the file, lazy-loaded only if you take it)
  • :stream (A file IO stream)
  • :filename (The filename that was used to load the file, e.g. files/s.json)
  • :basename (The basename of the name, e.g. x.json)
  • :path (The full path to the file, e.g. /data/migration/files/x.json)
  • Anything returned by File.stat (:ctime, :mtime, :size, etc...)

Instance Method Summary collapse

Constructor Details

#initialize(dir, files: nil, glob: nil, binary_mode: false, **file_opts) ⇒ FilesSource

Returns a new instance of FilesSource.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/micdrop/files_source.rb', line 15

def initialize(dir, files: nil, glob: nil, binary_mode: false, **file_opts)
  @dir = dir
  @files = if files.nil?
             if glob.nil?
               Dir.children(dir)
             else
               Dir.glob(glob, flags: File::FNM_EXTGLOB, base: dir)
             end
           else
             files
           end
  @binary_mode = binary_mode
  @file_opts = file_opts
end

Instance Method Details

#each_pairObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/micdrop/files_source.rb', line 30

def each_pair
  return enum_for :each_pair unless block_given?

  @files.each do |filename|
    path = File.join(@dir, filename)
    unless File.file? path
      warn format("%s is not a file and will be skipped", path)
      next
    end

    yield filename, FilesSourceRecord.new(path, @binary_mode, @file_opts)
  end
end