Class: Verb

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Verb

Returns a new instance of Verb.



4
5
6
# File 'lib/verb.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/verb.rb', line 8

def call(env)
  base_path = File.join(Dir.pwd, env["PATH_INFO"].sub(/\/$/, ""))
  exact_path = base_path + ".rhtml"
  index_path = File.join(base_path, "index.rhtml")

  if File.exists?(exact_path)
    render(exact_path, env)
  elsif File.exists?(index_path)
    render(index_path, env)
  else
    @app.call(env)
  end
end

#layout(name = "layout") ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/verb.rb', line 38

def layout(name = "layout")
  layout_path = File.join(File.dirname(@render_path), name + ".rhtml")
  
  if File.exists?(layout_path)
    ERB.new(File.open(layout_path) { |f| f.read }, nil, nil, "@erb_output").result(binding)
  else
    yield
  end
end

#render(path, env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/verb.rb', line 22

def render(path, env)
  @erb_output = ""
  @render_path = path
  
  @request = Rack::Request.new(env)
  @response = Rack::Response.new
  
  @response["Content-Type"] = "text/html"
  
  ERB.new(File.open(path) { |f| f.read }, nil, nil, "@erb_output").result(binding)

  @response.write(@erb_output)

  @response.finish
end