www 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,43 +11,37 @@ app.rb
11
11
  # encoding: utf-8
12
12
  require 'www'
13
13
 
14
- class Foo < Www::Base
15
- before do
16
- # do something
17
- end
18
-
14
+ class App < Www::Base
19
15
  get '/'
20
- def index
21
- "index"
16
+ def index(params)
17
+ params
22
18
  end
23
19
 
24
20
  get '/foo'
25
- def foo(params)
26
- haml :title => 'foo'
27
- end
28
-
29
- get '/regexp/?(.*)' # ex: /regexp/foo
30
- def regexp(arg, params)
31
- "#{arg} - #{params.inspect}"
21
+ def foo
22
+ haml :title => 'foo', :body => 'bar'
32
23
  end
33
24
 
34
25
  get '/(\d{4})/(\d{2})/(\d{2})' # ex: /2009/10/10
35
26
  def entry(year, month, date)
36
27
  [year, month, date]
37
28
  end
38
-
39
- def helper
40
- 'bar'
41
- end
42
29
  end
43
30
 
44
- views/foo.haml
31
+ foo.haml
32
+
33
+ %h2= title
34
+ %p= body
35
+
36
+ example.haml (as template)
45
37
 
46
38
  !!!
47
39
  %html
40
+ %head
41
+ %title= 'www-example'
48
42
  %body
49
- %h1= title
50
- %p= helper
43
+ %h1 www-example
44
+ != yield
51
45
 
52
46
  config.ru
53
47
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -1,32 +1,19 @@
1
1
  # encoding: utf-8
2
2
  require 'www'
3
3
 
4
- class Foo < Www::Base
5
- before do
6
- # do something
7
- end
8
-
4
+ class App < Www::Base
9
5
  get '/'
10
- def index
11
- "index"
6
+ def index(params)
7
+ params
12
8
  end
13
9
 
14
10
  get '/foo'
15
- def foo(params)
16
- haml :title => 'foo'
17
- end
18
-
19
- get '/regexp/?(.*)' # ex: /regexp/foo
20
- def regexp(arg, params)
21
- "#{arg} - #{params.inspect}"
11
+ def foo
12
+ haml :title => 'foo', :body => 'bar'
22
13
  end
23
14
 
24
15
  get '/(\d{4})/(\d{2})/(\d{2})' # ex: /2009/10/10
25
16
  def entry(year, month, date)
26
17
  [year, month, date]
27
18
  end
28
-
29
- def helper
30
- 'bar'
31
- end
32
19
  end
@@ -1,5 +1,2 @@
1
- !!!
2
- %html
3
- %body
4
- %h1= title
5
- %p= helper
1
+ %h2= title
2
+ %p= body
@@ -0,0 +1,7 @@
1
+ !!!
2
+ %html
3
+ %head
4
+ %title= 'www-example'
5
+ %body
6
+ %h1 www-example
7
+ != yield
@@ -4,15 +4,12 @@ module Www
4
4
  include View
5
5
 
6
6
  class << self
7
- @@current_route = nil
8
- @@routes = []
9
-
10
7
  def routes
11
- @@routes
8
+ @@routes ||= []
12
9
  end
13
10
 
14
11
  def route(pattern, methods = [:get])
15
- @@current_route = Route.new(pattern, methods, self)
12
+ @current_route = Route.new(pattern, methods, self)
16
13
  end
17
14
  alias_method :_, :route
18
15
 
@@ -30,10 +27,10 @@ module Www
30
27
  end
31
28
 
32
29
  def method_added(name)
33
- return unless @@current_route
34
- @@current_route.name = name
35
- @@routes << @@current_route
36
- @@current_route = nil
30
+ return unless @current_route
31
+ @current_route.name = name
32
+ routes << @current_route
33
+ @current_route = nil
37
34
 
38
35
  class_eval do
39
36
  alias_method :"www_#{name}", name
@@ -57,7 +54,7 @@ module Www
57
54
  end
58
55
 
59
56
  def find_route(path, request_method)
60
- [@@routes.detect { |route|
57
+ [routes.detect { |route|
61
58
  path.match(route.pattern) && route.request_methods.include?(request_method.downcase.to_sym) }, $~]
62
59
  end
63
60
 
@@ -81,7 +78,6 @@ module Www
81
78
  end
82
79
 
83
80
  def initialize(request = nil)
84
- # TODO: @_ で始まるようにした方がいいかもしれない
85
81
  @request = request
86
82
  @response = Rack::Response.new
87
83
  end
@@ -8,6 +8,14 @@ module Www
8
8
  end
9
9
 
10
10
  module ClassMethods
11
+ def layout(layout)
12
+ @layout = layout
13
+ end
14
+
15
+ def layout_path
16
+ @layout || 'layout'
17
+ end
18
+
11
19
  def view_dir(dir)
12
20
  @view_dir = dir
13
21
  end
@@ -26,9 +34,16 @@ module Www
26
34
  def render(type, *args)
27
35
  name = args[0].is_a?(String) ? args.shift : @route_name
28
36
  values = args[0]
29
- path = File.join(self.class.view_dir_path, "#{name}.#{type}")
30
- template = Tilt.new(path)
31
- template.render self, values # TODO: helper class instead of Object.new
37
+ view_path = File.join(self.class.view_dir_path, "#{name}.#{type}")
38
+ layout_path = "#{self.class.layout_path}.#{type}"
39
+
40
+ if File.exists?(layout_path)
41
+ Tilt.new(layout_path).render self, values do
42
+ Tilt.new(view_path).render self, values
43
+ end
44
+ else
45
+ Tilt.new(view_path).render self, values
46
+ end
32
47
  end
33
48
  end
34
49
  end
@@ -1 +1 @@
1
- = params.inspect
1
+ = foo
@@ -0,0 +1,3 @@
1
+ %html
2
+ %body
3
+ != yield
@@ -1,5 +1,6 @@
1
1
  class Foo < Www::Base
2
2
  view_dir 'spec/views'
3
+ layout 'spec/views/layout'
3
4
 
4
5
  before {}
5
6
  before(:foo, :index) {}
@@ -11,7 +12,7 @@ class Foo < Www::Base
11
12
 
12
13
  get '/foo'
13
14
  def foo(params)
14
- haml :params => params
15
+ haml :foo => params['foo']
15
16
  end
16
17
 
17
18
  get '/regexp/?(.*)'
@@ -45,7 +46,13 @@ describe "Www" do
45
46
 
46
47
  it 'should call foo as "/foo" with params' do
47
48
  params = {'foo' => 'bar'}
48
- get('/foo', params).body.chomp.should == params.inspect
49
+ get('/foo', params).body.should == <<-EOS
50
+ <html>
51
+ <body>
52
+ bar
53
+ </body>
54
+ </html>
55
+ EOS
49
56
  end
50
57
 
51
58
  it 'should call regexp as "/regexp/..."' do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - jugyo
@@ -83,6 +83,7 @@ files:
83
83
  - example/app.rb
84
84
  - example/config.ru
85
85
  - example/foo.haml
86
+ - example/layout.haml
86
87
  - lib/www.rb
87
88
  - lib/www/app.rb
88
89
  - lib/www/base.rb
@@ -90,6 +91,7 @@ files:
90
91
  - lib/www/view.rb
91
92
  - spec/spec_helper.rb
92
93
  - spec/views/foo.haml
94
+ - spec/views/layout.haml
93
95
  - spec/www_spec.rb
94
96
  has_rdoc: true
95
97
  homepage: http://github.com/jugyo/www