class Strelka::CMS::PageCatalog

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.

Constants

DEFAULT_GLOB_PATTERN

The default glob pattern to match pages.

Attributes

basedir[R]

The base directory of the catalog

pageglob[R]

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

Public Class Methods

new( basedir=nil, pattern=DEFAULT_GLOB_PATTERN ) click to toggle source

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

# 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

Public Instance Methods

[]( pagename ) click to toggle source

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

# 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
each() click to toggle source

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

# 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
glob() click to toggle source

Return the glob pattern for pages in this catalog.

# File lib/strelka/cms/pagecatalog.rb, line 52
def glob
        return (self.basedir + self.pageglob).to_s
end
inspect() click to toggle source

Return the catalog object as a human-readable string.

# 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 ) click to toggle source

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

# File lib/strelka/cms/pagecatalog.rb, line 82
def matching_pattern( pattern )
        self.class.new( self.basedir, pattern )
end
relative_to( dir ) click to toggle source

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

# 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

Protected Instance Methods

page_enumerator() click to toggle source

Return an Enumerator that will yield a Strelka::CMS::Page object for each page matching the pageglob.

# File lib/strelka/cms/pagecatalog.rb, line 127
def page_enumerator
        Enumerator.new do |yielder|
                self.page_path_enumerator.each do |path|
                        self.log.debug "Loading %s..." % [ path ]
                        page = Strelka::CMS::Page.load( path.to_s, self )
                        yielder.yield( page )
                end
        end
end
page_path_enumerator() click to toggle source

Return an Enumerator that will yield a Pathname for each page matching the pageglob

# File lib/strelka/cms/pagecatalog.rb, line 117
def page_path_enumerator
        self.log.debug "Fetching an enumerator for files matching: %s" % [ self.glob ]
        paths = Pathname.glob( self.glob )
        self.log.debug "  fully-qualifying %d paths: %p" % [ paths.length, paths ]
        return paths.collect {|fn| self.basedir + fn }.each
end