sinatra-bundles 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.md +20 -0
  5. data/Rakefile +47 -0
  6. data/VERSION +1 -0
  7. data/lib/sinatra-bundles.rb +0 -0
  8. data/lib/sinatra/bundles.rb +111 -0
  9. data/spec/app.rb +11 -0
  10. data/spec/production_app.rb +12 -0
  11. data/spec/public/javascripts/test1.js +5 -0
  12. data/spec/public/javascripts/test2.js +5 -0
  13. data/spec/public/stylesheets/test1.css +3 -0
  14. data/spec/public/stylesheets/test2.css +3 -0
  15. data/spec/sinatra-bundles_spec.rb +154 -0
  16. data/spec/spec.opts +2 -0
  17. data/spec/spec_helper.rb +17 -0
  18. data/vendor/cache/sinatra-0.10.1.gem +0 -0
  19. data/vendor/gems/sinatra-0.10.1/AUTHORS +43 -0
  20. data/vendor/gems/sinatra-0.10.1/CHANGES +467 -0
  21. data/vendor/gems/sinatra-0.10.1/LICENSE +22 -0
  22. data/vendor/gems/sinatra-0.10.1/README.jp.rdoc +552 -0
  23. data/vendor/gems/sinatra-0.10.1/README.rdoc +622 -0
  24. data/vendor/gems/sinatra-0.10.1/Rakefile +129 -0
  25. data/vendor/gems/sinatra-0.10.1/lib/sinatra.rb +7 -0
  26. data/vendor/gems/sinatra-0.10.1/lib/sinatra/base.rb +1143 -0
  27. data/vendor/gems/sinatra-0.10.1/lib/sinatra/images/404.png +0 -0
  28. data/vendor/gems/sinatra-0.10.1/lib/sinatra/images/500.png +0 -0
  29. data/vendor/gems/sinatra-0.10.1/lib/sinatra/main.rb +28 -0
  30. data/vendor/gems/sinatra-0.10.1/lib/sinatra/showexceptions.rb +303 -0
  31. data/vendor/gems/sinatra-0.10.1/lib/sinatra/tilt.rb +509 -0
  32. data/vendor/gems/sinatra-0.10.1/sinatra.gemspec +86 -0
  33. data/vendor/gems/sinatra-0.10.1/test/base_test.rb +160 -0
  34. data/vendor/gems/sinatra-0.10.1/test/builder_test.rb +65 -0
  35. data/vendor/gems/sinatra-0.10.1/test/contest.rb +64 -0
  36. data/vendor/gems/sinatra-0.10.1/test/erb_test.rb +81 -0
  37. data/vendor/gems/sinatra-0.10.1/test/erubis_test.rb +82 -0
  38. data/vendor/gems/sinatra-0.10.1/test/extensions_test.rb +100 -0
  39. data/vendor/gems/sinatra-0.10.1/test/filter_test.rb +195 -0
  40. data/vendor/gems/sinatra-0.10.1/test/haml_test.rb +90 -0
  41. data/vendor/gems/sinatra-0.10.1/test/helper.rb +76 -0
  42. data/vendor/gems/sinatra-0.10.1/test/helpers_test.rb +573 -0
  43. data/vendor/gems/sinatra-0.10.1/test/mapped_error_test.rb +186 -0
  44. data/vendor/gems/sinatra-0.10.1/test/middleware_test.rb +68 -0
  45. data/vendor/gems/sinatra-0.10.1/test/request_test.rb +33 -0
  46. data/vendor/gems/sinatra-0.10.1/test/response_test.rb +42 -0
  47. data/vendor/gems/sinatra-0.10.1/test/result_test.rb +98 -0
  48. data/vendor/gems/sinatra-0.10.1/test/route_added_hook_test.rb +59 -0
  49. data/vendor/gems/sinatra-0.10.1/test/routing_test.rb +878 -0
  50. data/vendor/gems/sinatra-0.10.1/test/sass_test.rb +79 -0
  51. data/vendor/gems/sinatra-0.10.1/test/server_test.rb +47 -0
  52. data/vendor/gems/sinatra-0.10.1/test/sinatra_test.rb +13 -0
  53. data/vendor/gems/sinatra-0.10.1/test/static_test.rb +87 -0
  54. data/vendor/gems/sinatra-0.10.1/test/templates_test.rb +155 -0
  55. data/vendor/gems/sinatra-0.10.1/test/views/error.builder +3 -0
  56. data/vendor/gems/sinatra-0.10.1/test/views/error.erb +3 -0
  57. data/vendor/gems/sinatra-0.10.1/test/views/error.erubis +3 -0
  58. data/vendor/gems/sinatra-0.10.1/test/views/error.haml +3 -0
  59. data/vendor/gems/sinatra-0.10.1/test/views/error.sass +2 -0
  60. data/vendor/gems/sinatra-0.10.1/test/views/foo/hello.test +1 -0
  61. data/vendor/gems/sinatra-0.10.1/test/views/hello.builder +1 -0
  62. data/vendor/gems/sinatra-0.10.1/test/views/hello.erb +1 -0
  63. data/vendor/gems/sinatra-0.10.1/test/views/hello.erubis +1 -0
  64. data/vendor/gems/sinatra-0.10.1/test/views/hello.haml +1 -0
  65. data/vendor/gems/sinatra-0.10.1/test/views/hello.sass +2 -0
  66. data/vendor/gems/sinatra-0.10.1/test/views/hello.test +1 -0
  67. data/vendor/gems/sinatra-0.10.1/test/views/layout2.builder +3 -0
  68. data/vendor/gems/sinatra-0.10.1/test/views/layout2.erb +2 -0
  69. data/vendor/gems/sinatra-0.10.1/test/views/layout2.erubis +2 -0
  70. data/vendor/gems/sinatra-0.10.1/test/views/layout2.haml +2 -0
  71. data/vendor/gems/sinatra-0.10.1/test/views/layout2.test +1 -0
  72. data/vendor/specifications/sinatra-0.10.1.gemspec +40 -0
  73. metadata +169 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Huckstep
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ sinatra-bundles
2
+ ===============
3
+
4
+ An easy way to bundle CSS and Javascript assets in your sinatra application.
5
+
6
+ Note on Patches/Pull Requests
7
+ -----------------------------
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a
12
+ future version unintentionally.
13
+ * Commit, do not mess with rakefile, version, or history.
14
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
15
+ * Send me a pull request. Bonus points for topic branches.
16
+
17
+ Copyright
18
+ ---------
19
+
20
+ Copyright (c) 2010 Daniel Huckstep. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sinatra-bundles"
8
+ gem.summary = %Q{Easy asset bundling for sinatra}
9
+ gem.description = %Q{Bundle CSS and Javascript assets to a single file, compress, and cache them for snappier web experiences.}
10
+ gem.email = 'darkhelmet@darkhelmetlive.com'
11
+ gem.homepage = 'http://github.com/darkhelmet/sinatra-bundles'
12
+ gem.authors = ['Daniel Huckstep']
13
+ gem.add_dependency 'rainpress', '>= 0'
14
+ gem.add_dependency 'packr', '>= 0'
15
+ gem.add_development_dependency 'rspec', '>= 1.2.9'
16
+ gem.add_development_dependency 'yard', '>= 0'
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ begin
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
43
+ rescue LoadError
44
+ task :yardoc do
45
+ abort 'YARD is not available. In order to run yardoc, you must: sudo gem install yard'
46
+ end
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
File without changes
@@ -0,0 +1,111 @@
1
+ require 'rainpress'
2
+ require 'packr'
3
+
4
+ module Sinatra
5
+ module Bundles
6
+ class Bundle
7
+ def initialize(app, files)
8
+ @app = app
9
+ @files = files
10
+ end
11
+
12
+ def each
13
+ @files.each do |f|
14
+ content = File.read(path(f))
15
+ content = compress(content) if @app.compress_bundles
16
+ yield("#{content}\n")
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def stamp
23
+ @files.map do |f|
24
+ File.mtime(path(f))
25
+ end.sort.first.to_i
26
+ end
27
+ end
28
+
29
+ class StylesheetBundle < Bundle
30
+ def to_html(name)
31
+ "<link type='text/css' href='/stylesheets/bundle_#{name}.css#{@app.stamp_bundles ? "?#{stamp}" : ''}' rel='stylesheet' media='screen' />"
32
+ end
33
+
34
+ protected
35
+
36
+ def compress(css)
37
+ Rainpress.compress(css)
38
+ end
39
+
40
+ def path(filename)
41
+ File.join(@app.public, 'stylesheets', "#{filename}.css")
42
+ end
43
+ end
44
+
45
+ class JavascriptBundle < Bundle
46
+ def to_html(name)
47
+ "<script type='text/javascript' src='/javascripts/bundle_#{name}.js#{@app.stamp_bundles ? "?#{stamp}" : ''}'></script>"
48
+ end
49
+
50
+ protected
51
+
52
+ def compress(js)
53
+ Packr.pack(js, :shrink_vars => true)
54
+ end
55
+
56
+ def path(filename)
57
+ File.join(@app.public, 'javascripts', "#{filename}.js")
58
+ end
59
+ end
60
+
61
+ module Helpers
62
+ def javascript_bundle_include_tag(bundle)
63
+ options.javascript_bundles[bundle].to_html(bundle)
64
+ end
65
+
66
+ def stylesheet_bundle_link_tag(bundle)
67
+ options.stylesheet_bundles[bundle].to_html(bundle)
68
+ end
69
+ end
70
+
71
+ def javascript_bundle(key, files)
72
+ javascript_bundles[key] = JavascriptBundle.new(self, files)
73
+ end
74
+
75
+ def stylesheet_bundle(key, files)
76
+ stylesheet_bundles[key] = StylesheetBundle.new(self, files)
77
+ end
78
+
79
+ def self.registered(app)
80
+ app.set(:javascript_bundles, {})
81
+ app.set(:stylesheet_bundles, {})
82
+ app.set(:bundle_cache_time, 60 * 60 * 24 * 365)
83
+ app.disable(:compress_bundles)
84
+ app.disable(:cache_bundles)
85
+ app.enable(:stamp_bundles)
86
+
87
+ app.configure :production do
88
+ app.enable(:compress_bundles)
89
+ app.enable(:cache_bundles)
90
+ end
91
+
92
+ app.helpers(Helpers)
93
+
94
+ app.get('/stylesheets/bundle_:bundle.css') do |bundle|
95
+ content_type('text/css')
96
+ headers['Vary'] = 'Accept-Encoding'
97
+ expires(options.bundle_cache_time, :public, :must_revalidate) if options.cache_bundles
98
+ options.stylesheet_bundles[bundle.intern]
99
+ end
100
+
101
+ app.get('/javascripts/bundle_:bundle.js') do |bundle|
102
+ content_type('text/javascript; charset=utf-8')
103
+ headers['Vary'] = 'Accept-Encoding'
104
+ expires(options.bundle_cache_time, :public, :must_revalidate) if options.cache_bundles
105
+ options.javascript_bundles[bundle.intern]
106
+ end
107
+ end
108
+ end
109
+
110
+ register(Bundles)
111
+ end
data/spec/app.rb ADDED
@@ -0,0 +1,11 @@
1
+ class TestApp < Sinatra::Base
2
+ configure do
3
+ set(:environment, :test)
4
+ set(:root, File.dirname(__FILE__))
5
+ end
6
+
7
+ register Sinatra::Bundles
8
+
9
+ stylesheet_bundle(:test, %w(test1 test2))
10
+ javascript_bundle(:test, %w(test1 test2))
11
+ end
@@ -0,0 +1,12 @@
1
+ gem 'sinatra', '>= 0.10.1'
2
+ require 'sinatra/base'
3
+ require 'sinatra/bundles'
4
+
5
+ class ProductionApp < Sinatra::Base
6
+ configure do
7
+ set(:root, File.dirname(__FILE__))
8
+ set(:environment, :production)
9
+ end
10
+
11
+ register Sinatra::Bundles
12
+ end
@@ -0,0 +1,5 @@
1
+ function hello(name) {
2
+ alert('Hello, ' + name);
3
+ }
4
+
5
+ hello('Bob');
@@ -0,0 +1,5 @@
1
+ function goodbye(name) {
2
+ alert('Hello, ' + name);
3
+ }
4
+
5
+ goodbye('Bob');
@@ -0,0 +1,3 @@
1
+ .test1 {
2
+ color: black;
3
+ }
@@ -0,0 +1,3 @@
1
+ .test2 {
2
+ color: white;
3
+ }
@@ -0,0 +1,154 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'app'
3
+ require 'production_app'
4
+
5
+ describe 'SinatraBundles' do
6
+ def app(env = :test)
7
+ case env
8
+ when :production
9
+ ProductionApp
10
+ else
11
+ TestApp
12
+ end
13
+ end
14
+
15
+ def stamp(type, ext, names)
16
+ names.map do |name|
17
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', type, "#{name}.#{ext}"))
18
+ end.map do |path|
19
+ File.mtime(path)
20
+ end.sort.first.to_i
21
+ end
22
+
23
+ def js_stamp(names)
24
+ stamp('javascripts', 'js', names)
25
+ end
26
+
27
+ def css_stamp(names)
28
+ stamp('stylesheets', 'css', names)
29
+ end
30
+
31
+ before do
32
+ @scripts = %w(test1 test2).map do |name|
33
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', 'javascripts', "#{name}.js"))
34
+ end
35
+
36
+ @styles = %w(test1 test2).map do |name|
37
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', 'stylesheets', "#{name}.css"))
38
+ end
39
+ end
40
+
41
+ before(:each) do
42
+ # reset defaults
43
+ app.set(:bundle_cache_time, 60 * 60 * 24 * 365)
44
+ app.disable(:compress_bundles)
45
+ app.disable(:cache_bundles)
46
+ app.enable(:stamp_bundles)
47
+ end
48
+
49
+ context 'settings' do
50
+ it 'should not cache by default' do
51
+ app.cache_bundles.should be_false
52
+ end
53
+
54
+ it 'should not compress by default' do
55
+ app.compress_bundles.should be_false
56
+ end
57
+
58
+ it 'should cache by default in production mode' do
59
+ app(:production).environment.should == :production
60
+ app(:production).cache_bundles.should be_true
61
+ end
62
+
63
+ it 'should compress by default in production mode' do
64
+ app(:production).environment.should == :production
65
+ app(:production).compress_bundles.should be_true
66
+ end
67
+
68
+ it 'should cache for 1 year by default' do
69
+ app.bundle_cache_time.should == (60 * 60 * 24 * 365)
70
+ end
71
+
72
+ it 'should timestamp by default' do
73
+ app.stamp_bundles.should be_true
74
+ end
75
+ end
76
+
77
+ context 'javascript bundles' do
78
+ it 'should be able to set bundles' do
79
+ app.javascript_bundle(:all, %w(foo bar baz))
80
+ app.javascript_bundles.should_not == {}
81
+ app.javascript_bundles[:all].should be_a_kind_of(Sinatra::Bundles::Bundle)
82
+ end
83
+
84
+ it 'should create a tag without a stamp if stamps are disabled' do
85
+ app.new.instance_eval do
86
+ options.disable(:stamp_bundles)
87
+ javascript_bundle_include_tag(:test)
88
+ end.should == "<script type='text/javascript' src='/javascripts/bundle_test.js'></script>"
89
+ end
90
+
91
+ it 'should stamp bundles with the timestamp of the newest file in the bundle' do
92
+ app.new.instance_eval do
93
+ javascript_bundle_include_tag(:test)
94
+ end.should == "<script type='text/javascript' src='/javascripts/bundle_test.js?#{js_stamp(%w(test1 test2))}'></script>"
95
+ end
96
+
97
+ it 'should serve bundles' do
98
+ get "/javascripts/bundle_test.js"
99
+ last_response.should be_ok
100
+ end
101
+
102
+ it 'should concat files in order with newlines including one at the end' do
103
+ get '/javascripts/bundle_test.js'
104
+ last_response.body.should == @scripts.map { |path| File.read(path) }.join("\n") + "\n"
105
+ end
106
+
107
+ it 'should set cache headers' do
108
+ app.enable(:cache_bundles)
109
+ get '/javascripts/bundle_test.js'
110
+ last_response.should be_ok
111
+ last_response.headers['Vary'].should == 'Accept-Encoding'
112
+ last_response.headers['Cache-Control'].should == 'public, must-revalidate, max-age=31536000'
113
+ end
114
+ end
115
+
116
+ context 'stylesheet bundles' do
117
+ it 'should be able to set bundles' do
118
+ app.stylesheet_bundle(:all, %w(foo bar baz))
119
+ app.stylesheet_bundles.should_not == {}
120
+ app.stylesheet_bundles[:all].should be_a_kind_of(Sinatra::Bundles::Bundle)
121
+ end
122
+
123
+ it 'should create a tag without a stamp if stamps are disabled' do
124
+ app.new.instance_eval do
125
+ options.disable(:stamp_bundles)
126
+ stylesheet_bundle_link_tag(:test)
127
+ end.should == "<link type='text/css' href='/stylesheets/bundle_test.css' rel='stylesheet' media='screen' />"
128
+ end
129
+
130
+ it 'should stamp bundles with the timestamp of the newest file in the bundle' do
131
+ app.new.instance_eval do
132
+ stylesheet_bundle_link_tag(:test)
133
+ end.should == "<link type='text/css' href='/stylesheets/bundle_test.css?#{css_stamp(%w(test1 test2))}' rel='stylesheet' media='screen' />"
134
+ end
135
+
136
+ it 'should serve bundles' do
137
+ get "/stylesheets/bundle_test.css"
138
+ last_response.should be_ok
139
+ end
140
+
141
+ it 'should concat files in order with newlines including one at the end' do
142
+ get '/stylesheets/bundle_test.css'
143
+ last_response.body.should == @styles.map { |path| File.read(path) }.join("\n") + "\n"
144
+ end
145
+
146
+ it 'should set cache headers' do
147
+ app.enable(:cache_bundles)
148
+ get '/stylesheets/bundle_test.css'
149
+ last_response.should be_ok
150
+ last_response.headers['Vary'].should == 'Accept-Encoding'
151
+ last_response.headers['Cache-Control'].should == 'public, must-revalidate, max-age=31536000'
152
+ end
153
+ end
154
+ end