class Racket::Utils::Views::TemplateResolver

Class used for resolving template paths.

Constants

FSM

Utility functions for filesystem.

Public Class Methods

call_template_proc(proc, controller) click to toggle source

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.

@param [Proc] proc @param [Racket::Controller] controller @return [String]

# 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) click to toggle source

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

@param [Racket::Controller] controller @return [String]

# 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
new(options) click to toggle source
# 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
service(options = {}) click to toggle source

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

@param [Hash] options @return [Proc]

# 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

Public Instance Methods

get_template_object(path, controller) click to toggle source

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

@param [String] path @param [Racket::Controller] controller @return [Pathname|Proc|nil]

# 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) click to toggle source

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

@param [String] path @param [Proc|String|nil] template @param [Racket::Controller] controller @return [pathname|nil]

# 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