Class: MassiveSitemap::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_sitemap/builder/base.rb

Direct Known Subclasses

Index, Rotating

Constant Summary collapse

OPTS =
{
  :url       => nil,
  :indent_by => 2
}
HEADER_NAME =
'urlset'
HEADER_ATTRIBUTES =
{
  'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
  'xmlns:xsi'          => "http://www.w3.org/2001/XMLSchema-instance",
  'xsi:schemaLocation' => "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer, options = {}, &block) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
# File 'lib/massive_sitemap/builder/base.rb', line 22

def initialize(writer, options = {}, &block)
  @writer      = writer
  @options     = self.class::OPTS.merge(options)
  @opened_tags = []

  process(&block)
end

Class Method Details

.generate(writer, options = {}, &block) ⇒ Object

alias for .new



31
32
33
# File 'lib/massive_sitemap/builder/base.rb', line 31

def self.generate(writer, options = {}, &block)
  self.new(writer, options, &block)
end

Instance Method Details

#add(path, attrs = {}) ⇒ Object

add url, prefix with given base_url



36
37
38
# File 'lib/massive_sitemap/builder/base.rb', line 36

def add(path, attrs = {})
  add_url! ::File.join(url, path), attrs
end

#close!(indent = true) ⇒ Object

close a tag, if last one, close writer too



50
51
52
53
54
55
56
57
58
59
# File 'lib/massive_sitemap/builder/base.rb', line 50

def close!(indent = true)
  if name = @opened_tags.pop
    @writer.print "\n" + ' ' * @options[:indent_by] * @opened_tags.size if indent
    @writer.print "</#{name}>"
    if @opened_tags.size == 0
      @writer.close!
      true
    end
  end
end

#init_writer!(writer_options = {}, &block) ⇒ Object

implicitly called by add_url!, call explicitly to check if writer can be used



41
42
43
44
45
46
47
# File 'lib/massive_sitemap/builder/base.rb', line 41

def init_writer!(writer_options = {}, &block)
  unless @writer.inited?
    @writer.init!(writer_options)
    header!
  end
  process(&block)
end