Module: Pancake::Mixins::Render::ClassMethods

Defined in:
lib/pancake/mixins/render.rb

Instance Method Summary collapse

Instance Method Details

#_find_template(name, opts = {}) ⇒ Object



44
45
46
# File 'lib/pancake/mixins/render.rb', line 44

def _find_template(name, opts = {})
  template(name, opts)
end

#_find_view_context_class_for(template) ⇒ Object



53
54
55
56
57
58
# File 'lib/pancake/mixins/render.rb', line 53

def _find_view_context_class_for(template)
  _view_context_cache[template] ||= begin
                                      self::ViewContext
                                    end
  _view_context_cache[template]
end

#_renderer_and_view_context_class_for(tplate) ⇒ Object



60
61
62
# File 'lib/pancake/mixins/render.rb', line 60

def _renderer_and_view_context_class_for(tplate)
  [template(tplate), _find_view_context_class_for(tplate)]
end

#_template_cacheObject



29
30
31
# File 'lib/pancake/mixins/render.rb', line 29

def _template_cache
  @_template_cache ||= {}
end

#_template_name_for(name, opts) ⇒ Object



64
65
66
# File 'lib/pancake/mixins/render.rb', line 64

def _template_name_for(name, opts)
  "#{name}"
end

#_template_path_name(opts = {}) ⇒ Object

Allows you to set path label for the templates for this class

Examples:

MyClass.push_paths(:my_templates, "somewhere", "**/*")

MyClass._template_path_name #=> :my_templates


40
41
42
# File 'lib/pancake/mixins/render.rb', line 40

def _template_path_name(opts = {})
  opts[:template_path_name] || :views
end

#_view_context_cacheObject



48
49
50
51
# File 'lib/pancake/mixins/render.rb', line 48

def _view_context_cache
  @_view_context_cache ||= {}
  @_view_context_cache
end

#base_template_nameObject



108
109
110
# File 'lib/pancake/mixins/render.rb', line 108

def base_template_name
  :base
end

#template(name, opts = {}) ⇒ Object



68
69
70
71
72
73
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
# File 'lib/pancake/mixins/render.rb', line 68

def template(name, opts = {})
  case name
  when Template
    name
  when String, Symbol
    template_names = case __template = _template_name_for(name, opts)
    when String, Symbol
      [__template]
    when Array
      __template
    when Proc
      [__template.call(opts)].flatten
    else
      nil
    end

    renderer = _template_cache[template_names]
    return renderer if renderer

    unique_paths = unique_paths_for(_template_path_name(opts), :invert => true)

    renderer_path = nil
    template_name = nil

    template_names.detect do |tn|
      unique_paths.detect do |path|
        if path.last =~ %r[^\/?(#{tn})\.\w+$]
          template_name = tn
          renderer_path = path.join
        end
      end
    end

    raise TemplateNotFound unless renderer_path
    _template_cache[template_names] = Template.new(template_name, self, renderer_path)
  else
    nil
  end
end