wrapt 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,5 +11,5 @@ group(:test) do
11
11
  gem 'ZenTest'
12
12
  gem 'haml'
13
13
  gem 'rake'
14
- gem 'jeweler'
14
+ gem 'rack-test'
15
15
  end
@@ -28,7 +28,7 @@ dependencies:
28
28
  group:
29
29
  - :test
30
30
  version: ">= 0"
31
- jeweler:
31
+ rack-test:
32
32
  group:
33
33
  - :test
34
34
  version: ">= 0"
@@ -47,25 +47,17 @@ specs:
47
47
  - any_view:
48
48
  version: 0.2.0pre
49
49
  source: 0
50
- - json_pure:
51
- version: 1.4.2
52
- - gemcutter:
53
- version: 0.5.0
54
- - git:
55
- version: 1.2.5
56
50
  - haml:
57
51
  version: 2.2.24
58
52
  - hashie:
59
53
  version: 0.2.0
60
- - rubyforge:
61
- version: 2.0.4
62
- - jeweler:
63
- version: 1.4.0
64
54
  - rack:
65
55
  version: 1.1.0
56
+ - rack-test:
57
+ version: 0.5.3
66
58
  - rspec:
67
59
  version: 1.3.0
68
- hash: 5219adb34705354844440dcaeead99c7e6b271c5
60
+ hash: 418544cd843b148c4469663f63faf3628ffce373
69
61
  sources:
70
62
  - Git:
71
63
  uri: git://github.com/hassox/any_view.git
@@ -129,6 +129,29 @@ Once that content is set, you may then use it in the layout by yielding to the l
129
129
  = yield :nav # yields the content with the label :nav
130
130
  </code></pre>
131
131
 
132
+ h3. Preventing Layouts from the Client
133
+
134
+ Wrapt allows you to prevent layouts from being applied when requested by the client.
135
+
136
+ This means that when using ajax, or esi, you can request the content without layout. There is no magic formula for this, because there could be any number of ways to provide this facility. Instead of prescribing a method to allow requests to not include the layout, you may configure wrapt with a block which checks the request. From there you can choose to ignore the layout request.
137
+
138
+ h4. Example
139
+
140
+ <pre><code>
141
+ use Wrapt do |wrapt|
142
+ wrapt.ignore_layout do |env|
143
+ request = Rack::Request.new(env)
144
+ request.params['ignore_layout'] == 'true'
145
+ end
146
+ end
147
+
148
+ run MyApp
149
+ </code></pre>
150
+
151
+ Now when you make a request to any url, you may tell it to ignore the layout by including an 'ignore_layout=true' on the url.
152
+
153
+ GET '/foo?ignore_layout=true'
154
+
132
155
  h3. Helpers
133
156
 
134
157
  You can include any helpers you need into the layout by including them into
@@ -1,23 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
 
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "wrapt"
8
- gem.summary = %Q{TODO: one-line summary of your gem}
9
- gem.description = %Q{TODO: longer description of your gem}
10
- gem.email = "has.sox@gmail.com"
11
- gem.homepage = "http://github.com/hassox/wrapt"
12
- gem.authors = ["Daniel Neighman"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- Jeweler::GemcutterTasks.new
17
- rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
- end
20
-
21
4
  require 'spec/rake/spectask'
22
5
  Spec::Rake::SpecTask.new(:spec) do |spec|
23
6
  spec.libs << 'lib' << 'spec'
@@ -30,8 +13,6 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
13
  spec.rcov = true
31
14
  end
32
15
 
33
- task :spec => :check_dependencies
34
-
35
16
  task :default => :spec
36
17
 
37
18
  require 'rake/rdoctask'
@@ -2,10 +2,12 @@ class Wrapt
2
2
  class Layout
3
3
  include Enumerable
4
4
  attr_accessor :wrapt, :env, :template_name, :format
5
+ attr_reader :request
5
6
 
6
7
  def initialize(wrapt, env)
7
8
  @env = env
8
9
  @wrapt = wrapt
10
+ @request = Rack::Request.new(@env)
9
11
  @content_for = Hashie::Mash.new
10
12
  end
11
13
 
@@ -50,7 +52,7 @@ class Wrapt
50
52
  layout.template_name = opts[:layout] if opts[:layout]
51
53
 
52
54
  layout.content = content
53
- layout.map.join
55
+ layout.render_layout(false)
54
56
  end
55
57
 
56
58
  def dup
@@ -111,6 +113,15 @@ class Wrapt
111
113
  # The interface for rack.
112
114
  # @api public
113
115
  def each
116
+ result = render_layout(@wrapt.ignore_layout?(env))
117
+ yield result
118
+ result
119
+ end
120
+
121
+ # @api private
122
+ def render_layout(ignore_layout = false)
123
+ return content_for[:content] if ignore_layout
124
+
114
125
  opts = {}
115
126
  opts[:format] ||= format
116
127
  template = template_name
@@ -125,7 +136,6 @@ class Wrapt
125
136
  else
126
137
  content_for[:content]
127
138
  end
128
- yield output
129
139
  output
130
140
  end
131
141
  end
@@ -27,6 +27,8 @@
27
27
  # If you don't want a layout, simply don't use the wrapt object.
28
28
  class Wrapt
29
29
 
30
+ IGNORE_LAYOUT = lambda{|e| false}
31
+
30
32
  # Wrapt is initialized as middleware in a Rack stack
31
33
  # @block the wrapt instance is passed to the block for further configuration
32
34
  # You can set layout template directories,
@@ -57,6 +59,39 @@ class Wrapt
57
59
  !!@master
58
60
  end
59
61
 
62
+ # Wrapt allows you to ignore layouts from the client side.
63
+ #
64
+ # This may be useful for esi, or ajax, where you want the content, but not the layout
65
+ #
66
+ # @block Provide a block to act as a guard for ignoring the layout from the client side.
67
+ # The block is provided with the Rack environment from the request
68
+ #
69
+ # @see Wrapt#ignore_layout?
70
+ # @api public
71
+ def ignore_layout(&block)
72
+ @ignore_layout = block
73
+ end
74
+
75
+ # Checks to see if the layout should be ignored for this request.
76
+ #
77
+ # @example
78
+ # use Wrapt do |wrapt|
79
+ # wrapt.ignore_layout do |env|
80
+ # request = Rack::Request.new(env)
81
+ # params["apply_layout"] == false
82
+ # end
83
+ # end
84
+ # run MyApp
85
+ #
86
+ # GET "/?apply_layout=false" # Layout is ignored
87
+ #
88
+ # @see Wrapt#ignore_layout
89
+ # @api public
90
+ def ignore_layout?(env)
91
+ @ignore_layout ||= IGNORE_LAYOUT
92
+ @ignore_layout.call(env)
93
+ end
94
+
60
95
  def call(env)
61
96
  env['request.variables'] ||= Hashie::Mash.new
62
97
  layout = env['layout']
@@ -5,6 +5,7 @@ require 'spec'
5
5
  require 'spec/autorun'
6
6
  require 'rack'
7
7
  require 'haml'
8
+ require 'rack/test'
8
9
 
9
10
  Spec::Runner.configure do |config|
10
11
 
@@ -71,6 +71,22 @@ describe Wrapt do
71
71
  @wrapt.default_format.should == :html
72
72
  end
73
73
 
74
+ it "should not ignore the layout by default" do
75
+ env = Rack::MockRequest.env_for("/")
76
+ @wrapt.ignore_layout?(env).should be_false
77
+ end
78
+
79
+ it "should allow me to setup a condition that the layout is ignored on" do
80
+ @wrapt.ignore_layout do |env|
81
+ env['ignore_layout']
82
+ end
83
+
84
+ env = Rack::MockRequest.env_for("/")
85
+ env['ignore_layout'] = true
86
+
87
+ @wrapt.ignore_layout?(env).should be_true
88
+ end
89
+
74
90
  it "should provide a hook for me to work out the format to use"
75
91
  end
76
92
 
@@ -105,18 +121,17 @@ describe Wrapt do
105
121
  describe "injecting into the environment" do
106
122
  before do
107
123
  @wrapt = Wrapt.new(SpecWraptApp){|w| w.default_template = :wrapper}
124
+ @env = Rack::MockRequest.env_for("/")
108
125
  end
109
126
 
110
127
  it "should inject a Wrapt::Layout object into the environment" do
111
- env = {}
112
- @wrapt.call(env)
113
- env['layout'].should be_an_instance_of(Wrapt::Layout)
128
+ @wrapt.call(@env)
129
+ @env['layout'].should be_an_instance_of(Wrapt::Layout)
114
130
  end
115
131
 
116
132
  it "should make sure there's a request.variables key in the env" do
117
- env = {}
118
- @wrapt.call(env)
119
- vars = env['request.variables']
133
+ @wrapt.call(@env)
134
+ vars = @env['request.variables']
120
135
  vars.should_not be_nil
121
136
  vars.should respond_to(:[])
122
137
  vars.should respond_to(:[]=)
@@ -241,5 +256,71 @@ describe Wrapt do
241
256
  result.should include("<div class='content'>Main Content</div>")
242
257
  result.should include("<div class='foo'>Foo Content</div>")
243
258
  end
259
+
260
+ describe "integrated in a rack stack" do
261
+ include Rack::Test::Methods
262
+ before do
263
+ dirs = layouts_dirs
264
+ builder = Rack::Builder.new do
265
+ use Wrapt do |w|
266
+ w.default_template = "wrapper"
267
+ w.layout_dirs = dirs
268
+ w.ignore_layout do |e|
269
+ r = Rack::Request.new(e)
270
+ r.params['apply_layout'] == 'false'
271
+ end
272
+ end
273
+ run SpecWraptApp
274
+ end
275
+
276
+ @app = builder.to_app
277
+ $message = nil
278
+ end
279
+
280
+ def app
281
+ @app
282
+ end
283
+
284
+ it "should wrap the content in the layout" do
285
+ $message = "Content For This Page"
286
+ r = get "/"
287
+ r.body.should include("Wrapper Template")
288
+ r.body.should include("Content For This Page")
289
+ end
290
+
291
+ it "should not layout when there is an apply_layout=false parameter" do
292
+ $message = "Unwrapped Content"
293
+ r = get "/", :apply_layout => :false
294
+ r.body.should include("Unwrapped Content")
295
+ r.body.should_not include("Wrapper Template")
296
+ end
297
+
298
+ it "should layout when wrapping the applciation manually" do
299
+ $wrapped_content = nil
300
+ dirs = layouts_dirs
301
+ builder = Rack::Builder.new do
302
+ use Wrapt do |w|
303
+ w.default_template = "wrapper"
304
+ w.layout_dirs = dirs
305
+ w.ignore_layout do |e|
306
+ r = Rack::Request.new(e)
307
+ r.params['apply_layout'] == 'false'
308
+ end
309
+ end
310
+ run(lambda do |e|
311
+ layout = e['layout']
312
+ $wrapped_content = layout.wrap("Manual Wrap", :layout => "other")
313
+ layout.content = "Unwrapped Content"
314
+ Rack::Response.new(layout).finish
315
+ end)
316
+ end
317
+
318
+ @app = builder.to_app
319
+ result = get "/", :apply_layout => "false"
320
+ result.body.to_s.should == "Unwrapped Content"
321
+ $wrapped_content.should include("Manual Wrap")
322
+ $wrapped_content.should include("Other Template")
323
+ end
324
+ end
244
325
  end
245
326
  end
@@ -3,7 +3,7 @@ require 'bundler'
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = %q{wrapt}
6
- s.version = "0.1.0"
6
+ s.version = "0.1.1"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.authors = ["Daniel Neighman"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Neighman
@@ -83,6 +83,7 @@ files:
83
83
  - /Users/dneighman/Projects/wrapt/spec/spec_helper.rb
84
84
  - /Users/dneighman/Projects/wrapt/spec/wrapt_spec.rb
85
85
  - /Users/dneighman/Projects/wrapt/VERSION
86
+ - /Users/dneighman/Projects/wrapt/wrapt-0.1.0.gem
86
87
  - /Users/dneighman/Projects/wrapt/wrapt.gemspec
87
88
  has_rdoc: true
88
89
  homepage: http://github.com/hassox/wrapt