Class: Boatman::MonitoredDirectory

Inherits:
Object
  • Object
show all
Includes:
Copyable
Defined in:
lib/boatman/monitored_directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Copyable

#copy, #move

Constructor Details

#initialize(path, match_data = nil) ⇒ MonitoredDirectory

Returns a new instance of MonitoredDirectory.



8
9
10
11
# File 'lib/boatman/monitored_directory.rb', line 8

def initialize(path, match_data = nil)
  @path = path
  @match_data = match_data
end

Instance Attribute Details

#match_dataObject

Returns the value of attribute match_data.



6
7
8
# File 'lib/boatman/monitored_directory.rb', line 6

def match_data
  @match_data
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/boatman/monitored_directory.rb', line 5

def path
  @path
end

Instance Method Details

#age(params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/boatman/monitored_directory.rb', line 13

def age(params)
  params.each do |key, value|
    case key
    when :greater_than
      @minimum_age = value
    when :less_than
      @maximum_age = value
    end
  end
end

#entries_matching(entry_pattern, type, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/boatman/monitored_directory.rb', line 24

def entries_matching(entry_pattern, type, &block)
  @minimum_age ||= false
  @maximum_age ||= false

  entry_paths = FTPUtils.ls(@path).grep(/#{entry_pattern}/).collect do |name|
    "#{@path}/#{name}"
  end

  Boatman.logger.debug "Found #{entry_paths.size} entries in #{@path}"
  entry_paths.each do |entry_path|
    next if type == :file && !FTPUtils::FTPFile.file?(entry_path)
    next if type == :directory && !FTPUtils::FTPFile.directory?(entry_path)

    age = Time.now - FTPUtils::FTPFile.mtime(entry_path)
    next if @minimum_age && age < @minimum_age
    next if @maximum_age && age > @maximum_age

    match_data = FTPUtils::FTPFile.basename(entry_path).match(entry_pattern) if entry_pattern.is_a?(Regexp)
    case type
    when :file
      entry = MonitoredFile.new(entry_path, match_data)
    when :directory
      entry = MonitoredDirectory.new(entry_path, match_data)
    end

    entry.instance_eval &block
  end
end

#files_ending_with(file_ending, &block) ⇒ Object



57
58
59
# File 'lib/boatman/monitored_directory.rb', line 57

def files_ending_with(file_ending, &block)
  return files_matching(/#{file_ending}$/, &block)
end

#files_matching(file_pattern, &block) ⇒ Object



53
54
55
# File 'lib/boatman/monitored_directory.rb', line 53

def files_matching(file_pattern, &block)
  return entries_matching(file_pattern, type = :file, &block)
end

#folders_ending_with(folder_ending, &block) ⇒ Object



65
66
67
# File 'lib/boatman/monitored_directory.rb', line 65

def folders_ending_with(folder_ending, &block)
  return folders_matching(/#{folder_ending}$/, &block)
end

#folders_matching(folder_pattern, &block) ⇒ Object



61
62
63
# File 'lib/boatman/monitored_directory.rb', line 61

def folders_matching(folder_pattern, &block)
  return entries_matching(folder_pattern, type = :directory, &block)
end