Sha256: 30082a169097decdf333902d6a58111c35f768f68118ffde467c99971b5a241d

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'fileutils'
require 'massive_sitemap/writer/base'

# Write into File

module MassiveSitemap
  module Writer
    class File < Base

      class FileExistsException < IOError; end

      OPTS = Base::OPTS.merge(
        :root            => '.',
        :force_overwrite => false,
        :filename        => "sitemap.xml",
      )

      protected
      def open_stream
        ::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
        # Move from tmp_file into acutal file
        ::File.delete(filename) if ::File.exists?(filename)
        ::File.rename(tmp_filename, filename)
      end

      def init?
        if !options[:force_overwrite] && ::File.exists?(filename)
          raise FileExistsException, "Can not create file: #{filename} exits"
        end
        true
      end

      def streams
        files.map do |path|
          [::File.basename(path), ::File.stat(path).mtime]
        end.compact
      end

      def stream
        options[:filename]
      end

      private
      def filename
        ::File.join options[:root], options[:filename]
      end

      def tmp_filename
        filename + ".tmp"
      end

      def files
        Dir[::File.join(options[:root], "*.xml")]
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
massive_sitemap-2.0.0.rc4 lib/massive_sitemap/writer/file.rb