Class: Arrow::TemplateFactory
- Defined in:
- lib/arrow/templatefactory.rb
Overview
The TemplateFactory class, which is responsible for interpreting the 'templates' section of the configuration, and providing template-loading and -caching according to that configuration,
Authors
- Michael Granger [email protected]
Please see the file LICENSE in the top-level directory for licensing details.
Instance Attribute Summary collapse
-
#cache ⇒ Object
The Arrow::Cache object used to cache template objects.
-
#loader ⇒ Object
The loader object that the factory uses to load templates.
-
#path ⇒ Object
The path to search for templates.
Class Method Summary collapse
-
.build_template_loader(config) ⇒ Object
Given an Arrow::Config object (+config+), attempt to load and instantiate the configured template loader object.
Instance Method Summary collapse
-
#get_template(name) ⇒ Object
Load a template object with the specified name.
-
#initialize(config) ⇒ TemplateFactory
constructor
Create a new TemplateFactory from the given configuration object, which should specify a loader class for templates.
-
#load_from_file(name) ⇒ Object
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).
-
#template_expiration_hook(key, template) ⇒ Object
Called when a template is expired from the cache.
Methods inherited from Object
deprecate_class_method, deprecate_method, inherited
Constructor Details
#initialize(config) ⇒ TemplateFactory
Create a new TemplateFactory from the given configuration object, which should specify a loader class for templates.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/arrow/templatefactory.rb', line 56 def initialize( config ) @config = config @cache = nil if config.templates.cache @cache = Arrow::Cache.new( "Template Factory", config.templates.cacheConfig, &method(:template_expiration_hook) ) end @loader = self.class.build_template_loader( config ) @path = config.templates.path super() end |
Instance Attribute Details
#cache ⇒ Object
The Arrow::Cache object used to cache template objects.
79 80 81 |
# File 'lib/arrow/templatefactory.rb', line 79 def cache @cache end |
#loader ⇒ Object
The loader object that the factory uses to load templates
82 83 84 |
# File 'lib/arrow/templatefactory.rb', line 82 def loader @loader end |
#path ⇒ Object
The path to search for templates
85 86 87 |
# File 'lib/arrow/templatefactory.rb', line 85 def path @path end |
Class Method Details
.build_template_loader(config) ⇒ Object
Given an Arrow::Config object (+config+), attempt to load and instantiate the configured template loader object.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/arrow/templatefactory.rb', line 28 def self::build_template_loader( config ) # Resolve the loader name into the Class object by traversing # constants. klass = config.templates.loader. split( /::/ ). inject( Object ) {|mod, name| mod.const_get( name ) or raise ConfigError, "No such template loader class #{name} for #{mod.name}" } if klass.respond_to?( :load, false ) Arrow::Logger[ self ].debug "Loader (%s) class responds to ::load; using it directly: %p" % [ klass.name, klass.method(:load) ] return klass else Arrow::Logger[ self ].debug "Loader (%s) expects instantiation." % [ klass.name ] return klass.new( config ) end end |
Instance Method Details
#get_template(name) ⇒ Object
Load a template object with the specified name.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/arrow/templatefactory.rb', line 89 def get_template( name ) self.log.debug "Fetching template '#{name}'" if @cache self.log.debug "Doing cached fetch." tmpl = @cache.fetch( name, &method(:load_from_file) ) if tmpl.changed? self.log.debug "Template has changed on disk: reloading" @cache.invalidate( name ) tmpl = @cache.fetch( name, &method(:load_from_file) ) end return tmpl.dup else self.log.debug "Caching disabled. Loading from file." return self.load_from_file( name ) end end |
#load_from_file(name) ⇒ Object
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)
112 113 114 115 |
# File 'lib/arrow/templatefactory.rb', line 112 def load_from_file( name ) self.log.debug "Loading template #{name} from the filesystem" return @loader.load( name, @path ) end |
#template_expiration_hook(key, template) ⇒ Object
Called when a template is expired from the cache
119 120 121 |
# File 'lib/arrow/templatefactory.rb', line 119 def template_expiration_hook( key, template ) self.log.debug "Template %s is expiring." % key end |