sinatra-content-for 0.1

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.
@@ -0,0 +1,3 @@
1
+ doc
2
+ dist
3
+ tmp
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2008-2009 Nicolas Sanguinetti, entp.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ = ContentFor
2
+
3
+ Small extension for the Sinatra[http://sinatrarb.com] web framework
4
+ that allows you to use the following helpers in your views:
5
+
6
+ <% content_for :some_key do %>
7
+ <chunk of="html">...</chunk>
8
+ <% end %>
9
+
10
+ <% yield_content :some_key %>
11
+
12
+ This allows you to capture blocks inside views to be rendered later
13
+ in this request. For example, to populate different parts of your
14
+ layout from your view.
15
+
16
+ == Usage
17
+
18
+ If you're writing "classic" style apps, then requring
19
+ <tt>sinatra/content_for</tt> should be enough. If you're writing
20
+ "classy" apps, then you also need to call
21
+ <tt>helpers Sinatra::ContentFor</tt> in your app definition.
22
+
23
+ == And how is this useful?
24
+
25
+ For example, some of your views might need a few javascript tags and
26
+ stylesheets, but you don't want to force this files in all your pages.
27
+ Then you can put <tt><% yield_content :scripts_and_styles %></tt> on
28
+ your layout, inside the <head> tag, and each view can call
29
+ <tt>content_for</tt> setting the appropriate set of tags that should
30
+ be added to the layout.
31
+
32
+ == Warning
33
+
34
+ This only works with ERB as a rendering mechanism. I haven't figured
35
+ how to make it work with Haml. If you find a way, contact me and I'll
36
+ include it.
37
+
38
+ == Credits
39
+
40
+ Code by foca[http://github.com/foca], inspired on the Ruby on Rails
41
+ helpers with the same name.
@@ -0,0 +1,19 @@
1
+ module Sinatra
2
+ module ContentFor
3
+ def content_for(key, &block)
4
+ content_blocks[key.to_sym] << block
5
+ end
6
+
7
+ def yield_content(key)
8
+ content_blocks[key.to_sym].each {|content| content.call }
9
+ end
10
+
11
+ private
12
+
13
+ def content_blocks
14
+ @content_blocks ||= Hash.new {|h,k| h[k] = [] }
15
+ end
16
+ end
17
+
18
+ helpers ContentFor
19
+ end
@@ -0,0 +1,34 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sinatra-content-for"
3
+ s.version = "0.1"
4
+ s.date = "2009-05-07"
5
+
6
+ s.description = "Small Sinatra extension to add a content_for helper similar to Rails'"
7
+ s.summary = "Small Sinatra extension to add a content_for helper similar to Rails'"
8
+ s.homepage = "http://sinatrarb.com"
9
+
10
+ s.authors = ["Nicolás Sanguinetti"]
11
+ s.email = "contacto@nicolassanguinetti.info"
12
+
13
+ s.require_paths = ["lib"]
14
+ s.rubyforge_project = "sinatra-ditties"
15
+ s.has_rdoc = true
16
+ s.rubygems_version = "1.3.1"
17
+
18
+ s.add_dependency "sinatra"
19
+
20
+ if s.respond_to?(:add_development_dependency)
21
+ s.add_development_dependency "contest"
22
+ s.add_development_dependency "sr-mg"
23
+ s.add_development_dependency "redgreen"
24
+ end
25
+
26
+ s.files = %w[
27
+ .gitignore
28
+ LICENSE
29
+ README.rdoc
30
+ sinatra-content-for.gemspec
31
+ lib/sinatra/content_for.rb
32
+ test/content_for_test.rb
33
+ ]
34
+ end
@@ -0,0 +1,77 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ begin
4
+ require 'rack'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'rack'
8
+ end
9
+
10
+ require 'contest'
11
+ require 'sinatra/test'
12
+
13
+ begin
14
+ require 'redgreen'
15
+ rescue LoadError
16
+ end
17
+
18
+ require File.dirname(__FILE__) + '/../lib/sinatra/content_for'
19
+
20
+ Sinatra::Base.set :environment, :test
21
+
22
+ module Sinatra
23
+ class Base
24
+ set :environment, :test
25
+ helpers ContentFor
26
+ end
27
+ end
28
+
29
+ class Test::Unit::TestCase
30
+ include Sinatra::Test
31
+
32
+ class << self
33
+ alias_method :it, :test
34
+ end
35
+
36
+ def mock_app(base=Sinatra::Base, &block)
37
+ @app = Sinatra.new(base, &block)
38
+ end
39
+ end
40
+
41
+ class ContentForTest < Test::Unit::TestCase
42
+ def erb_app(view)
43
+ mock_app {
44
+ layout { '<% yield_content :foo %>' }
45
+ get('/') { erb view }
46
+ }
47
+ end
48
+
49
+ it 'renders blocks declared with the same key you use when rendering' do
50
+ erb_app '<% content_for :foo do %>foo<% end %>'
51
+
52
+ get '/'
53
+ assert ok?
54
+ assert_equal 'foo', body
55
+ end
56
+
57
+ it 'does not render a block with a different key' do
58
+ erb_app '<% content_for :bar do %>bar<% end %>'
59
+
60
+ get '/'
61
+ assert ok?
62
+ assert_equal '', body
63
+ end
64
+
65
+ it 'renders multiple blocks with the same key' do
66
+ erb_app <<-erb_snippet
67
+ <% content_for :foo do %>foo<% end %>
68
+ <% content_for :foo do %>bar<% end %>
69
+ <% content_for :baz do %>WON'T RENDER ME<% end %>
70
+ <% content_for :foo do %>baz<% end %>
71
+ erb_snippet
72
+
73
+ get '/'
74
+ assert ok?
75
+ assert_equal 'foobarbaz', body
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-content-for
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - "Nicol\xC3\xA1s Sanguinetti"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-07 00:00:00 -03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: contest
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: sr-mg
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: redgreen
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Small Sinatra extension to add a content_for helper similar to Rails'
56
+ email: contacto@nicolassanguinetti.info
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files: []
62
+
63
+ files:
64
+ - .gitignore
65
+ - LICENSE
66
+ - README.rdoc
67
+ - sinatra-content-for.gemspec
68
+ - lib/sinatra/content_for.rb
69
+ - test/content_for_test.rb
70
+ has_rdoc: true
71
+ homepage: http://sinatrarb.com
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project: sinatra-ditties
94
+ rubygems_version: 1.3.3
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Small Sinatra extension to add a content_for helper similar to Rails'
98
+ test_files: []
99
+