sinatra-content-for2 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
- doc
2
- dist
3
- tmp
1
+ doc/
2
+ dist/
3
+ tmp/
4
+ *.swp
data/README.rdoc CHANGED
@@ -29,6 +29,8 @@ so if you have problems with that, make sure to check for this.
29
29
 
30
30
  <B>Note</b> Use <tt><%= yield_content ... %></tt> in Erubis.
31
31
 
32
+ Also we provide content_for? method that checks that block is defined.
33
+
32
34
  == Usage
33
35
 
34
36
  If you're writing "classic" style apps, then requring
@@ -51,3 +53,5 @@ Code by foca[http://github.com/foca], inspired on the Ruby on Rails
51
53
  helpers with the same name. Haml support by mattly[http://github.com/mattly].
52
54
  Erubis support by hiroyuki[http://github.com/hryk].
53
55
  Bundler/etc. unicode compatibility by arnabc[https://github.com/arnabc] and akzhan[https://github.com/akzhan].
56
+ content_for? method by BrunoGrasselli[https://github.com/BrunoGrasselli].
57
+
@@ -16,6 +16,20 @@ module Sinatra
16
16
  def content_for(key, &block)
17
17
  content_blocks[key.to_sym] << block if block_given?
18
18
  end
19
+
20
+ # Check if a block of content with the given key was defined. For
21
+ # example:
22
+ #
23
+ # <% content_for :head do %>
24
+ # <script type="text/javascript" src="/foo.js"></script>
25
+ # <% end %>
26
+ #
27
+ # <% if content_for? :head %>
28
+ # <span>content "head" was defined.</span>
29
+ # <% end %>
30
+ def content_for?(key)
31
+ content_blocks[key.to_sym].any?
32
+ end
19
33
 
20
34
  # Render the captured blocks for a given key. For example:
21
35
  #
@@ -2,8 +2,8 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "sinatra-content-for2"
5
- s.version = "0.2.3"
6
- s.date = "2011-01-13"
5
+ s.version = "0.2.4"
6
+ s.date = "2011-02-04"
7
7
 
8
8
  s.description = "Small Sinatra extension to add a content_for helper similar to Rails'"
9
9
  s.summary = "Small Sinatra extension to add a content_for helper similar to Rails'"
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.require_paths = ["lib"]
16
16
  s.rubyforge_project = "sinatra-ditties"
17
17
  s.has_rdoc = true
18
- s.rubygems_version = "1.3.1"
18
+ s.rubygems_version = "1.3.7"
19
19
 
20
20
  s.add_dependency "sinatra"
21
21
 
@@ -8,27 +8,31 @@ rescue LoadError
8
8
  end
9
9
 
10
10
  require 'contest'
11
- require 'sinatra/test'
11
+ require 'sinatra'
12
+ require 'erubis'
12
13
  require 'haml'
14
+ require 'rack/test'
13
15
 
14
16
  begin
15
17
  require 'redgreen'
16
18
  rescue LoadError
17
19
  end
18
20
 
19
- require File.dirname(__FILE__) + '/../lib/sinatra/content_for'
21
+ $LOAD_PATH.unshift('./lib')
22
+
23
+ require 'sinatra/content_for2'
20
24
 
21
25
  Sinatra::Base.set :environment, :test
22
26
 
23
27
  module Sinatra
24
28
  class Base
25
29
  set :environment, :test
26
- helpers ContentFor
30
+ helpers ContentFor2
27
31
  end
28
32
  end
29
33
 
30
34
  class Test::Unit::TestCase
31
- include Sinatra::Test
35
+ include Rack::Test::Methods
32
36
 
33
37
  class << self
34
38
  alias_method :it, :test
@@ -37,11 +41,15 @@ class Test::Unit::TestCase
37
41
  def mock_app(base=Sinatra::Base, &block)
38
42
  @app = Sinatra.new(base, &block)
39
43
  end
44
+
45
+ def app
46
+ @app
47
+ end
40
48
  end
41
49
 
42
50
  class ContentForTest < Test::Unit::TestCase
43
- context 'using erb' do
44
- def erb_app(view)
51
+ context 'using erubis' do
52
+ def erubis_app(view)
45
53
  mock_app {
46
54
  layout { '<% yield_content :foo %>' }
47
55
  get('/') { erb view }
@@ -49,23 +57,23 @@ class ContentForTest < Test::Unit::TestCase
49
57
  end
50
58
 
51
59
  it 'renders blocks declared with the same key you use when rendering' do
52
- erb_app '<% content_for :foo do %>foo<% end %>'
60
+ erubis_app '<% content_for :foo do %>foo<% end %>'
53
61
 
54
62
  get '/'
55
- assert ok?
56
- assert_equal 'foo', body
63
+ assert last_response.ok?
64
+ assert_equal 'foo', last_response.body
57
65
  end
58
66
 
59
67
  it 'does not render a block with a different key' do
60
- erb_app '<% content_for :bar do %>bar<% end %>'
68
+ erubis_app '<% content_for :bar do %>bar<% end %>'
61
69
 
62
70
  get '/'
63
- assert ok?
64
- assert_equal '', body
71
+ assert last_response.ok?
72
+ assert_equal '', last_response.body
65
73
  end
66
74
 
67
75
  it 'renders multiple blocks with the same key' do
68
- erb_app <<-erb_snippet
76
+ erubis_app <<-erb_snippet
69
77
  <% content_for :foo do %>foo<% end %>
70
78
  <% content_for :foo do %>bar<% end %>
71
79
  <% content_for :baz do %>WON'T RENDER ME<% end %>
@@ -73,8 +81,8 @@ class ContentForTest < Test::Unit::TestCase
73
81
  erb_snippet
74
82
 
75
83
  get '/'
76
- assert ok?
77
- assert_equal 'foobarbaz', body
84
+ assert last_response.ok?
85
+ assert_equal 'foobarbaz', last_response.body
78
86
  end
79
87
 
80
88
  it 'passes values to the blocks' do
@@ -84,8 +92,8 @@ class ContentForTest < Test::Unit::TestCase
84
92
  }
85
93
 
86
94
  get '/'
87
- assert ok?
88
- assert_equal '<i>1</i> 2', body
95
+ assert last_response.ok?
96
+ assert_equal '<i>1</i> 2', last_response.body
89
97
  end
90
98
  end
91
99
 
@@ -104,8 +112,8 @@ class ContentForTest < Test::Unit::TestCase
104
112
  haml_end
105
113
 
106
114
  get '/'
107
- assert ok?
108
- assert_equal "foo\n", body
115
+ assert last_response.ok?
116
+ assert_equal "foo\n", last_response.body
109
117
  end
110
118
 
111
119
  it 'does not render a block with a different key' do
@@ -115,8 +123,8 @@ haml_end
115
123
  haml_end
116
124
 
117
125
  get '/'
118
- assert ok?
119
- assert_equal "\n", body
126
+ assert last_response.ok?
127
+ assert_equal "\n", last_response.body
120
128
  end
121
129
 
122
130
  it 'renders multiple blocks with the same key' do
@@ -132,8 +140,8 @@ haml_end
132
140
  haml_end
133
141
 
134
142
  get '/'
135
- assert ok?
136
- assert_equal "foo\nbar\nbaz\n", body
143
+ assert last_response.ok?
144
+ assert_equal "foo\nbar\nbaz\n", last_response.body
137
145
  end
138
146
 
139
147
  it 'passes values to the blocks' do
@@ -149,8 +157,38 @@ haml_end
149
157
  }
150
158
 
151
159
  get '/'
152
- assert ok?
153
- assert_equal "<i>1</i>\n2\n", body
160
+ assert last_response.ok?
161
+ assert_equal "<i>1</i>\n2\n", last_response.body
162
+ end
163
+ end
164
+
165
+ context 'content_for?' do
166
+ class TestApp
167
+ include Sinatra::ContentFor2
168
+ end
169
+
170
+ setup do
171
+ @helper = TestApp.new
172
+ end
173
+
174
+ context 'defined content' do
175
+ setup do
176
+ @helper.instance_variable_set(:@content_blocks, {:test => ["block"]})
177
+ end
178
+
179
+ it 'returns true' do
180
+ assert_equal @helper.content_for?(:test), true
181
+ end
182
+ end
183
+
184
+ context 'undefined content' do
185
+ setup do
186
+ @helper.instance_variable_set(:@content_blocks, {:test => []})
187
+ end
188
+
189
+ it 'returns false' do
190
+ assert_equal @helper.content_for?(:test), false
191
+ end
154
192
  end
155
193
  end
156
194
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-content-for2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 2
9
- - 3
10
- version: 0.2.3
8
+ - 4
9
+ version: 0.2.4
11
10
  platform: ruby
12
11
  authors:
13
12
  - "Nicol\xC3\xA1s Sanguinetti"
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-01-13 00:00:00 +03:00
17
+ date: 2011-02-04 00:00:00 +03:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -40,7 +38,6 @@ dependencies:
40
38
  requirements:
41
39
  - - ">="
42
40
  - !ruby/object:Gem::Version
43
- hash: 3
44
41
  segments:
45
42
  - 0
46
43
  version: "0"
@@ -54,7 +51,6 @@ dependencies:
54
51
  requirements:
55
52
  - - ">="
56
53
  - !ruby/object:Gem::Version
57
- hash: 3
58
54
  segments:
59
55
  - 0
60
56
  version: "0"
@@ -68,7 +64,6 @@ dependencies:
68
64
  requirements:
69
65
  - - ">="
70
66
  - !ruby/object:Gem::Version
71
- hash: 3
72
67
  segments:
73
68
  - 0
74
69
  version: "0"
@@ -103,7 +98,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
98
  requirements:
104
99
  - - ">="
105
100
  - !ruby/object:Gem::Version
106
- hash: 3
107
101
  segments:
108
102
  - 0
109
103
  version: "0"
@@ -112,7 +106,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
106
  requirements:
113
107
  - - ">="
114
108
  - !ruby/object:Gem::Version
115
- hash: 3
116
109
  segments:
117
110
  - 0
118
111
  version: "0"