Class: BBMB::Util::FileMission

Inherits:
Object
  • Object
show all
Defined in:
lib/bbmb/util/polling_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backup_dirObject

Returns the value of attribute backup_dir.



16
17
18
# File 'lib/bbmb/util/polling_manager.rb', line 16

def backup_dir
  @backup_dir
end

#deleteObject

Returns the value of attribute delete.



16
17
18
# File 'lib/bbmb/util/polling_manager.rb', line 16

def delete
  @delete
end

#directoryObject

Returns the value of attribute directory.



16
17
18
# File 'lib/bbmb/util/polling_manager.rb', line 16

def directory
  @directory
end

#glob_patternObject

Returns the value of attribute glob_pattern.



16
17
18
# File 'lib/bbmb/util/polling_manager.rb', line 16

def glob_pattern
  @glob_pattern
end

Instance Method Details

#file_pathsObject



17
18
19
20
21
22
23
# File 'lib/bbmb/util/polling_manager.rb', line 17

def file_paths
  path = File.expand_path(@glob_pattern || '*', @directory)
  res = Dir.glob(path).collect { |entry|
    File.expand_path(entry, @directory)
  }.compact
  res
end

#poll(&block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/bbmb/util/polling_manager.rb', line 24

def poll(&block)
  @directory = File.expand_path(@directory, BBMB.config.bbmb_dir) unless File.directory?(@directory) &&
      File.absolute_path(@directory) == @directory
  file_paths.each { |path|
    poll_path(path, &block)
  }
end

#poll_path(path, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bbmb/util/polling_manager.rb', line 31

def poll_path(path, &block)
  @backup_dir = BBMB.config.backup_dir if BBMB.config.respond_to?(:backup_dir)
  @backup_dir ||= Dir.tmpdir
  File.open(path) { |io|
    block.call(File.basename(path), io)
  }
rescue StandardError => err
  BBMB::Util::Mail.notify_error(err)
ensure
  if(@backup_dir)
    if File.absolute_path(@backup_dir) == @backup_dir
      dir = @backup_dir
    else
      dir = File.expand_path(@backup_dir, BBMB.config.bbmb_dir)
    end
    FileUtils.mkdir_p(dir)
    FileUtils.cp(path, dir)
  end
  if(@delete)
    FileUtils.rm(path)
  end
end