rhr 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rhr (0.1.2)
4
+ rhr (0.1.3)
5
5
  rack
6
6
  tilt
7
7
 
data/Readme.md CHANGED
@@ -14,9 +14,22 @@ Usage
14
14
  - Supports [Erb, Haml, Liquid, ... everything](https://github.com/rtomayko/tilt)
15
15
  - Does not serve Rakefile / Gemfile / Gemfile.lock + everything starting with `_` or `.`
16
16
 
17
+ Layouting
18
+ =========
19
+
20
+ Put a _layout.<FORMAT> file into the root of the project.
21
+ All pages will be rendered in the `yield`.
22
+
23
+ <html>
24
+ <head>
25
+ <title>Amazing</title>
26
+ </head>
27
+ <body><%= yield %></body>
28
+ </html>
29
+
17
30
  TODO (fork!)
18
31
  ====
19
- - use _layout.erb files as layout unless view does something like `no_layout`
32
+ - do not use layout if view does something like `no_layout`
20
33
  - add helpers like link_to / tag / form
21
34
  - escape html in params <-> xss
22
35
 
@@ -24,7 +37,11 @@ Author
24
37
  ======
25
38
  Initial (crazy) idea by [Steffen Schröder](https://github.com/ChaosSteffen)
26
39
 
40
+ ### [Contributors](http://github.com/grosser/rhr/contributors)
41
+ - [Sascha Depold](http://github.com/sdeplod)
42
+
27
43
  [Michael Grosser](http://grosser.it)<br/>
28
44
  michael@grosser.it<br/>
29
45
  Hereby placed under public domain, do what you want, just do not hold me accountable...<br/>
30
46
  [![Build Status](https://secure.travis-ci.org/grosser/rhr.png)](http://travis-ci.org/grosser/rhr)
47
+
@@ -0,0 +1,26 @@
1
+ class Object
2
+ def memoize(*names)
3
+ names.each do |name|
4
+ unmemoized = "__unmemoized_#{name}"
5
+ class_eval %{
6
+ alias :#{unmemoized} :#{name}
7
+ private :#{unmemoized}
8
+ def #{name}(*args)
9
+ cache = (@#{unmemoized} ||= {})
10
+ if cache.has_key?(args)
11
+ cache[args]
12
+ else
13
+ cache[args] = send(:#{unmemoized}, *args).freeze
14
+ end
15
+ end
16
+ }
17
+ end
18
+ end unless defined?(memoize)
19
+
20
+ # Memoize class methods
21
+ def cmemoize(*method_names)
22
+ (class << self; self; end).class_eval do
23
+ memoize(*method_names)
24
+ end
25
+ end unless defined?(cmemoize)
26
+ end
data/lib/rhr/server.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'rhr/core_ext/object'
2
+
1
3
  module RHR
2
4
  class Server
3
5
  def initialize
@@ -11,7 +13,13 @@ module RHR
11
13
  if renderer = Tilt[template]
12
14
  request = Rack::Request.new(env)
13
15
  params = request.GET.merge(request.POST)
16
+
14
17
  body = renderer.new(template).render(nil, :request => request, :params => params)
18
+
19
+ if layout = find_layout
20
+ body = renderer.new(layout).render(nil, :request => request, :params => params) { body }
21
+ end
22
+
15
23
  [200, {}, [body]]
16
24
  else
17
25
  Rack::File.new('.').call(env.merge('PATH_INFO' => template))
@@ -23,6 +31,11 @@ module RHR
23
31
 
24
32
  private
25
33
 
34
+ def find_layout
35
+ Dir['*'].grep(/^_layout(\.|$)/).first
36
+ end
37
+ memoize :find_layout
38
+
26
39
  def find_template(path)
27
40
  file = path.dup
28
41
  file.sub!('/','')
@@ -40,5 +53,6 @@ module RHR
40
53
  file
41
54
  end
42
55
  end
56
+ memoize :find_template
43
57
  end
44
58
  end
data/lib/rhr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RHR
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
data/spec/rhr_spec.rb CHANGED
@@ -20,14 +20,24 @@ describe RHR do
20
20
  describe 'server' do
21
21
  include Rack::Test::Methods
22
22
 
23
- before :all do
24
- Dir.chdir 'spec/site'
25
- end
26
-
27
23
  def app
28
24
  RHR::Server.new
29
25
  end
30
26
 
27
+ def write(path, content)
28
+ @files ||= []
29
+ @files << path
30
+ File.open(path,'w'){|f| f.write(content) }
31
+ end
32
+
33
+ after do
34
+ (@files || []).each{|f| `rm #{f}` }
35
+ end
36
+
37
+ before :all do
38
+ Dir.chdir 'spec/site'
39
+ end
40
+
31
41
  it "can get /" do
32
42
  get '/'
33
43
  last_response.body.should == 'TEST'
@@ -108,5 +118,19 @@ describe RHR do
108
118
  post "/xxx/params.erb?a=1&b=2", {'b' => 3}
109
119
  last_response.body.should == "[[\"a\", \"1\"], [\"b\", \"3\"]]"
110
120
  end
121
+
122
+ describe 'with layout' do
123
+ it 'renders the layout if available' do
124
+ write '_layout.erb', '<div><%= yield %></div>'
125
+ get '/'
126
+ last_response.body.should == "<div>TEST</div>"
127
+ end
128
+
129
+ it 'does not render non-layout files' do
130
+ write 'no_layout.erb', '<div><%= yield %></div>'
131
+ get '/'
132
+ last_response.body.should == "TEST"
133
+ end
134
+ end
111
135
  end
112
136
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,10 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-07 00:00:00 +02:00
18
+ date: 2011-11-04 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
+ type: :runtime
22
23
  requirement: &id001 !ruby/object:Gem::Requirement
23
24
  none: false
24
25
  requirements:
@@ -28,11 +29,11 @@ dependencies:
28
29
  segments:
29
30
  - 0
30
31
  version: "0"
31
- type: :runtime
32
32
  name: rack
33
33
  version_requirements: *id001
34
34
  prerelease: false
35
35
  - !ruby/object:Gem::Dependency
36
+ type: :runtime
36
37
  requirement: &id002 !ruby/object:Gem::Requirement
37
38
  none: false
38
39
  requirements:
@@ -42,7 +43,6 @@ dependencies:
42
43
  segments:
43
44
  - 0
44
45
  version: "0"
45
- type: :runtime
46
46
  name: tilt
47
47
  version_requirements: *id002
48
48
  prerelease: false
@@ -62,6 +62,7 @@ files:
62
62
  - Readme.md
63
63
  - bin/rhr
64
64
  - lib/rhr.rb
65
+ - lib/rhr/core_ext/object.rb
65
66
  - lib/rhr/server.rb
66
67
  - lib/rhr/version.rb
67
68
  - rhr.gemspec