Class: OEmbed::TemplateResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/oembed_links/template_resolver.rb

Overview

The TemplateResolver class acts as a filesystem path resolver for finding templates, as well as a template renderer. Currently support is enabled for Haml, Erubis, and ERB templates; if you wish to force a particular type of processor for any of the templates used, you may set it by specifing it with template_processor= method. You can specify a base path for resolving template names with the template_root= method.

Defined Under Namespace

Classes: ERBTemplate

Class Method Summary collapse

Class Method Details

.eval_template_for_path(path, url, data, response) ⇒ Object

The evaluated result of the template will be returned.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/oembed_links/template_resolver.rb', line 74

def self.eval_template_for_path(path, url, data, response)
  rendered_response = nil
  if defined?(ApplicationController) && defined?(ActionController)
    if !defined?(ActionController::TestRequest) ||
        !defined?(ActionController::TestResponse)
      require 'action_controller/test_process'          
    end
    @app_c ||= ApplicationController.new
    rendered_response = @app_c.process(ActionController::TestRequest.new,
                                       ActionController::TestResponse.new,
                                       :render_for_file,
                                       path,
                                       200,
                                       nil,
                                       { :data => data,
                                         :url => url,
                                         :response => response }).body
  end
  if rendered_response.nil? && actual_path = resolve_template_path(path)
    contents = File.read(actual_path)
    processor = (@template_processor || File.extname(actual_path)[1..4]).to_s
    has_erubis = defined?(Erubis)
    if processor == "haml"
      require 'haml' unless defined?(Haml)
      rendered_response = Haml::Engine.new(contents).render({ }, :data => data, :url => url, :response => response)
    elsif processor == "erubis" || has_erubis
      require 'erubis' unless has_erubis
      rendered_response = Erubis::Eruby.new(contents).result(:data => data, :url => url, :response => response)
    else
      require 'erb' unless defined?(ERB)
      rendered_response = ERBTemplate.new(url, data, response).evaluate(contents)
    end        
  end
  return rendered_response.chomp
end

.resolve_template_path(path) ⇒ Object

Resolves the template path for the given (potentially relative) path If the given path is found to exist immediately, whether by itself or when combined with the optionally present template_root (OEmbed::TemplateResolver.template_root), it is returned immediately.

If no path is found for the supplied template path, an exception is raised.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/oembed_links/template_resolver.rb', line 40

def self.resolve_template_path(path)
  tmp_path = (@template_root) ? File.join(@template_root, path) : path
  found_path = nil
  if File.exists?(tmp_path)
    found_path = tmp_path
  end
  unless found_path
    raise StandardError.new("File not found: #{path}")
  else
    return found_path        
  end
end

.template_processorObject

Return the current forced template processor, or nil



29
30
31
# File 'lib/oembed_links/template_resolver.rb', line 29

def self.template_processor
  @template_processor 
end

.template_processor=(p) ⇒ Object

Specify the template processor to use for rendering templates; this will be used regardless of file extension



22
23
24
25
26
# File 'lib/oembed_links/template_resolver.rb', line 22

def self.template_processor=(p)
  p = p.to_s if p.is_a? Symbol
  raise "Unsupported processor type" unless ["erb", "haml", "erubis", nil].include?(p)
  @template_processor = p
end

.template_rootObject

Get the current base filesystem path, or nil



16
17
18
# File 'lib/oembed_links/template_resolver.rb', line 16

def self.template_root
  @template_root
end

.template_root=(r) ⇒ Object

Specify the base filesystem path for resolving templates.



11
12
13
# File 'lib/oembed_links/template_resolver.rb', line 11

def self.template_root=(r)
  @template_root = r
end