Class: Racket::Utils::Views::TemplateLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/utils/views/template_locator.rb

Overview

Class used for locating templates. This class uses the TemplateResolver class internally for getting the template for the first time and caches the result so that subsequent Calls will not need to resolve the template again.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TemplateLocator

Returns a new instance of TemplateLocator.



41
42
43
# File 'lib/racket/utils/views/template_locator.rb', line 41

def initialize(options)
  options.each_pair { |key, value| instance_variable_set("@#{key}".to_sym, value) }
end

Class Method Details

.get_template(cache, resolver, controller) ⇒ String|nil

Tries to locate a template matching the controllers action in the file system and returns the path if a matching file is found. If no matching file is found, nil is returned. The result is cached, meaning that the filesystem lookup for a specific path will only happen once.

Parameters:

Returns:

  • (String|nil)


72
73
74
75
76
77
78
79
# File 'lib/racket/utils/views/template_locator.rb', line 72

def self.get_template(cache, resolver, controller)
  path = TemplateResolver.get_template_path(controller)
  unless cache.key?(path)
    template = resolver.get_template_object(path, controller)
    cache.store(path, template)
  end
  resolver.resolve_template(path, cache.load(path), controller)
end

.service(_options = {}) ⇒ Proc

Returns a service proc that can be used by the registry.

Parameters:

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

    (unused)

Returns:

  • (Proc)


30
31
32
33
34
35
36
37
38
39
# File 'lib/racket/utils/views/template_locator.rb', line 30

def self.service(_options = {})
  lambda do |reg|
    new(
      layout_cache: reg.layout_cache,
      layout_resolver: reg.layout_resolver,
      view_cache: reg.view_cache,
      view_resolver: reg.view_resolver
    )
  end
end

Instance Method Details

#get_layout(controller) ⇒ String|nil

Returns the layout associated with the current request. On the first request to any action the result is cached, meaning that the layout only needs to be looked up once.

Parameters:

Returns:

  • (String|nil)


50
51
52
# File 'lib/racket/utils/views/template_locator.rb', line 50

def get_layout(controller)
  self.class.get_template(@layout_cache, @layout_resolver, controller)
end

#get_view(controller) ⇒ String|nil

Returns the view associated with the current request. On the first request to any action the result is cached, meaning that the view only needs to be looked up once.

Parameters:

Returns:

  • (String|nil)


59
60
61
# File 'lib/racket/utils/views/template_locator.rb', line 59

def get_view(controller)
  self.class.get_template(@view_cache, @view_resolver, controller)
end