Sha256: fac2bdcffe466d81babd59f12fd6eab0db35b09ae636d7e36b4f8b7fb1598064

Contents?: true

Size: 1.35 KB

Versions: 15

Compression:

Stored size: 1.35 KB

Contents

module SitemapGenerator
  # A class for generating sitemap names given the base for the filename.
  #
  # === Example
  # namer = SitemapNamer.new(:sitemap)
  # namer.to_s => 'sitemap1.xml.gz'
  # namer.next.to_s => 'sitemap2.xml.gz'
  class SitemapNamer
    NameError = Class.new(StandardError)

    # Params:
    #   base - string or symbol that forms the base of the generated filename
    #
    # Options include:
    #   :extension - Default: '.xml.gz'. File extension to append.
    #   :start     - Default: 1. Index at which to start counting.
    def initialize(base, options={});
      @options = options.reverse_merge(
        :extension => '.xml.gz',
        :start => 1
      )
      @base = base
      reset
    end

    def to_s
      "#{@base}#{@count}#{@options[:extension]}"
    end

    # Increment count and return self
    def next
      @count += 1
      self
    end

    # Decrement count and return self
    def previous
      raise NameError, "Already at the start of the series" if start?
      @count -= 1
      self
    end

    # Reset count to the starting index
    def reset
      @count = @options[:start]
    end

    def start?
      @count <= @options[:start]
    end
  end

  # A Namer for Sitemap Indexes.  The name never changes.
  class SitemapIndexNamer < SitemapNamer
    def to_s
      "#{@base}#{@options[:extension]}"
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sitemap_generator-2.2.1 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.8 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.7 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.7.pre1 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.6 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.5 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.4 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.3 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.2 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.1 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.1.0 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.0.1.pre2 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.0.1.pre1 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.0.1 lib/sitemap_generator/sitemap_namer.rb
sitemap_generator-2.0.0 lib/sitemap_generator/sitemap_namer.rb