Class: Strelka::CMS::PageCatalog

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
Enumerable
Defined in:
lib/strelka/cms/pagecatalog.rb

Overview

A catalog of Strelka::CMS::Page objects. It provides a collection interface over a group of pages, suitable for searching for, or iterating over pages matching one or more criteria.

Constant Summary collapse

DEFAULT_GLOB_PATTERN =

The default glob pattern to match pages.

'**/*.page'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir = nil, pattern = DEFAULT_GLOB_PATTERN) ⇒ PageCatalog

Create a new PageCatalog that will find and read pages from the configured directory.



31
32
33
34
35
36
37
# File 'lib/strelka/cms/pagecatalog.rb', line 31

def initialize( basedir=nil, pattern=DEFAULT_GLOB_PATTERN )
	basedir ||= Pathname.pwd
	pattern.slice!( 0, 1 ) if pattern.start_with?( '/' )
	self.log.debug "New catalog for pages matching: %s in: %s" % [ pattern, basedir ]
	@basedir  = Pathname( basedir )
	@pageglob = pattern
end

Instance Attribute Details

#basedirObject (readonly)

The base directory of the catalog



45
46
47
# File 'lib/strelka/cms/pagecatalog.rb', line 45

def basedir
  @basedir
end

#pageglobObject (readonly)

The glob pattern that will match a collection of one or more pages



48
49
50
# File 'lib/strelka/cms/pagecatalog.rb', line 48

def pageglob
  @pageglob
end

Instance Method Details

#[](pagename) ⇒ Object

Fetch a Strelka::CMS::Page from the catalog that matches pagename.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/strelka/cms/pagecatalog.rb', line 99

def []( pagename )
	pagename = pagename + '.page' unless pagename.end_with?( '.page' )
	paths = self.page_path_enumerator
	pagepath = paths.find do |path|
		self.log.debug "Testing path %s (relative to %s) for %s: %s" %
			[ path, self.basedir, pagename, path.relative_path_from(self.basedir) ]
		path.relative_path_from(self.basedir).to_s == pagename.to_s
	end or return nil

	return Strelka::CMS::Page.load( pagepath, self )
end

#eachObject

Enumerable interface – if called with a block, load and yield each page matching the #pageglob. If called without a block, return an Enumerator.



59
60
61
62
63
64
65
66
67
# File 'lib/strelka/cms/pagecatalog.rb', line 59

def each
	iter = self.page_enumerator
	if block_given?
		block = Proc.new
		iter.each( &block )
	else
		return iter
	end
end

#globObject

Return the glob pattern for pages in this catalog.



52
53
54
# File 'lib/strelka/cms/pagecatalog.rb', line 52

def glob
	return (self.basedir + self.pageglob).to_s
end

#inspectObject

Return the catalog object as a human-readable string.



88
89
90
91
92
93
94
95
# File 'lib/strelka/cms/pagecatalog.rb', line 88

def inspect
	"#<%s:0x%0x %s, %d documents>" % [
		self.class.name,
		self.object_id / 2,
		self.glob,
		self.page_path_enumerator.count,
	]
end

#matching_pattern(pattern) ⇒ Object

Return a clone of the directory that will use the specified pattern to find pages instead of the original.



82
83
84
# File 'lib/strelka/cms/pagecatalog.rb', line 82

def matching_pattern( pattern )
	self.class.new( self.basedir, pattern )
end

#relative_to(dir) ⇒ Object

Return a clone of the directory that will limit the pages in the catalog to those under the given dir.



72
73
74
75
76
77
# File 'lib/strelka/cms/pagecatalog.rb', line 72

def relative_to( dir )
	dir = dir.to_s
	dir.slice!( 0, 1 ) if dir.start_with?( '/' )
	self.log.debug "Build new catalog for %p relative to %s" % [ dir, self.basedir ]
	self.class.new( self.basedir + dir, self.pageglob )
end