Class: Racket::Utils::Views::TemplateResolver

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

Overview

Class used for resolving template paths.

Constant Summary collapse

FSM =

Alias for Racket::Utils::FileSystem module.

Racket::Utils::FileSystem

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TemplateResolver

Returns a new instance of TemplateResolver.



43
44
45
# File 'lib/racket/utils/views/template_resolver.rb', line 43

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

Class Method Details

.call_template_proc(proc, controller) ⇒ String

Calls a template proc. Depending on how many parameters the template proc takes, different types of information will be passed to the proc. If the proc takes zero parameters, no information will be passed. If the proc takes one parameter, it will contain the current action. If the proc takes two parameters, they will contain the current action and the current

params.

If the proc takes three parameters, they will contain the current action, the current params and the current request.

Parameters:

Returns:

  • (String)


93
94
95
96
97
# File 'lib/racket/utils/views/template_resolver.rb', line 93

def self.call_template_proc(proc, controller)
  racket = controller.racket
  proc_args = [racket.action, racket.params, controller.request].slice(0...proc.arity)
  proc.call(*proc_args).to_s
end

.get_template_path(controller) ⇒ String

Returns the “url path” that should be used when searching for templates.

Parameters:

Returns:

  • (String)


103
104
105
106
107
108
# File 'lib/racket/utils/views/template_resolver.rb', line 103

def self.get_template_path(controller)
  template_path =
    [controller.class.get_route, controller.racket.action].join('/')
  template_path = template_path[1..-1] if template_path.start_with?('//')
  template_path
end

.service(options = {}) ⇒ Proc

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

Parameters:

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

Returns:

  • (Proc)


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

def self.service(options = {})
  type = options[:type]
  lambda do |reg|
    new(
      base_dir: reg.application_settings.send("#{type}_dir"),
      logger: reg.application_logger,
      type: type,
      utils: reg.utils
    )
  end
end

Instance Method Details

#get_template_object(path, controller) ⇒ Pathname|Proc|nil

Returns the template object representing the specified path/controller combination.

Parameters:

Returns:

  • (Pathname|Proc|nil)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/racket/utils/views/template_resolver.rb', line 52

def get_template_object(path, controller)
  default_template = controller.settings.fetch("default_#{@type}".to_sym)
  template =
    FSM.resolve_path_with_default(
      FSM.fs_path(@base_dir, path), default_template
    )
  @logger.inform_dev(
    "Using #{@type} #{template.inspect} for #{controller.class}." \
    "#{controller.racket.action}."
  )
  template
end

#resolve_template(path, template, controller) ⇒ pathname|nil

Returns the “resolved” path for the given parameters. This is either a pathname or nil.

Parameters:

Returns:

  • (pathname|nil)


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

def resolve_template(path, template, controller)
  return template unless template.is_a?(Proc)
  FSM.resolve_path(
    FSM.fs_path(
      FSM.fs_path(@base_dir, path).dirname,
      self.class.call_template_proc(template, controller)
    )
  )
end