rhr 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/Readme.md +16 -0
- data/lib/rhr/server.rb +19 -7
- data/lib/rhr/version.rb +1 -1
- data/lib/rhr/view.rb +4 -0
- data/spec/rhr_spec.rb +26 -0
- metadata +44 -63
- data/spec/site/Gemfile +0 -1
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/Gemfile.lock
CHANGED
data/Readme.md
CHANGED
@@ -27,10 +27,26 @@ Layouting
|
|
27
27
|
<body><%= yield %></body>
|
28
28
|
</html>
|
29
29
|
|
30
|
+
Helpers
|
31
|
+
=======
|
32
|
+
|
33
|
+
Put a `helpers.rb` file into the project root:
|
34
|
+
|
35
|
+
module Helpers
|
36
|
+
def foo
|
37
|
+
'bar'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
You can now use the `foo` method in the view:
|
42
|
+
|
43
|
+
<div><%= foo %></div>
|
44
|
+
|
30
45
|
TODO (fork!)
|
31
46
|
====
|
32
47
|
- do not use layout if view does something like `no_layout`
|
33
48
|
- add helpers like link_to / tag / form
|
49
|
+
- move params and request to the View
|
34
50
|
- escape html in params <-> xss
|
35
51
|
|
36
52
|
Author
|
data/lib/rhr/server.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
require 'rhr/view'
|
1
2
|
require 'rhr/core_ext/object'
|
2
3
|
|
3
4
|
module RHR
|
4
5
|
class Server
|
5
6
|
def initialize
|
6
|
-
@files = Dir["**/*"]
|
7
|
-
@
|
7
|
+
@files = Dir["**/*"]
|
8
|
+
@servable_files = @files - %w[Gemfile Gemfile.lock Rakefile helpers.rb]
|
9
|
+
@servable_files -= @servable_files.grep(/(^|\/)_/)
|
8
10
|
end
|
9
11
|
|
10
12
|
def call(env)
|
@@ -14,10 +16,11 @@ module RHR
|
|
14
16
|
request = Rack::Request.new(env)
|
15
17
|
params = request.GET.merge(request.POST)
|
16
18
|
|
17
|
-
|
19
|
+
view = build_view
|
20
|
+
body = renderer.new(template).render(view, :request => request, :params => params)
|
18
21
|
|
19
22
|
if layout = find_layout
|
20
|
-
body = renderer.new(layout).render(
|
23
|
+
body = renderer.new(layout).render(view, :request => request, :params => params) { body }
|
21
24
|
end
|
22
25
|
|
23
26
|
[200, {}, [body]]
|
@@ -31,8 +34,17 @@ module RHR
|
|
31
34
|
|
32
35
|
private
|
33
36
|
|
37
|
+
def build_view
|
38
|
+
view = View.new
|
39
|
+
if @files.include?('helpers.rb')
|
40
|
+
require './helpers'
|
41
|
+
view.send(:extend, Helpers)
|
42
|
+
end
|
43
|
+
view
|
44
|
+
end
|
45
|
+
|
34
46
|
def find_layout
|
35
|
-
|
47
|
+
@files.grep(/^_layout(\.|$)/).first
|
36
48
|
end
|
37
49
|
memoize :find_layout
|
38
50
|
|
@@ -42,12 +54,12 @@ module RHR
|
|
42
54
|
file.sub!(/\/$/,'')
|
43
55
|
is_root = (file == '')
|
44
56
|
|
45
|
-
return if not is_root and not @
|
57
|
+
return if not is_root and not @servable_files.index(file)
|
46
58
|
|
47
59
|
if is_root or File.directory?(file)
|
48
60
|
# folder -> find an index file
|
49
61
|
file << '/' unless is_root
|
50
|
-
@
|
62
|
+
@servable_files.grep(/^#{Regexp.escape file}index\.[^\/]+$/).first
|
51
63
|
else
|
52
64
|
# just another normal template
|
53
65
|
file
|
data/lib/rhr/version.rb
CHANGED
data/lib/rhr/view.rb
ADDED
data/spec/rhr_spec.rb
CHANGED
@@ -79,6 +79,14 @@ describe RHR do
|
|
79
79
|
last_response.content_type.should == 'image/jpeg'
|
80
80
|
end
|
81
81
|
|
82
|
+
%w[Rakefile Gemfile Gemfile.lock helpers.rb].each do |file|
|
83
|
+
it "ignores #{file}" do
|
84
|
+
write file, 'Foo'
|
85
|
+
get "/#{file}"
|
86
|
+
last_response.status.should == 404
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
82
90
|
it "ignores common ruby files" do
|
83
91
|
get "/Gemfile"
|
84
92
|
last_response.status.should == 404
|
@@ -132,5 +140,23 @@ describe RHR do
|
|
132
140
|
last_response.body.should == "TEST"
|
133
141
|
end
|
134
142
|
end
|
143
|
+
|
144
|
+
describe 'with helpers' do
|
145
|
+
before do
|
146
|
+
write 'helpers.erb', '<%= foo %>'
|
147
|
+
write 'helpers.rb', 'module Helpers; def foo; "hello"; end; end'
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'passes helpers into the view' do
|
151
|
+
get "/helpers.erb"
|
152
|
+
last_response.body.should == 'hello'
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'passes helpers into the layout view' do
|
156
|
+
write '_layout.erb', '<%= foo %>|<%= yield %>'
|
157
|
+
get "/helpers.erb"
|
158
|
+
last_response.body.should == 'hello|hello'
|
159
|
+
end
|
160
|
+
end
|
135
161
|
end
|
136
162
|
end
|
metadata
CHANGED
@@ -1,60 +1,46 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhr
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 3
|
10
|
-
version: 0.1.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Michael Grosser
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
22
|
-
type: :runtime
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
12
|
+
date: 2011-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
32
15
|
name: rack
|
33
|
-
|
34
|
-
prerelease: false
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
type: :runtime
|
37
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &71381800 !ruby/object:Gem::Requirement
|
38
17
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *71381800
|
25
|
+
- !ruby/object:Gem::Dependency
|
46
26
|
name: tilt
|
47
|
-
|
27
|
+
requirement: &71381480 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
48
34
|
prerelease: false
|
35
|
+
version_requirements: *71381480
|
49
36
|
description:
|
50
37
|
email: michael@grosser.it
|
51
|
-
executables:
|
38
|
+
executables:
|
52
39
|
- rhr
|
53
40
|
extensions: []
|
54
|
-
|
55
41
|
extra_rdoc_files: []
|
56
|
-
|
57
|
-
|
42
|
+
files:
|
43
|
+
- .gitignore
|
58
44
|
- .travis.yml
|
59
45
|
- Gemfile
|
60
46
|
- Gemfile.lock
|
@@ -65,10 +51,10 @@ files:
|
|
65
51
|
- lib/rhr/core_ext/object.rb
|
66
52
|
- lib/rhr/server.rb
|
67
53
|
- lib/rhr/version.rb
|
54
|
+
- lib/rhr/view.rb
|
68
55
|
- rhr.gemspec
|
69
56
|
- spec/rhr_spec.rb
|
70
57
|
- spec/site/.hidden.html
|
71
|
-
- spec/site/Gemfile
|
72
58
|
- spec/site/_hidden.html
|
73
59
|
- spec/site/_hidden/xxx.html
|
74
60
|
- spec/site/index.erb
|
@@ -81,39 +67,34 @@ files:
|
|
81
67
|
- spec/site/xxx/params.erb
|
82
68
|
- spec/site/yyy/index.html
|
83
69
|
- spec/spec_helper.rb
|
84
|
-
has_rdoc: true
|
85
70
|
homepage: http://github.com/grosser/rhr
|
86
71
|
licenses: []
|
87
|
-
|
88
72
|
post_install_message:
|
89
73
|
rdoc_options: []
|
90
|
-
|
91
|
-
require_paths:
|
74
|
+
require_paths:
|
92
75
|
- lib
|
93
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
77
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
segments:
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
segments:
|
100
83
|
- 0
|
101
|
-
|
102
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
hash: -943244397
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
86
|
none: false
|
104
|
-
requirements:
|
105
|
-
- -
|
106
|
-
- !ruby/object:Gem::Version
|
107
|
-
|
108
|
-
segments:
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
segments:
|
109
92
|
- 0
|
110
|
-
|
93
|
+
hash: -943244397
|
111
94
|
requirements: []
|
112
|
-
|
113
95
|
rubyforge_project:
|
114
|
-
rubygems_version: 1.
|
96
|
+
rubygems_version: 1.8.10
|
115
97
|
signing_key:
|
116
98
|
specification_version: 3
|
117
99
|
summary: Ruby Hypertext Refinement -- the ease of PHP with the elegance of Ruby
|
118
100
|
test_files: []
|
119
|
-
|
data/spec/site/Gemfile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
xxx
|