Sha256: c0b6e5bffaf5343c3eafd7478afc7e60347899d98419d30fd5055b2e79de24e5

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "massive_sitemap/builder/base"
# writer only has print and puts as interface

module MassiveSitemap
  module Builder
    class Rotating < Base
      NUM_URLS = 1..50_000

      OPTS = Base::OPTS.merge(
        :max_urls => NUM_URLS.max
      )

      def initialize(writer, options = {}, &block)
        @rotations = 0
        @urls      = 0

        super
      end

      # On rotation, close current file, and reopen a new one
      # with same file name but -<counter> appendend
      def init!(&block)
        unless @writer.inited?
          @urls    = 0
          filename = filename_with_rotation(@writer.options[:filename], @rotations)
          @rotations += 1
          @writer.init! :filename => filename
          header!(&block)
        end
      end

      def add_url!(location, attrs = {})
        if @urls >= @options[:max_urls]
          close!
        end
        super
        @urls += 1
      end

      private
      def filename_with_rotation(filename, rotation = nil)
        filename, _, ext = split_filename(filename)
        rotation = (rotation.to_i > 0) ? "-#{rotation}" : nil
        [filename, rotation, ext].join
      end

      def split_filename(filename)
        filename.to_s.scan(/^([^.]*?)(-[0-9]+)?(\..+)?$/).flatten
      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/builder/rotating.rb