lib/massive_sitemap/writer/file.rb in massive_sitemap-2.0.0.rc4 vs lib/massive_sitemap/writer/file.rb in massive_sitemap-2.0.0.rc5

- old
+ new

@@ -13,52 +13,72 @@ :root => '.', :force_overwrite => false, :filename => "sitemap.xml", ) + def initialize(options = {}) + super + @files = Dir[::File.join(@options[:root], "*#{::File.extname(filename)}")] + end + protected def open_stream + #create dir if not exists + @stream_id = filename ::File.dirname(tmp_filename).tap do |dir| FileUtils.mkdir_p(dir) unless ::File.exists?(dir) end ::File.open(tmp_filename, 'w:ASCII-8BIT') end - def close_stream(stream) - stream.close + def close_stream(file) + file.close # Move from tmp_file into acutal file ::File.delete(filename) if ::File.exists?(filename) ::File.rename(tmp_filename, filename) + @files << filename + rotate end def init? - if !options[:force_overwrite] && ::File.exists?(filename) - raise FileExistsException, "Can not create file: #{filename} exits" + if !@options[:force_overwrite] && @files.include?(filename) + error_message = "Can not create file: #{filename} exits" + rotate #push next possible filename + raise FileExistsException, error_message end true end - def streams - files.map do |path| + def stream_ids + @files.map do |path| [::File.basename(path), ::File.stat(path).mtime] end.compact end - def stream - options[:filename] + def stream_id + @stream_id && ::File.basename(@stream_id) end private def filename - ::File.join options[:root], options[:filename] + ::File.join @options[:root], @options[:filename] end def tmp_filename filename + ".tmp" end - def files - Dir[::File.join(options[:root], "*.xml")] + def rotate + @options[:filename] = with_rotation(@options[:filename]) + end + + def with_rotation(filename) + filename, rotation, ext = split_filename(filename) + [filename, "-", rotation.to_i + 1, ext].join + end + + def split_filename(filename) + filename.to_s.scan(/^([^.]*?)(?:-([0-9]+))?(\..+)?$/).flatten end end end end