Class: SitemapGenerator::SimpleNamer

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_generator/simple_namer.rb

Overview

A class for generating sitemap filenames.

The SimpleNamer uses the same namer instance for the sitemap index and the sitemaps. If no index is needed, the first sitemap gets the first name. However, if an index is needed, the index gets the first name.

A typical sequence would looks like this:

* sitemap.xml.gz
* sitemap1.xml.gz
* sitemap2.xml.gz
* sitemap3.xml.gz
* ...

Arguments:

base - string or symbol that forms the base of the generated filename e.g.
     if `:geo`, files are generated like `geo.xml.gz`, `geo1.xml.gz`, `geo2.xml.gz` etc.

Options:

:extension - Default: '.xml.gz'. File extension to append.
:start     - Default: 1. Numerical index at which to start counting.
:zero      - Default: nil.  A string or number that is appended to +base+
           to create the first name in the sequence.  So setting this
           to '_index' would produce 'sitemap_index.xml.gz' as
           the first name.  Thereafter, the numerical index defined by +start+
           is used, and subsequent names would be 'sitemap1.xml.gz', 'sitemap2.xml.gz', etc.
           In these examples the `base` string is assumed to be 'sitemap'.

Instance Method Summary collapse

Constructor Details

#initialize(base, options = {}) ⇒ SimpleNamer

Returns a new instance of SimpleNamer.



31
32
33
34
35
36
37
38
# File 'lib/sitemap_generator/simple_namer.rb', line 31

def initialize(base, options = {})
  @options = SitemapGenerator::Utilities.reverse_merge(options,
                                                       zero: nil, # marker for the start of the series
                                                       extension: '.xml.gz',
                                                       start: 1)
  @base = base
  reset
end

Instance Method Details

#nextObject

Return this instance set to the next name



56
57
58
59
60
61
62
63
# File 'lib/sitemap_generator/simple_namer.rb', line 56

def next
  if start?
    @count = @options[:start]
  else
    @count += 1
  end
  self
end

#previousObject

Return this instance set to the previous name

Raises:

  • (NameError)


66
67
68
69
70
71
72
73
74
75
# File 'lib/sitemap_generator/simple_namer.rb', line 66

def previous
  raise NameError, 'Already at the start of the series' if start?

  if @count <= @options[:start]
    @count = @options[:zero]
  else
    @count -= 1
  end
  self
end

#resetObject

Reset to the first name



46
47
48
# File 'lib/sitemap_generator/simple_namer.rb', line 46

def reset
  @count = @options[:zero]
end

#start?Boolean

True if on the first name

Returns:

  • (Boolean)


51
52
53
# File 'lib/sitemap_generator/simple_namer.rb', line 51

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

#to_sObject



40
41
42
43
# File 'lib/sitemap_generator/simple_namer.rb', line 40

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