Module: Cadenza::Context::Loaders

Included in:
Cadenza::Context
Defined in:
lib/cadenza/context/loaders.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loadersArray (readonly)

Returns the list of loaders.

Returns:

  • (Array)

    the list of loaders


22
23
24
# File 'lib/cadenza/context/loaders.rb', line 22

def loaders
   @loaders ||= []
end

#whiny_template_loadingBoolean

Returns true if a TemplateNotFoundError should still be raised if not calling the bang form of #load_source or #load_template.

Returns:


12
13
14
# File 'lib/cadenza/context/loaders.rb', line 12

def whiny_template_loading
   @whiny_template_loading ||= false
end

Instance Method Details

#add_load_path(path) ⇒ Loader

constructs a FilesystemLoader with the string given as its path and adds the loader to the end of the loader list.

Parameters:

  • path (String)

    to use for loader

Returns:

  • (Loader)

    the loader that was created


31
32
33
34
35
# File 'lib/cadenza/context/loaders.rb', line 31

def add_load_path(path)
  loader = FilesystemLoader.new(path)
  add_loader(loader)
  loader
end

#add_loader(loader) ⇒ Object

adds the given loader to the end of the loader list.

Parameters:

  • loader (Loader)

    the loader to add

Returns:

  • nil


41
42
43
44
# File 'lib/cadenza/context/loaders.rb', line 41

def add_loader(loader)
   loaders.push loader
   nil
end

#clear_loadersObject

removes all loaders from the context

Returns:

  • nil


48
49
50
51
# File 'lib/cadenza/context/loaders.rb', line 48

def clear_loaders
   loaders.reject! { true }
   nil
end

#load_source(template_name) ⇒ String

loads and returns the given template but does not parse it

Parameters:

  • template_name (String)

    the name of the template to load

Returns:

  • (String)

    the template text or nil if the template could not be loaded

Raises:


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cadenza/context/loaders.rb', line 59

def load_source(template_name)
   source = nil

   loaders.each do |loader|
      source = loader.load_source(template_name)
      break if source
   end

   if source.nil? and whiny_template_loading
      raise TemplateNotFoundError.new(template_name)
   else
      return source
   end
end

#load_source!(template_name) ⇒ String

loads and returns the given template but does not parse it

Parameters:

  • template_name (String)

    the name of the template to load

Returns:

  • (String)

    the template text

Raises:


79
80
81
# File 'lib/cadenza/context/loaders.rb', line 79

def load_source!(template_name)
   load_source(template_name) || raise(TemplateNotFoundError.new(template_name))
end

#load_template(template_name) ⇒ DocumentNode

loads, parses and returns the given template

Parameters:

  • template_name (String)

    the name of the template to load

Returns:

  • (DocumentNode)

    the root of the parsed document or nil if the template could not be loaded.

Raises:


90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cadenza/context/loaders.rb', line 90

def load_template(template_name)
   template = nil

   loaders.each do |loader|
      template = loader.load_template(template_name)
      break if template
   end
   
   if template.nil? and whiny_template_loading
      raise TemplateNotFoundError.new(template_name)
   else
      return template
   end
end

#load_template!(template_name) ⇒ DocumentNode

loads, parses and returns the given template

Parameters:

  • template_name (String)

    the name of the template ot load

Returns:

Raises:


110
111
112
# File 'lib/cadenza/context/loaders.rb', line 110

def load_template!(template_name)
   load_template(template_name) || raise(TemplateNotFoundError.new(template_name))
end