Subversion Info

Rev
437
Last Checked In
2008-03-28 00:49:20 (2 weeks ago)
Checked in by
deveiant

Parent

Class Index

Quicksearch

Arrow::TemplateFactory

AbstractFactory for templates — defer specification of which templating system to use until load time, and provide timestamp-based caching for template files.

Constants

SVNRev
SVN Revision
SVNId
SVN Id

Attributes

cache[RW]
The Arrow::Cache object used to cache template objects.
loader[RW]
The loader object that the factory uses to load templates
path[RW]
The path to search for templates

Public Class Methods

build_template_loader( config ) click to toggle source

Given an Arrow::Config object (config), attempt to load and instantiate the configured template loader object.

    # File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 45
45:     def self::build_template_loader( config )
46: 
47:         # Resolve the loader name into the Class object by traversing
48:         # constants.
49:         klass = config.templates.loader.
50:             split( /::/ ).
51:             inject( Object ) {|mod, name|
52:                 mod.const_get( name ) or raise ConfigError,
53:                     "No such template loader class #{name} for #{mod.name}"
54:             }
55: 
56:         if klass.respond_to?( :load )
57:             return klass
58:         else
59:             return klass.new( config )
60:         end
61:     end
new( config ) click to toggle source

Create a new TemplateFactory from the given configuration object, which should specify a loader class for templates.

    # File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 70
70:     def initialize( config )
71:         @config = config
72:         @cache = nil
73:         
74:         if config.templates.cache
75:             @cache = Arrow::Cache.new(
76:                 "Template Factory",
77:                 config.templates.cacheConfig,
78:                 &method(:template_expiration_hook) )
79:         end
80: 
81:         @loader = self.class.build_template_loader( config )
82:         @path = config.templates.path
83: 
84:         super()
85:     end

Public Instance Methods

get_template( name ) click to toggle source

Load a template object with the specified name.

     # File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 103
103:     def get_template( name )
104:         self.log.debug "Fetching template '#{name}'"
105: 
106:         if @cache
107:             self.log.debug "Doing cached fetch."
108:             tmpl = @cache.fetch( name, &method(:load_from_file) )
109: 
110:             if tmpl.changed?
111:                 self.log.debug "Template has changed on disk: reloading"
112:                 @cache.invalidate( name )
113:                 tmpl = @cache.fetch( name, &method(:load_from_file) )
114:             end
115: 
116:             return tmpl.dup
117:         else
118:             self.log.debug "Caching disabled. Loading from file."
119:             return self.load_from_file( name )
120:         end
121:     end
load_from_file( name ) click to toggle source

Load a template from its source file (ie., if caching is turned off or if the cached version is either expired or not yet seen)

     # File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 126
126:     def load_from_file( name )
127:         self.log.debug "Loading template #{name} from the filesystem"
128:         return @loader.load( name, @path )
129:     end
template_expiration_hook( key, template ) click to toggle source

Called when a template is expired from the cache

     # File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 133
133:     def template_expiration_hook( key, template )
134:         self.log.debug "Template %s is expiring." % key
135:     end

secsequence

--- SEC00180

seccomment

--- ""

attributes

--- 
- name: cache
  rw: RW
  a_desc: |+
    
    The <a href="Cache.html">Arrow::Cache</a> object used to cache template
    objects.
    
- name: loader
  rw: RW
  a_desc: |+
    
    The loader object that the factory uses to load templates
    
- name: path
  rw: RW
  a_desc: |+
    
    The path to search for templates
    

method_list

--- 
- methods: 
  - visibility: public
    aref: M000368
    name: build_template_loader
    sourcecode: "    <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 45</span>\n\
      45:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-keyword kw\">self</span><span class=\"ruby-operator\">::</span><span class=\"ruby-identifier\">build_template_loader</span>( <span class=\"ruby-identifier\">config</span> )\n\
      46: \n\
      47:         <span class=\"ruby-comment cmt\"># Resolve the loader name into the Class object by traversing</span>\n\
      48:         <span class=\"ruby-comment cmt\"># constants.</span>\n\
      49:         <span class=\"ruby-identifier\">klass</span> = <span class=\"ruby-identifier\">config</span>.<span class=\"ruby-identifier\">templates</span>.<span class=\"ruby-identifier\">loader</span>.\n\
      50:             <span class=\"ruby-identifier\">split</span>( <span class=\"ruby-regexp re\">/::/</span> ).\n\
      51:             <span class=\"ruby-identifier\">inject</span>( <span class=\"ruby-constant\">Object</span> ) {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">mod</span>, <span class=\"ruby-identifier\">name</span><span class=\"ruby-operator\">|</span>\n\
      52:                 <span class=\"ruby-identifier\">mod</span>.<span class=\"ruby-identifier\">const_get</span>( <span class=\"ruby-identifier\">name</span> ) <span class=\"ruby-keyword kw\">or</span> <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">ConfigError</span>,\n\
      53:                     <span class=\"ruby-node\">&quot;No such template loader class #{name} for #{mod.name}&quot;</span>\n\
      54:             }\n\
      55: \n\
      56:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">klass</span>.<span class=\"ruby-identifier\">respond_to?</span>( <span class=\"ruby-identifier\">:load</span> )\n\
      57:             <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">klass</span>\n\
      58:         <span class=\"ruby-keyword kw\">else</span>\n\
      59:             <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">klass</span>.<span class=\"ruby-identifier\">new</span>( <span class=\"ruby-identifier\">config</span> )\n\
      60:         <span class=\"ruby-keyword kw\">end</span>\n\
      61:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Given an <a href="Config.html">Arrow::Config</a> object (<tt>config</tt>),
      attempt to load and instantiate the configured template loader object.
      </p>
    params: ( config )
  - visibility: public
    aref: M000369
    name: new
    sourcecode: "    <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 70</span>\n\
      70:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">initialize</span>( <span class=\"ruby-identifier\">config</span> )\n\
      71:         <span class=\"ruby-ivar\">@config</span> = <span class=\"ruby-identifier\">config</span>\n\
      72:         <span class=\"ruby-ivar\">@cache</span> = <span class=\"ruby-keyword kw\">nil</span>\n\
      73:         \n\
      74:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">config</span>.<span class=\"ruby-identifier\">templates</span>.<span class=\"ruby-identifier\">cache</span>\n\
      75:             <span class=\"ruby-ivar\">@cache</span> = <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">Cache</span>.<span class=\"ruby-identifier\">new</span>(\n\
      76:                 <span class=\"ruby-value str\">&quot;Template Factory&quot;</span>,\n\
      77:                 <span class=\"ruby-identifier\">config</span>.<span class=\"ruby-identifier\">templates</span>.<span class=\"ruby-identifier\">cacheConfig</span>,\n\
      78:                 <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">method</span>(<span class=\"ruby-identifier\">:template_expiration_hook</span>) )\n\
      79:         <span class=\"ruby-keyword kw\">end</span>\n\
      80: \n\
      81:         <span class=\"ruby-ivar\">@loader</span> = <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>.<span class=\"ruby-identifier\">build_template_loader</span>( <span class=\"ruby-identifier\">config</span> )\n\
      82:         <span class=\"ruby-ivar\">@path</span> = <span class=\"ruby-identifier\">config</span>.<span class=\"ruby-identifier\">templates</span>.<span class=\"ruby-identifier\">path</span>\n\
      83: \n\
      84:         <span class=\"ruby-keyword kw\">super</span>()\n\
      85:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Create a <a href="TemplateFactory.html#M000369">new</a> <a
      href="TemplateFactory.html">TemplateFactory</a> from the given
      configuration object, which should specify a loader class for templates.
      </p>
    params: ( config )
  category: Class
  type: Public
- methods: 
  - visibility: public
    aref: M000370
    name: get_template
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 103</span>\n\
      103:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">get_template</span>( <span class=\"ruby-identifier\">name</span> )\n\
      104:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-node\">&quot;Fetching template '#{name}'&quot;</span>\n\
      105: \n\
      106:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@cache</span>\n\
      107:             <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;Doing cached fetch.&quot;</span>\n\
      108:             <span class=\"ruby-identifier\">tmpl</span> = <span class=\"ruby-ivar\">@cache</span>.<span class=\"ruby-identifier\">fetch</span>( <span class=\"ruby-identifier\">name</span>, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">method</span>(<span class=\"ruby-identifier\">:load_from_file</span>) )\n\
      109: \n\
      110:             <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">tmpl</span>.<span class=\"ruby-identifier\">changed?</span>\n\
      111:                 <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;Template has changed on disk: reloading&quot;</span>\n\
      112:                 <span class=\"ruby-ivar\">@cache</span>.<span class=\"ruby-identifier\">invalidate</span>( <span class=\"ruby-identifier\">name</span> )\n\
      113:                 <span class=\"ruby-identifier\">tmpl</span> = <span class=\"ruby-ivar\">@cache</span>.<span class=\"ruby-identifier\">fetch</span>( <span class=\"ruby-identifier\">name</span>, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">method</span>(<span class=\"ruby-identifier\">:load_from_file</span>) )\n\
      114:             <span class=\"ruby-keyword kw\">end</span>\n\
      115: \n\
      116:             <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">tmpl</span>.<span class=\"ruby-identifier\">dup</span>\n\
      117:         <span class=\"ruby-keyword kw\">else</span>\n\
      118:             <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;Caching disabled. Loading from file.&quot;</span>\n\
      119:             <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">load_from_file</span>( <span class=\"ruby-identifier\">name</span> )\n\
      120:         <span class=\"ruby-keyword kw\">end</span>\n\
      121:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Load a template object with the specified name.
      </p>
    params: ( name )
  - visibility: public
    aref: M000371
    name: load_from_file
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 126</span>\n\
      126:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">load_from_file</span>( <span class=\"ruby-identifier\">name</span> )\n\
      127:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-node\">&quot;Loading template #{name} from the filesystem&quot;</span>\n\
      128:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-ivar\">@loader</span>.<span class=\"ruby-identifier\">load</span>( <span class=\"ruby-identifier\">name</span>, <span class=\"ruby-ivar\">@path</span> )\n\
      129:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Load a template from its source file (ie., if caching is turned off or if
      the cached version is either expired or not yet seen)
      </p>
    params: ( name )
  - visibility: public
    aref: M000372
    name: template_expiration_hook
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/templatefactory.rb, line 133</span>\n\
      133:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">template_expiration_hook</span>( <span class=\"ruby-identifier\">key</span>, <span class=\"ruby-identifier\">template</span> )\n\
      134:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;Template %s is expiring.&quot;</span> <span class=\"ruby-operator\">%</span> <span class=\"ruby-identifier\">key</span>\n\
      135:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Called when a template is expired from the cache
      </p>
    params: ( key, template )
  category: Instance
  type: Public

sectitle

--- 

constants

--- 
- name: SVNRev
  desc: |+
    
    SVN Revision
    
  value: "%q$Rev: 437 $"
- name: SVNId
  desc: |+
    
    SVN Id
    
  value: "%q$Id: templatefactory.rb 437 2008-03-28 00:49:20Z deveiant $"

[Validate]

Generated with the Darkfish Rdoc Generator.