sinatra-bundles 0.0.2 → 0.1.0

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
@@ -17,5 +17,7 @@ tmtags
17
17
  coverage
18
18
  rdoc
19
19
  pkg
20
+ doc
21
+ .yardoc
20
22
 
21
23
  ## PROJECT::SPECIFIC
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Daniel Huckstep
1
+ Copyright (c) 2010 Daniel Huckstep
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -3,6 +3,78 @@ sinatra-bundles
3
3
 
4
4
  An easy way to bundle CSS and Javascript assets in your sinatra application.
5
5
 
6
+ * Tests: [http://runcoderun.com/darkhelmet/sinatra-bundles](http://runcoderun.com/darkhelmet/sinatra-bundles)
7
+
8
+ Usage
9
+ -----
10
+
11
+ sinatra-bundles combines Javascript and CSS into one file. Meaning, you can bundle 2 or more Javascript files into one, similar with CSS stylesheets. Any bundled files are expected to be in the public directory, under 'javascripts' and 'stylesheets'
12
+
13
+ Assuming you have the following files in public:
14
+
15
+ ./stylesheets/reset.css
16
+ ./stylesheets/fonts.css
17
+ ./stylesheets/grid.css
18
+ ./javascripts/jquery.js
19
+ ./javascripts/lightbox.js
20
+ ./javascripts/blog.js
21
+
22
+ You can bundle these files in your app like this:
23
+
24
+ require 'sinatra'
25
+ require 'sinatra/bundles'
26
+
27
+ stylesheet_bundle(:all, %w(reset fonts grid))
28
+ javascript_bundle(:all, %w(jquery lightbox blog))
29
+
30
+ get '/' do
31
+ 'sinatra-bundles rocks!'
32
+ end
33
+
34
+ Then in your view, you can use the view helpers to insert the proper script tags:
35
+
36
+ = javascript_bundle_include_tag(:all)
37
+ = stylesheet_bundle_link_tag(:all)
38
+
39
+ All 6 of those files will be served up in 2 files, and they'll be compressed and have headers set for caching.
40
+
41
+ Configuration
42
+ -------------
43
+
44
+ The defaults are pretty good. In development/test mode:
45
+
46
+ bundle_cache_time # => 60 * 60 * 24 * 365, or 1 year
47
+ compress_bundles # => false
48
+ cache_bundles # => false
49
+ stamp_bundles # => true
50
+
51
+ And in production mode, compression and caching is enabled
52
+
53
+ compress_bundles # => true
54
+ cache_bundles # => true
55
+
56
+ To change any of these, use set/enable/disable
57
+
58
+ require 'sinatra'
59
+ require 'sinatra/bundles'
60
+
61
+ stylesheet_bundle(:all, %w(reset fonts grid))
62
+ javascript_bundle(:all, %w(jquery lightbox blog))
63
+
64
+ disable(:compress_bundles)
65
+ enable(:cache_bundles)
66
+ set(:bundle_cache_time, 60 * 60 * 24)
67
+ disable(:stamp_bundles)
68
+
69
+ get '/' do
70
+ 'sinatra-bundles rocks!'
71
+ end
72
+
73
+ Examples
74
+ --------
75
+
76
+ Check out the code for my blog for a real example: [darkblog on github](http://github.com/darkhelmet/darkblog)
77
+
6
78
  Note on Patches/Pull Requests
7
79
  -----------------------------
8
80
 
data/Rakefile CHANGED
@@ -13,6 +13,7 @@ begin
13
13
  gem.add_dependency 'rainpress', '>= 0'
14
14
  gem.add_dependency 'packr', '>= 0'
15
15
  gem.add_development_dependency 'rspec', '>= 1.2.9'
16
+ gem.add_development_dependency 'rack-test', '>= 0.5.3'
16
17
  gem.add_development_dependency 'yard', '>= 0'
17
18
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
19
  end
@@ -39,7 +40,9 @@ task :default => :spec
39
40
 
40
41
  begin
41
42
  require 'yard'
42
- YARD::Rake::YardocTask.new
43
+ YARD::Rake::YardocTask.new do |t|
44
+ t.files = ['lib/**/*.rb']
45
+ end
43
46
  rescue LoadError
44
47
  task :yardoc do
45
48
  abort 'YARD is not available. In order to run yardoc, you must: sudo gem install yard'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.0
@@ -2,23 +2,34 @@ require 'rainpress'
2
2
  require 'packr'
3
3
 
4
4
  module Sinatra
5
+ # Main Bundles Module
5
6
  module Bundles
7
+ # The base class for a bundle of files.
8
+ # The developer user sinatra-bundles should
9
+ # never have to deal with this directly
6
10
  class Bundle
7
11
  def initialize(app, files)
8
12
  @app = app
9
13
  @files = files
10
14
  end
11
15
 
16
+ # Since we pass Bundles back as the body,
17
+ # this follows Rack standards and supports an each method
18
+ # to yield parts of the body, in our case, the files.
12
19
  def each
13
20
  @files.each do |f|
14
21
  content = File.read(path(f))
15
22
  content = compress(content) if @app.compress_bundles
23
+ # Include a new line to prevent weirdness at file boundaries
16
24
  yield("#{content}\n")
17
25
  end
18
26
  end
19
27
 
20
28
  private
21
29
 
30
+ # The timestamp of the bundle, which is the newest file in the bundle.
31
+ #
32
+ # @return [Integer] The timestamp of the bundle
22
33
  def stamp
23
34
  @files.map do |f|
24
35
  File.mtime(path(f))
@@ -26,64 +37,115 @@ module Sinatra
26
37
  end
27
38
  end
28
39
 
40
+ # Bundle for stylesheets
29
41
  class StylesheetBundle < Bundle
42
+ # Generate the HTML tag for the stylesheet
43
+ #
44
+ # @param [String] name The name of a bundle
45
+ # @return [String] The HTML that can be inserted into the doc
30
46
  def to_html(name)
31
47
  "<link type='text/css' href='/stylesheets/bundle_#{name}.css#{@app.stamp_bundles ? "?#{stamp}" : ''}' rel='stylesheet' media='screen' />"
32
48
  end
33
49
 
34
50
  protected
35
51
 
52
+ # Compress CSS
53
+ #
54
+ # @param [String] css The CSS to compress
55
+ # @return [String] Compressed CSS
36
56
  def compress(css)
37
57
  Rainpress.compress(css)
38
58
  end
39
59
 
60
+ # Get the path of the file on disk
61
+ #
62
+ # @param [String] filename The name of sheet,
63
+ # assumed to be in the public directory, under 'stylesheets'
64
+ # @return [String] The full path to the file
40
65
  def path(filename)
41
66
  File.join(@app.public, 'stylesheets', "#{filename}.css")
42
67
  end
43
68
  end
44
69
 
70
+ # Bundle for javascripts
45
71
  class JavascriptBundle < Bundle
72
+ # Generate the HTML tag for the script file
73
+ #
74
+ # @param [String] name The name of a bundle
75
+ # @return [String] The HTML that can be inserted into the doc
46
76
  def to_html(name)
47
77
  "<script type='text/javascript' src='/javascripts/bundle_#{name}.js#{@app.stamp_bundles ? "?#{stamp}" : ''}'></script>"
48
78
  end
49
79
 
50
80
  protected
51
81
 
82
+ # Compress Javascript
83
+ #
84
+ # @param [String] js The Javascript to compress
85
+ # @return [String] Compressed Javascript
52
86
  def compress(js)
53
87
  Packr.pack(js, :shrink_vars => true)
54
88
  end
55
89
 
90
+ # Get the path of the file on disk
91
+ #
92
+ # @param [String] filename The name of sheet,
93
+ # assumed to be in the public directory, under 'javascripts'
94
+ # @return [String] The full path to the file
56
95
  def path(filename)
57
96
  File.join(@app.public, 'javascripts', "#{filename}.js")
58
97
  end
59
98
  end
60
99
 
100
+ # View helpers
61
101
  module Helpers
102
+ # Emit a script tag for a javascript bundle
103
+ #
104
+ # @param [Symbol,String] bundle The bundle name
105
+ # @return [String] HTML script tag
62
106
  def javascript_bundle_include_tag(bundle)
63
107
  options.javascript_bundles[bundle].to_html(bundle)
64
108
  end
65
109
 
110
+ # Emit a script tag for a stylesheet bundle
111
+ #
112
+ # @param [Symbol,String] bundle The bundle name
113
+ # @return [String] HTML link tag
66
114
  def stylesheet_bundle_link_tag(bundle)
67
115
  options.stylesheet_bundles[bundle].to_html(bundle)
68
116
  end
69
117
  end
70
118
 
119
+ # Set a Javascript bundle
120
+ # javascript_bundle(:all, %w(jquery lightbox))
121
+ # @param [Symbol,String] key The bundle name
122
+ # @param [Array(String)] files The list of filenames, without extension,
123
+ # assumed to be in the public directory, under 'javascripts'
71
124
  def javascript_bundle(key, files)
72
125
  javascript_bundles[key] = JavascriptBundle.new(self, files)
73
126
  end
74
127
 
128
+ # Set a CSS bundle
129
+ # stylesheet_bundle(:all, %w(reset grid fonts))
130
+ # @param [Symbol,String] key The bundle name
131
+ # @param [Array(String)] files The list of filenames, without extension,
132
+ # assumed to be in the public directory, under 'stylesheets'
75
133
  def stylesheet_bundle(key, files)
76
134
  stylesheet_bundles[key] = StylesheetBundle.new(self, files)
77
135
  end
78
136
 
79
137
  def self.registered(app)
138
+ # Setup empty bundle containers
80
139
  app.set(:javascript_bundles, {})
81
140
  app.set(:stylesheet_bundles, {})
141
+
142
+ # Setup defaults
82
143
  app.set(:bundle_cache_time, 60 * 60 * 24 * 365)
83
144
  app.disable(:compress_bundles)
84
145
  app.disable(:cache_bundles)
85
146
  app.enable(:stamp_bundles)
86
147
 
148
+ # Production defaults
87
149
  app.configure :production do
88
150
  app.enable(:compress_bundles)
89
151
  app.enable(:cache_bundles)
@@ -0,0 +1,130 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sinatra-bundles}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel Huckstep"]
12
+ s.date = %q{2010-01-12}
13
+ s.description = %q{Bundle CSS and Javascript assets to a single file, compress, and cache them for snappier web experiences.}
14
+ s.email = %q{darkhelmet@darkhelmetlive.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/sinatra/bundles.rb",
27
+ "sinatra-bundles.gemspec",
28
+ "spec/app.rb",
29
+ "spec/production_app.rb",
30
+ "spec/public/javascripts/test1.js",
31
+ "spec/public/javascripts/test2.js",
32
+ "spec/public/stylesheets/test1.css",
33
+ "spec/public/stylesheets/test2.css",
34
+ "spec/sinatra-bundles_spec.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb",
37
+ "vendor/cache/sinatra-0.10.1.gem",
38
+ "vendor/gems/sinatra-0.10.1/AUTHORS",
39
+ "vendor/gems/sinatra-0.10.1/CHANGES",
40
+ "vendor/gems/sinatra-0.10.1/LICENSE",
41
+ "vendor/gems/sinatra-0.10.1/README.jp.rdoc",
42
+ "vendor/gems/sinatra-0.10.1/README.rdoc",
43
+ "vendor/gems/sinatra-0.10.1/Rakefile",
44
+ "vendor/gems/sinatra-0.10.1/lib/sinatra.rb",
45
+ "vendor/gems/sinatra-0.10.1/lib/sinatra/base.rb",
46
+ "vendor/gems/sinatra-0.10.1/lib/sinatra/images/404.png",
47
+ "vendor/gems/sinatra-0.10.1/lib/sinatra/images/500.png",
48
+ "vendor/gems/sinatra-0.10.1/lib/sinatra/main.rb",
49
+ "vendor/gems/sinatra-0.10.1/lib/sinatra/showexceptions.rb",
50
+ "vendor/gems/sinatra-0.10.1/lib/sinatra/tilt.rb",
51
+ "vendor/gems/sinatra-0.10.1/sinatra.gemspec",
52
+ "vendor/gems/sinatra-0.10.1/test/base_test.rb",
53
+ "vendor/gems/sinatra-0.10.1/test/builder_test.rb",
54
+ "vendor/gems/sinatra-0.10.1/test/contest.rb",
55
+ "vendor/gems/sinatra-0.10.1/test/erb_test.rb",
56
+ "vendor/gems/sinatra-0.10.1/test/erubis_test.rb",
57
+ "vendor/gems/sinatra-0.10.1/test/extensions_test.rb",
58
+ "vendor/gems/sinatra-0.10.1/test/filter_test.rb",
59
+ "vendor/gems/sinatra-0.10.1/test/haml_test.rb",
60
+ "vendor/gems/sinatra-0.10.1/test/helper.rb",
61
+ "vendor/gems/sinatra-0.10.1/test/helpers_test.rb",
62
+ "vendor/gems/sinatra-0.10.1/test/mapped_error_test.rb",
63
+ "vendor/gems/sinatra-0.10.1/test/middleware_test.rb",
64
+ "vendor/gems/sinatra-0.10.1/test/request_test.rb",
65
+ "vendor/gems/sinatra-0.10.1/test/response_test.rb",
66
+ "vendor/gems/sinatra-0.10.1/test/result_test.rb",
67
+ "vendor/gems/sinatra-0.10.1/test/route_added_hook_test.rb",
68
+ "vendor/gems/sinatra-0.10.1/test/routing_test.rb",
69
+ "vendor/gems/sinatra-0.10.1/test/sass_test.rb",
70
+ "vendor/gems/sinatra-0.10.1/test/server_test.rb",
71
+ "vendor/gems/sinatra-0.10.1/test/sinatra_test.rb",
72
+ "vendor/gems/sinatra-0.10.1/test/static_test.rb",
73
+ "vendor/gems/sinatra-0.10.1/test/templates_test.rb",
74
+ "vendor/gems/sinatra-0.10.1/test/views/error.builder",
75
+ "vendor/gems/sinatra-0.10.1/test/views/error.erb",
76
+ "vendor/gems/sinatra-0.10.1/test/views/error.erubis",
77
+ "vendor/gems/sinatra-0.10.1/test/views/error.haml",
78
+ "vendor/gems/sinatra-0.10.1/test/views/error.sass",
79
+ "vendor/gems/sinatra-0.10.1/test/views/foo/hello.test",
80
+ "vendor/gems/sinatra-0.10.1/test/views/hello.builder",
81
+ "vendor/gems/sinatra-0.10.1/test/views/hello.erb",
82
+ "vendor/gems/sinatra-0.10.1/test/views/hello.erubis",
83
+ "vendor/gems/sinatra-0.10.1/test/views/hello.haml",
84
+ "vendor/gems/sinatra-0.10.1/test/views/hello.sass",
85
+ "vendor/gems/sinatra-0.10.1/test/views/hello.test",
86
+ "vendor/gems/sinatra-0.10.1/test/views/layout2.builder",
87
+ "vendor/gems/sinatra-0.10.1/test/views/layout2.erb",
88
+ "vendor/gems/sinatra-0.10.1/test/views/layout2.erubis",
89
+ "vendor/gems/sinatra-0.10.1/test/views/layout2.haml",
90
+ "vendor/gems/sinatra-0.10.1/test/views/layout2.test",
91
+ "vendor/specifications/sinatra-0.10.1.gemspec"
92
+ ]
93
+ s.homepage = %q{http://github.com/darkhelmet/sinatra-bundles}
94
+ s.rdoc_options = ["--charset=UTF-8"]
95
+ s.require_paths = ["lib"]
96
+ s.rubygems_version = %q{1.3.5}
97
+ s.summary = %q{Easy asset bundling for sinatra}
98
+ s.test_files = [
99
+ "spec/app.rb",
100
+ "spec/production_app.rb",
101
+ "spec/sinatra-bundles_spec.rb",
102
+ "spec/spec_helper.rb"
103
+ ]
104
+
105
+ if s.respond_to? :specification_version then
106
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
107
+ s.specification_version = 3
108
+
109
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
110
+ s.add_runtime_dependency(%q<rainpress>, [">= 0"])
111
+ s.add_runtime_dependency(%q<packr>, [">= 0"])
112
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
113
+ s.add_development_dependency(%q<rack-test>, [">= 0.5.3"])
114
+ s.add_development_dependency(%q<yard>, [">= 0"])
115
+ else
116
+ s.add_dependency(%q<rainpress>, [">= 0"])
117
+ s.add_dependency(%q<packr>, [">= 0"])
118
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
119
+ s.add_dependency(%q<rack-test>, [">= 0.5.3"])
120
+ s.add_dependency(%q<yard>, [">= 0"])
121
+ end
122
+ else
123
+ s.add_dependency(%q<rainpress>, [">= 0"])
124
+ s.add_dependency(%q<packr>, [">= 0"])
125
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
126
+ s.add_dependency(%q<rack-test>, [">= 0.5.3"])
127
+ s.add_dependency(%q<yard>, [">= 0"])
128
+ end
129
+ end
130
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-bundles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
@@ -42,6 +42,16 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: 1.2.9
44
44
  version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rack-test
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.5.3
54
+ version:
45
55
  - !ruby/object:Gem::Dependency
46
56
  name: yard
47
57
  type: :development
@@ -68,8 +78,8 @@ files:
68
78
  - README.md
69
79
  - Rakefile
70
80
  - VERSION
71
- - lib/sinatra-bundles.rb
72
81
  - lib/sinatra/bundles.rb
82
+ - sinatra-bundles.gemspec
73
83
  - spec/app.rb
74
84
  - spec/production_app.rb
75
85
  - spec/public/javascripts/test1.js
File without changes