georgi-kontrol 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -158,6 +158,10 @@ We create a Markdown file name `index.md`:
158
158
  We have now a simple page, which should be rendered as response. We
159
159
  create a simple app in a file `git_app.ru`:
160
160
 
161
+ require 'kontrol'
162
+ require 'bluecloth'
163
+ require 'git_store'
164
+
161
165
  class GitApp < Kontrol::Application
162
166
 
163
167
  def initialize(path)
@@ -172,6 +176,9 @@ create a simple app in a file `git_app.ru`:
172
176
  end
173
177
  end
174
178
 
179
+ run GitApp.new
180
+
181
+
175
182
  Add all the page to your git repository:
176
183
 
177
184
  git init
@@ -1,5 +1,6 @@
1
1
  require 'kontrol'
2
2
  require 'bluecloth'
3
+ require 'git_store'
3
4
 
4
5
  class GitApp < Kontrol::Application
5
6
 
@@ -10,7 +11,7 @@ class GitApp < Kontrol::Application
10
11
 
11
12
  map do
12
13
  page '/(.*)' do |name|
13
- text BlueCloth.new(@store[name + '.md']).to_html
14
+ text BlueCloth.new(@store[name]).to_html
14
15
  end
15
16
  end
16
17
  end
@@ -20,25 +20,29 @@ module Kontrol
20
20
  end
21
21
 
22
22
  def load_template(file)
23
- ERB.new(File.read("#{path}/templates/#{file}"))
23
+ ERB.new(File.read("#{self.path}/templates/#{file}"))
24
24
  end
25
25
 
26
26
  # Render template with given variables.
27
27
  def render_template(file, vars)
28
- Template.render(load_template(file), self, file, vars)
28
+ template = load_template(file) or raise "not found: #{path}"
29
+ Template.render(template, self, "#{self.path}/templates/#{file}", vars)
29
30
  end
30
31
 
31
32
  # Render named template and insert into layout with given variables.
32
- def render(name, vars = {})
33
- content = render_template(name, vars)
34
- layout = vars[:layout] || "layout.rhtml"
33
+ def render(name, options = {})
34
+ options = options.merge(:request => request, :params => params)
35
+ content = render_template(name, options)
36
+ layout = options.delete(:layout)
35
37
 
36
38
  if name[0, 1] == '_'
37
39
  return content
40
+
38
41
  elsif layout == false
39
42
  response.body = content
40
43
  else
41
- response.body = render_template(layout, vars.merge(:content => content))
44
+ options.merge!(:content => content)
45
+ response.body = render_template(layout || "layout.rhtml", options)
42
46
  end
43
47
 
44
48
  response['Content-Length'] = response.body.size.to_s
@@ -77,6 +81,10 @@ module Kontrol
77
81
  def get? ; request.get? end
78
82
  def put? ; request.put? end
79
83
  def delete? ; request.delete? end
84
+ def post ; request.post? and yield end
85
+ def get ; request.get? and yield end
86
+ def put ; request.put? and yield end
87
+ def delete ; request.delete? and yield end
80
88
 
81
89
  def text(s)
82
90
  response.body = s
@@ -121,9 +129,21 @@ module Kontrol
121
129
  "#<#{self.class.name} @path=#{path}>"
122
130
  end
123
131
 
132
+ def respond_to?(name)
133
+ if match = name.to_s.match(/^(.*)_path$/)
134
+ router.__find__(match[1])
135
+ else
136
+ super
137
+ end
138
+ end
139
+
124
140
  def method_missing(name, *args, &block)
125
141
  if match = name.to_s.match(/^(.*)_path$/)
126
- router.__find__(match[1]).generate(*args)
142
+ if route = router.__find__(match[1])
143
+ route.generate(*args)
144
+ else
145
+ super
146
+ end
127
147
  else
128
148
  super
129
149
  end
@@ -20,7 +20,9 @@ module Kontrol
20
20
  end
21
21
 
22
22
  def generate(*args)
23
- @format % args
23
+ @format % args.map { |arg|
24
+ arg.respond_to?(:to_param) ? arg.to_param : arg.to_s
25
+ }
24
26
  end
25
27
 
26
28
  end
@@ -13,7 +13,7 @@ module Kontrol
13
13
  instance_variable_set "@#{k}", v
14
14
  end
15
15
  end
16
-
16
+
17
17
  def __binding__
18
18
  binding
19
19
  end
@@ -7,6 +7,14 @@ describe Kontrol::Application do
7
7
  @class = Class.new(Kontrol::Application)
8
8
  @app = @class.new
9
9
  @request = Rack::MockRequest.new(@app)
10
+
11
+ def @app.load_template(file)
12
+ if file == "layout.rhtml"
13
+ ERB.new '<html><%= @content %></html>'
14
+ else
15
+ ERB.new '<p><%= @body %></p>'
16
+ end
17
+ end
10
18
  end
11
19
 
12
20
  def get(*args)
@@ -90,15 +98,17 @@ describe Kontrol::Application do
90
98
  get('/')['Content-Length'].should == '5'
91
99
  end
92
100
 
93
- it "should render templates" do
94
- def @app.load_template(file)
95
- if file == "layout.rhtml"
96
- ERB.new '<html><%= @content %></html>'
97
- else
98
- ERB.new '<p><%= @body %></p>'
101
+ it "should render no layout" do
102
+ map do
103
+ index '/' do
104
+ render 'index.rhtml', :body => 'BODY', :layout => false
99
105
  end
100
106
  end
101
107
 
108
+ get('/').body.should == '<p>BODY</p>'
109
+ end
110
+
111
+ it "should render templates" do
102
112
  map do
103
113
  index '/' do
104
114
  render 'index.rhtml', :body => 'BODY'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: georgi-kontrol
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Georgi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-13 00:00:00 -07:00
12
+ date: 2009-04-08 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -65,6 +65,6 @@ rubyforge_project:
65
65
  rubygems_version: 1.2.0
66
66
  signing_key:
67
67
  specification_version: 2
68
- summary: a micro web framework
68
+ summary: a micro framework
69
69
  test_files: []
70
70