def scan_now
checks = {
:date => {
:use=>false,
:proc=>Proc.new{ |file,stats| stats.mtime }
},
:size => {
:use=>false,
:proc=>Proc.new{ |file,stats| stats.size }
},
:crc => {
:use=>false,
:proc=>Proc.new{ |file,stats| 1 }
}
}
checks.each_pair{ |check_name,check|
check[:use] = (@onmodify_checks == check_name) || ( @onmodify_checks.respond_to?( :include? ) && @onmodify_checks.include?( check_name ) )
}
@directory.rewind
@directory.each{ |fname|
file_path = "#{@directory.path}/#{fname}"
next if (@name_regexp.respond_to?( :match ) && !@name_regexp.match( fname )) || !File.file?( file_path )
the_file = File.new( file_path )
file_stats = File.stat( file_path )
saved_stats = @known_file_stats[file_path]
new_stats = {}
checks.each_pair{ |check_name,check|
new_stats[check_name] = check[:proc].call( the_file, file_stats )
}
if saved_stats
if @on_modify.respond_to?( :call )
sufficiently_modified = @onmodify_requiresall
saved_stats = @known_file_stats[file_path]
checks.each_pair{ |check_name,check|
stat_changed = check[:use] && ( saved_stats[check_name] != new_stats[check_name] )
if @onmodify_requiresall
sufficiently_modified &&= stat_changed
else
sufficiently_modified ||= stat_changed
end
saved_stats[check_name] = new_stats[check_name]
}
@on_modify.call( the_file, saved_stats ) if sufficiently_modified
end
elsif @on_add.respond_to?( :call ) && (@scanned_once || @onadd_for_existing)
@known_file_stats[file_path] = new_stats
@on_add.call( the_file, new_stats )
end
the_file.close
}
if @on_remove.respond_to?( :call )
@known_file_stats.each_pair{ |path,stats|
next if File.file?( path )
stats[:path] = path
@on_remove.call( stats )
@known_file_stats.delete(path)
}
end
@scanned_once = true
end