Class: Pakada::Render

Inherits:
Object
  • Object
show all
Includes:
Module
Defined in:
lib/pakada/render/version.rb,
lib/pakada/render/renderer.rb,
lib/pakada/render/rendering.rb,
lib/pakada/render/controller.rb,
lib/pakada/render/rendering_context.rb,
lib/pakada/render.rb

Defined Under Namespace

Modules: Controller, Rendering Classes: Renderer, RenderingContext

Constant Summary collapse

VERSION =
"0.3.2"
LAYOUT_HEADER =
"X-Pakada-Render-Layout"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRender

Returns a new instance of Render.



19
20
21
# File 'lib/pakada/render.rb', line 19

def initialize
  Pakada.safety(Pakada::Render::RenderingContext).send :include, Hooked
end

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



17
18
19
# File 'lib/pakada/render.rb', line 17

def load_path
  @load_path
end

#template_mapObject (readonly)

Returns the value of attribute template_map.



17
18
19
# File 'lib/pakada/render.rb', line 17

def template_map
  @template_map
end

Instance Method Details

#bootObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/pakada/render.rb', line 62

def boot
  rendering = Pakada.safety(Pakada::Render::Rendering)
  Pakada.safety(Pakada::Render::RenderingContext).send :include, rendering
  Pakada.modules.each_value {|mod|
    mod.extend rendering
  }
  
  @load_path = build_load_path
  @template_map = build_template_map(load_path)
end

#build_load_pathObject



73
74
75
76
77
78
79
80
81
# File 'lib/pakada/render.rb', line 73

def build_load_path
  Hike::Trail.new("/").tap {|load_path|
    load_path.append_extensions *Tilt.mappings.keys
    load_path.append_path Pakada.path.join("templates")
    Pakada.modules.each_value {|mod|
      load_path.append_path mod.path.join("templates")
    }
  }
end

#build_template_map(load_path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pakada/render.rb', line 83

def build_template_map(load_path)
  load_path.paths.map {|path|
    prefix = %r|#{Regexp.quote path.to_s}/|
    Dir.glob("#{path}/**/*").map {|filepath|
      next if File.directory? filepath
      filepath.gsub(prefix, "").rpartition(".").first
    }
  }.flatten.compact.uniq.inject({}) {|map, name|
    map[name.to_sym] = Tilt.new(load_path.find(name))
    map
  }
end

#hooksObject



23
24
25
26
27
28
# File 'lib/pakada/render.rb', line 23

def hooks
  if Pakada[:dispatch]
    Pakada[:dispatch].after :create_controller, method(:make_renderable)
    Pakada[:dispatch].after :request, method(:render_layout)
  end
end

#make_renderable(cls) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/pakada/render.rb', line 30

def make_renderable(cls)
  cls.send :include, Pakada.safety(Pakada::Render::Controller)
  unless cls.options.key? :render
    cls.options[:render] = true
  end
  unless cls.options.key? :layout
    cls.options[:layout] = true
  end
  cls.instance_after :process, method(:render_controller)
end

#render_controller(controller) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/pakada/render.rb', line 41

def render_controller(controller)
  unless controller.options[:layout]
    controller.response.headers[LAYOUT_HEADER] = "no"
  end
  
  if controller.options[:render]
    controller.response.write controller.render
  end
end

#render_layout(response) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/pakada/render.rb', line 51

def render_layout(response)
  if !response[1].key?(LAYOUT_HEADER) || response[1][LAYOUT_HEADER] == "yes"
    response[1].delete "Content-Length"
    
    content = []
    response[2].each {|chunk| content << chunk }
    response[2] = [render!(:layout, :content => content,
                                    :@response => response)]
  end
end