Class: Arrow::TemplateFactory

Inherits:
Object
  • Object
show all
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

Please see the file LICENSE in the top-level directory for licensing details.

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Methods inherited from Object

deprecate_class_method, deprecate_method, inherited

Methods included from Loggable

#log

Constructor Details

- (TemplateFactory) initialize(config)

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

- (Object) cache

The Arrow::Cache object used to cache template objects.



79
80
81
# File 'lib/arrow/templatefactory.rb', line 79

def cache
  @cache
end

- (Object) loader

The loader object that the factory uses to load templates



82
83
84
# File 'lib/arrow/templatefactory.rb', line 82

def loader
  @loader
end

- (Object) path

The path to search for templates



85
86
87
# File 'lib/arrow/templatefactory.rb', line 85

def path
  @path
end

Class Method Details

+ (Object) build_template_loader(config)

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

- (Object) get_template(name)

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

- (Object) load_from_file(name)

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

- (Object) template_expiration_hook(key, template)

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