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.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRender

Returns a new instance of Render.



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

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

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



15
16
17
# File 'lib/pakada/render.rb', line 15

def load_path
  @load_path
end

#template_mapObject (readonly)

Returns the value of attribute template_map.



15
16
17
# File 'lib/pakada/render.rb', line 15

def template_map
  @template_map
end

Instance Method Details

#bootObject



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

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

#build_load_pathObject



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

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



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

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



21
22
23
24
25
26
# File 'lib/pakada/render.rb', line 21

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

#make_renderable(cls) ⇒ Object



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

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



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

def render_controller(controller)
  unless controller.options[:layout]
    controller.request.env["pakada.render.layout"] = false
  end
  
  if controller.options[:render]
    controller.response.write controller.render
  end
end

#render_layout(request, env) ⇒ Object



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

def render_layout(request, env)
  key = "pakada.render.layout"
  request.call(env).tap {|response|
    env[key] = true unless env.key? key
    if env[key]
      response[1].delete "Content-Length"
      content = []
      response[2].each {|chunk| content << chunk }
      response[2] = [render!(:layout, :content => content)]
    end
  }
end