Sha256: 1848eeb91fa3f477ae3affc0814ee33caece222f43f4dce51a29b515f1aa6035

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

module Navigasmic
  class XmlNavigationBuilder

    @@wrapper_tag = :urlset
    @@group_tag = :urlset
    @@item_tag = :url

    attr_accessor :template, :name, :items, :host

    def initialize(template, name, options = {}, &proc)
      @template, @name, @items = template, name.to_s, []
      @host = options[:host] || "http://#{template.request.host_with_port}"
      @changefreq = options[:changefreq] || 'yearly'
      render(options.delete(:xml), &proc)
    end

    def render(options, &proc)
      buffer = template.capture(self, &proc)

      options ||= {}
      options['xmlns'] ||= 'http://www.sitemaps.org/schemas/sitemap/0.9'
      options['xmlns:xsi'] ||= 'http://www.w3.org/2001/XMLSchema-instance'
      options['xsi:schemaLocation'] ||= 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'

      template.concat(template.content_tag(@@wrapper_tag, buffer, options))
    end

    def group(label = nil, options = {}, &proc)
      raise ArgumentError, "Missing block" unless block_given?

      buffer = template.capture(self, &proc)

      visible = options[:hidden_unless].blank? ? true : options[:hidden_unless].is_a?(Proc) ? options[:hidden_unless].call : options[:hidden_unless]
      visible ? template.concat(template.content_tag(@@group_tag, buffer)) : ''
    end

    def item(label, options = {}, &proc)
      buffer = block_given? ? template.capture(self, &proc) : ''

      item = NavigationItem.new(label, options, template)

      contents = template.content_tag(:loc, @host + template.url_for(item.link))
      contents << template.content_tag(:changefreq, options[:changefreq] || @changefreq)
      contents << template.content_tag(:lastmod, options[:lastmod]) if options[:lastmod]
      contents << template.content_tag(:priority, options[:priority]) if options[:priority]

      item.hidden? ? '' : template.concat(template.content_tag(@@item_tag, contents + buffer, options.delete(:xml)))
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
navigasmic-0.5.0 lib/builders/xml_builder.rb