Class: Sitemapper::Map
- Inherits:
-
Object
- Object
- Sitemapper::Map
- Defined in:
- lib/sitemapper/map.rb
Constant Summary collapse
- SCHEMA =
:nodoc:
'http://www.sitemaps.org/schemas/sitemap/0.9'- INDENT =
:nodoc:
-1 #:nodoc:
Class Method Summary collapse
-
.site_root ⇒ Object
Returns the site root (previously defined with site_root=).
-
.site_root=(root) ⇒ Object
Set the site root for the generated URLs.
Instance Method Summary collapse
-
#initialize(file) ⇒ Map
constructor
Initialize a map.
-
#map_path(path, opts = {}) ⇒ Object
Map the given path.
-
#map_url(loc, opts = {}) ⇒ Object
Map the given localization.
-
#map_urls {|_self| ... } ⇒ Object
Map multiple URLs and write once (at the end).
-
#unmap_path(loc) ⇒ Object
Unmap the given path.
-
#unmap_url(loc) ⇒ Object
Unmap the given localization (loc).
Constructor Details
#initialize(file) ⇒ Map
Initialize a map
- file is the path to the (new or existent) sitemap.xml
25 26 27 28 29 30 31 |
# File 'lib/sitemapper/map.rb', line 25 def initialize(file) @builder = REXML::Document.new(File.exists?(file)? File.read(file) : nil) @locker = Mutex.new @file = file @write = true initialize_map end |
Class Method Details
.site_root ⇒ Object
Returns the site root (previously defined with site_root=)
11 12 13 |
# File 'lib/sitemapper/map.rb', line 11 def self.site_root @@site_root ||= 'http://www.example.com/' end |
.site_root=(root) ⇒ Object
Set the site root for the generated URLs
- root is the root, (ex.: http://www.example.com)
18 19 20 |
# File 'lib/sitemapper/map.rb', line 18 def self.site_root=(root) @@site_root = root end |
Instance Method Details
#map_path(path, opts = {}) ⇒ Object
Map the given path
path is the path to be mapped opts is a hash containing options for this url (see map_url)
55 56 57 |
# File 'lib/sitemapper/map.rb', line 55 def map_path(path, opts={}) map_url(URI.join(Map.site_root, path), opts) end |
#map_url(loc, opts = {}) ⇒ Object
Map the given localization
loc is the URL to me mapped opts is a hash with the following parameters:
- url or path is the complete URL or path (ex.: /articles/rails-doesnt-scale) [required]
- lastmod a date object to the last modification [optional]
- changefreq the frequency of change (:daily, :monthly, :yearly) [optional]
- priority the priority of the URL (0..1) [optional]
44 45 46 47 48 49 |
# File 'lib/sitemapper/map.rb', line 44 def map_url(loc, opts={}) @locker.synchronize do set_url_node(loc, opts) write_file end end |
#map_urls {|_self| ... } ⇒ Object
Map multiple URLs and write once (at the end)
Usage:
map.map_urls do |map|
map.map_url('http://www.example.com/about')
map.unmap_url('http://www.example.com/contact')
end
68 69 70 71 72 73 74 |
# File 'lib/sitemapper/map.rb', line 68 def map_urls @write = false yield(self) @write = true @locker.synchronize { write_file } end |
#unmap_path(loc) ⇒ Object
Unmap the given path
- loc is the Path t be unmaped
89 90 91 |
# File 'lib/sitemapper/map.rb', line 89 def unmap_path(loc) unmap_url(URI.join(Map.site_root, loc)) end |
#unmap_url(loc) ⇒ Object
Unmap the given localization (loc)
- loc is the URL to be unmaped
79 80 81 82 83 84 |
# File 'lib/sitemapper/map.rb', line 79 def unmap_url(loc) @locker.synchronize do unset_url_node(loc) write_file end end |