Class: AudioGlue::TemplateLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/audio_glue/template_loader.rb

Overview

Loads .glue templates and caches them.

Examples:

loader = AudioGlue::TemplateLoader.new('/project/audio_templates/')

# load and cache "/project/audio_templates/john/hi.glue"
loader.get('john/hi') # => subclass of AudioGlue::Template

Constant Summary collapse

TEMPLATE_EXT =

Extension of template files.

'glue'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, opts = {}) ⇒ TemplateLoader

Returns a new instance of TemplateLoader.

Parameters:

  • base_path (String)

    path to a directory with templates

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :helper (Module)

    module which provides custom methods for templates.



24
25
26
27
28
# File 'lib/audio_glue/template_loader.rb', line 24

def initialize(base_path, opts = {})
  @base_path = base_path
  @helper    = opts.delete(:helper)
  @cache     = {}
end

Instance Attribute Details

#base_pathObject (readonly)



14
15
16
# File 'lib/audio_glue/template_loader.rb', line 14

def base_path
  @base_path
end

#cacheObject (readonly)



18
19
20
# File 'lib/audio_glue/template_loader.rb', line 18

def cache
  @cache
end

Instance Method Details

#get(template_name) ⇒ Class

Load and cache the template from a .glue template file.

Parameters:

  • template_name (String)

    name of template in base_path directory

Returns:



35
36
37
38
# File 'lib/audio_glue/template_loader.rb', line 35

def get(template_name)
  path = absolute_path(template_name)
  @cache[path] ||= load_template_from_file(path)
end

#reset_cache!Hash

Reset the cache.

Returns:

  • (Hash)

    empty cache



43
44
45
# File 'lib/audio_glue/template_loader.rb', line 43

def reset_cache!
  @cache.clear
end