classy_assets 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d46c96e739ac209aa3be084cb29edc50b128add9
4
- data.tar.gz: aadd3440b39e892a6274140698bbe05324c8cf22
3
+ metadata.gz: e9ec6db00dd5b915529cd34cdd6e6bb446b349bc
4
+ data.tar.gz: bfe108e29e8043150a6eafac96f62d90b739b69c
5
5
  SHA512:
6
- metadata.gz: 80185477cad1aadc5f8d472bae056b27a66454bf145c2604f5c987a39eabec5d9b40ebf4792e5cbdd59abd40d24ee197162e03a18260d3f7b733156f81f42817
7
- data.tar.gz: 22d8aef7596fa473f83907f5092832e4757b5a8b392d2a67ea185f8cfc4999a3f5e66a9553724184af3df802253388472e177ccb58cca64258934d55c521f138
6
+ metadata.gz: b744c30940d2b046e742e0a3c1ff0b44a428dc67a71df34edbe581ab5c9354bf36820c79ec4bf1e1182392db9deedb7d3841066bc0a4dfe32673bcc4035e5ff8
7
+ data.tar.gz: 6cf13ce5ed8a463696de6533e90229eead7633228e5887bc5fbbe9ed276c46a75c8d9b91135f51875ed5ea71566de3a438b00c755d165f8c46c7ac530da0bae0
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singleton'
4
+
5
+ module ClassyAssets
6
+ class Config
7
+ include Singleton
8
+
9
+ attr_accessor :asset_debug, :asset_digest, :asset_host, :asset_paths, :asset_prefix, :asset_root
10
+
11
+ def asset_debug
12
+ @asset_debug ||= (ENV['RACK_ENV'] == 'development')
13
+ end
14
+
15
+ def asset_digest
16
+ @asset_digest ||= false
17
+ end
18
+
19
+ def asset_paths
20
+ @asset_paths ||= build_asset_paths
21
+ @asset_paths.uniq! # ensure no duplicates
22
+ @asset_paths
23
+ end
24
+
25
+ def asset_prefix
26
+ @asset_prefix ||= 'assets'
27
+ end
28
+
29
+ def asset_root
30
+ raise Errors::NilAssetRoot.new if @asset_root.nil?
31
+ @asset_root
32
+ end
33
+
34
+ def public_path
35
+ @public_path ||= File.join(asset_root, 'public')
36
+ end
37
+
38
+ private
39
+
40
+ def build_asset_paths
41
+ Dir.glob(File.join(asset_root, asset_prefix, '*'))
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ module ClassyAssets
4
+ module Errors
5
+ class NilAssetRoot < StandardError
6
+ def message
7
+ "ClassyAssets.config.asset_root is nil, and must be configured."
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'singleton'
4
+ require 'sprockets'
5
+
6
+ module ClassyAssets
7
+ class Sprockets
8
+ include Singleton
9
+
10
+ def environment
11
+ asset_root = ClassyAssets.config.asset_root
12
+ @environment = ::Sprockets::Environment.new(asset_root)
13
+
14
+ ClassyAssets.config.asset_paths.each do |asset_path|
15
+ @environment.append_path asset_path
16
+ end
17
+
18
+ @environment.context_class.class_eval do
19
+ def asset_path(path, options = {})
20
+ ClassyAssets.asset_url_for(path)
21
+ end
22
+ end
23
+
24
+ @environment
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module ClassyAssets
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/classy_assets.rb CHANGED
@@ -12,21 +12,36 @@ require 'sinatra/base'
12
12
  require 'sprockets'
13
13
 
14
14
  # classy assets components
15
+ require 'classy_assets/config'
15
16
  require 'classy_assets/configuration'
17
+ require 'classy_assets/errors/nil_asset_root'
16
18
  require 'classy_assets/sass/script/functions'
19
+ require 'classy_assets/sprockets'
17
20
  require 'classy_assets/sprockets/sass_importer'
18
21
  require 'classy_assets/version'
19
22
 
20
- # Utility methods
21
23
  module ClassyAssets
24
+ # singleton sprockets environment
25
+ def self.sprockets
26
+ ClassyAssets::Sprockets.instance.environment
27
+ end
28
+
29
+ # singleton configuration
30
+ def self.config(&block)
31
+ yield ClassyAssets::Config.instance if block_given?
32
+ ClassyAssets::Config.instance
33
+ end
34
+
35
+ # utility methods
36
+
22
37
  def self.asset_url_for(asset)
23
- asset = Configuration.sprockets[asset].send(determine_path_type)
24
- debug = (Configuration.debug_mode) ? '?body=1' : ''
25
- "#{Configuration.asset_host}/#{Configuration.asset_prefix}/#{asset}#{debug}"
38
+ asset = sprockets[asset].send(determine_path_type)
39
+ debug = (config.asset_debug) ? '?body=1' : ''
40
+ "#{config.asset_host}/#{config.asset_prefix}/#{asset}#{debug}"
26
41
  end
27
42
 
28
43
  def self.asset_data_uri_for(asset)
29
- asset = Configuration.sprockets[asset]
44
+ asset = sprockets[asset]
30
45
  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
31
46
  "data:#{asset.content_type};base64,#{EscapeUtils.escape_url(base64)}"
32
47
  end
@@ -35,12 +50,14 @@ module ClassyAssets
35
50
  sources = [sources] unless sources.is_a? Array
36
51
  sources.map do |source|
37
52
  source = "#{source}.#{ext}" unless source =~ /\.#{ext}$/
38
- asset_url = (source =~ /\/|http/) ? source : ::ClassyAssets.asset_url_for(source)
53
+ asset_url = (source =~ /\/|http/) ? source : asset_url_for(source)
39
54
  yield(asset_url)
40
55
  end.join('')
41
56
  end
42
57
 
58
+ private
59
+
43
60
  def self.determine_path_type
44
- (Configuration.asset_digest) ? :digest_path : :logical_path
61
+ (config.asset_digest) ? :digest_path : :logical_path
45
62
  end
46
63
  end
@@ -1,16 +1,20 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'classy_assets'
4
+ require 'rack/builder'
4
5
 
5
6
  module Rack
6
- class ClassyAssets < Sinatra::Base
7
- ::ClassyAssets::Configuration.sprockets.mime_types.each do |ext, content_type|
8
- get "/#{::ClassyAssets::Configuration.asset_prefix}/*#{ext}" do |asset|
9
- filename = "#{asset}#{ext}"
10
- filename.gsub!(/-(\h{32})#{ext}$/, ext) if ::ClassyAssets::Configuration.asset_digest
11
- content_type content_type
12
- ::ClassyAssets::Configuration.sprockets[filename] || halt(404)
13
- end
14
- end
7
+ class ClassyAssets
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ if env['PATH_INFO'] =~ /\/#{::ClassyAssets.config.asset_prefix}\//
14
+ Rack::URLMap.new("/#{::ClassyAssets.config.asset_prefix}" => ::ClassyAssets.sprockets).call(env)
15
+ else
16
+ @app.call(env)
17
+ end
18
+ end
15
19
  end
16
20
  end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ClassyAssets::Config do
6
+ before do
7
+ @asset_root = File.expand_path('../../support', __FILE__)
8
+ @asset_host = 'http://example.com'
9
+ @asset_prefix = 'assets'
10
+
11
+ @asset_paths = Dir.glob(File.join(@asset_root, @asset_prefix, '*'))
12
+ @vendor_path = File.expand_path('../../support/vendor', __FILE__)
13
+ @asset_paths << @vendor_path
14
+
15
+ ClassyAssets.config do |config|
16
+ config.asset_root = @asset_root # must be configured first
17
+ config.asset_debug = true
18
+ config.asset_digest = true
19
+ config.asset_host = @asset_host
20
+ config.asset_paths << @vendor_path
21
+ end
22
+
23
+ @configuration = ClassyAssets.config
24
+ end
25
+
26
+ it "returns the configured asset_debug" do
27
+ @configuration.asset_debug.must_equal true
28
+ end
29
+
30
+ it "returns the configured asset_digest" do
31
+ @configuration.asset_digest.must_equal true
32
+ end
33
+
34
+ it "returns the configured asset_host" do
35
+ @configuration.asset_host.must_equal @asset_host
36
+ end
37
+
38
+ it "returns the configured asset_prefix" do
39
+ @configuration.asset_prefix.must_equal @asset_prefix
40
+ end
41
+
42
+ it "returns the configured asset_root" do
43
+ @configuration.asset_root.must_equal @asset_root
44
+ end
45
+
46
+ it "returns the configured asset_paths" do
47
+ @configuration.asset_paths.must_equal @asset_paths
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'spec_config_helper'
5
+
6
+ describe ClassyAssets::Sprockets do
7
+ before do
8
+ asset_root = ClassyAssets.config.asset_root
9
+ asset_prefix = ClassyAssets.config.asset_prefix
10
+
11
+ @asset_paths = Dir.glob(File.join(asset_root, asset_prefix, '*'))
12
+ @sprockets = ClassyAssets.sprockets
13
+ end
14
+
15
+ it "returns the sprockets environment" do
16
+ @sprockets.must_be_kind_of Sprockets::Environment
17
+ end
18
+
19
+ it "returns the correct asset paths" do
20
+ @sprockets.paths.must_equal @asset_paths
21
+ end
22
+ end
@@ -1,14 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
+ require 'spec_config_helper'
4
5
  require 'rack/classy_assets'
5
6
 
6
7
  class ClassyApp < Sinatra::Base
7
- configure do
8
- ClassyAssets::Configuration.configure do |config|
9
- config.root_path = File.expand_path('../../support', __FILE__)
10
- end
11
- end
12
8
  use Rack::ClassyAssets
13
9
  end
14
10
 
@@ -16,7 +12,7 @@ def app
16
12
  ClassyApp.new
17
13
  end
18
14
 
19
- describe Rack::ClassyAssets do
15
+ describe Rack::ClassyAssets do
20
16
  context "stylesheets" do
21
17
  context "css" do
22
18
  it "will respond sucessfully" do
@@ -1,47 +1,47 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
+ require 'spec_config_helper'
4
5
  require 'sinatra/classy_assets'
5
6
 
6
- class ClassyApp < Sinatra::Base
7
- configure do
8
- ClassyAssets::Configuration.configure do |config|
9
- config.root_path = File.expand_path('../../support', __FILE__)
10
- end
11
- end
7
+ class ClassySinatraApp < Sinatra::Base
12
8
  register Sinatra::ClassyAssets
13
9
 
14
- get "/with_css" do
15
- slim :index, layout: :layout_css
10
+ get "/stylesheet_tag" do
11
+ slim :stylesheet_tag
16
12
  end
17
13
 
18
- get "/with_media_option" do
19
- slim :index, layout: :layout_media
14
+ get "/stylesheet_tag_media" do
15
+ slim :stylesheet_tag_media
20
16
  end
21
17
 
22
- get "/with_js" do
23
- slim :index, layout: :layout_js
18
+ get "/javascript_tag" do
19
+ slim :javascript_tag
24
20
  end
25
21
 
26
- template :layout_css do
27
- "html\n\thead\n\t\t==stylesheet_tag('application')\n\tbody\n\t\t==yield\n"
22
+ template :index do
23
+ "| Classy Assets"
28
24
  end
29
25
 
30
- template :layout_media do
31
- "html\n\thead\n\t\t==stylesheet_tag('application', media: 'print')\n\tbody\n\t\t==yield\n"
26
+ template :layout do
27
+ "html\n\tbody\n\t\t==yield\n"
32
28
  end
33
29
 
34
- template :layout_js do
35
- "html\n\thead\n\tbody\n\t\t==javascript_tag('application')\n==yield\n"
30
+ template :stylesheet_tag do
31
+ "==stylesheet_tag('application')"
36
32
  end
37
33
 
38
- template :index do
39
- "| Classy Assets"
34
+ template :stylesheet_tag_media do
35
+ "==stylesheet_tag('application', media: 'print')"
36
+ end
37
+
38
+ template :javascript_tag do
39
+ "==javascript_tag('application')"
40
40
  end
41
41
  end
42
42
 
43
43
  def app
44
- ClassyApp.new
44
+ ClassySinatraApp.new
45
45
  end
46
46
 
47
47
  describe Sinatra::ClassyAssets do
@@ -54,112 +54,25 @@ describe Sinatra::ClassyAssets do
54
54
 
55
55
  context "Helpers" do
56
56
  it "outputs a stylesheet link tag" do
57
- get "/with_css"
57
+ get "/stylesheet_tag"
58
58
  last_response.body.must_match(/<link href="\/assets\/application.css" media="screen" rel="stylesheet">/)
59
59
  end
60
60
 
61
61
  it "outputs the stylesheet tag with the correct media attribute" do
62
- get "/with_media_option"
62
+ get "/stylesheet_tag_media"
63
63
  last_response.body.must_match(/<link href="\/assets\/application.css" media="print" rel="stylesheet">/)
64
64
  end
65
65
 
66
66
  it "outputs a javascript tag" do
67
- get "/with_js"
67
+ get "/javascript_tag"
68
68
  last_response.body.must_match(/<script src="\/assets\/application.js"><\/script>/)
69
69
  end
70
70
  end
71
71
 
72
72
  context "uses Rack::ClassyAssets middleware" do
73
- describe "stylesheets" do
74
- describe "css" do
75
- it "will respond sucessfully" do
76
- get '/assets/stylesheet.css'
77
- last_response.status.must_equal 200
78
- end
79
- end
80
-
81
- describe "sass" do
82
- it "will respond sucessfully" do
83
- get '/assets/application.css'
84
- last_response.status.must_equal 200
85
- end
86
- end
87
- end
88
-
89
- describe "javascripts" do
90
- describe "js" do
91
- it "will respond sucessfully" do
92
- get '/assets/javascript.js'
93
- last_response.status.must_equal 200
94
- end
95
- end
96
-
97
- describe "coffeescript" do
98
- it "will respond sucessfully" do
99
- get '/assets/application.js'
100
- last_response.status.must_equal 200
101
- end
102
- end
103
- end
104
-
105
- describe "images" do
106
- describe "gif" do
107
- it "will respond sucessfully" do
108
- get '/assets/image.gif'
109
- last_response.status.must_equal 200
110
- end
111
- end
112
-
113
- describe "jpg" do
114
- it "will respond sucessfully" do
115
- get '/assets/image.jpg'
116
- last_response.status.must_equal 200
117
- end
118
- end
119
-
120
- describe "png" do
121
- it "will respond sucessfully" do
122
- get '/assets/image.png'
123
- last_response.status.must_equal 200
124
- end
125
- end
126
- end
127
-
128
- describe "fonts" do
129
- describe "eot" do
130
- it "will respond sucessfully" do
131
- get '/assets/font.eot'
132
- last_response.status.must_equal 200
133
- end
134
- end
135
-
136
- describe "otf" do
137
- it "will respond sucessfully" do
138
- get '/assets/font.otf'
139
- last_response.status.must_equal 200
140
- end
141
- end
142
-
143
- describe "svg" do
144
- it "will respond sucessfully" do
145
- get '/assets/font.svg'
146
- last_response.status.must_equal 200
147
- end
148
- end
149
-
150
- describe "ttf" do
151
- it "will respond sucessfully" do
152
- get '/assets/font.ttf'
153
- last_response.status.must_equal 200
154
- end
155
- end
156
-
157
- describe "woff" do
158
- it "will respond sucessfully" do
159
- get '/assets/font.woff'
160
- last_response.status.must_equal 200
161
- end
162
- end
73
+ it "requests for assets will respond sucessfully" do
74
+ get '/assets/stylesheet.css'
75
+ last_response.status.must_equal 200
163
76
  end
164
77
  end
165
78
  end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ ClassyAssets.config do |config|
4
+ config.asset_root = File.expand_path('../support', __FILE__)
5
+ end
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classy_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - StyleSeek Engineering
@@ -183,18 +183,22 @@ files:
183
183
  - classy_assets.gemspec
184
184
  - lib/classy_assets.rb
185
185
  - lib/classy_assets/cli.rb
186
- - lib/classy_assets/configuration.rb
186
+ - lib/classy_assets/config.rb
187
+ - lib/classy_assets/errors/nil_asset_root.rb
187
188
  - lib/classy_assets/sass/script/functions.rb
189
+ - lib/classy_assets/sprockets.rb
188
190
  - lib/classy_assets/sprockets/sass_importer.rb
189
191
  - lib/classy_assets/tasks.rb
190
192
  - lib/classy_assets/tasks/assets.rake
191
193
  - lib/classy_assets/version.rb
192
194
  - lib/rack/classy_assets.rb
193
195
  - lib/sinatra/classy_assets.rb
194
- - spec/classy_assets/configuration_spec.rb
196
+ - spec/classy_assets/config_spec.rb
197
+ - spec/classy_assets/sprockets_spec.rb
195
198
  - spec/classy_assets_spec.rb
196
199
  - spec/rack/classy_assets_spec.rb
197
200
  - spec/sinatra/classy_assets_spec.rb
201
+ - spec/spec_config_helper.rb
198
202
  - spec/spec_helper.rb
199
203
  - spec/support/assets/fonts/font.eot
200
204
  - spec/support/assets/fonts/font.otf
@@ -208,6 +212,7 @@ files:
208
212
  - spec/support/assets/javascripts/javascript.js
209
213
  - spec/support/assets/stylesheets/application.scss
210
214
  - spec/support/assets/stylesheets/stylesheet.css
215
+ - spec/support/vendor/.gitkeep
211
216
  homepage: https://github.com/styleseek/classy_assets
212
217
  licenses:
213
218
  - MIT
@@ -233,10 +238,12 @@ signing_key:
233
238
  specification_version: 4
234
239
  summary: Classy Assets (powered by Sprockets) for Sinatra and Rack apps
235
240
  test_files:
236
- - spec/classy_assets/configuration_spec.rb
241
+ - spec/classy_assets/config_spec.rb
242
+ - spec/classy_assets/sprockets_spec.rb
237
243
  - spec/classy_assets_spec.rb
238
244
  - spec/rack/classy_assets_spec.rb
239
245
  - spec/sinatra/classy_assets_spec.rb
246
+ - spec/spec_config_helper.rb
240
247
  - spec/spec_helper.rb
241
248
  - spec/support/assets/fonts/font.eot
242
249
  - spec/support/assets/fonts/font.otf
@@ -250,3 +257,4 @@ test_files:
250
257
  - spec/support/assets/javascripts/javascript.js
251
258
  - spec/support/assets/stylesheets/application.scss
252
259
  - spec/support/assets/stylesheets/stylesheet.css
260
+ - spec/support/vendor/.gitkeep
@@ -1,92 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module ClassyAssets
4
- class Configuration
5
- def self.configure
6
- yield self
7
- end
8
-
9
- def self.asset_digest
10
- @asset_digest.nil? ? false : @asset_digest
11
- end
12
-
13
- def self.asset_digest=(digest)
14
- @asset_digest = digest
15
- end
16
-
17
- def self.asset_host
18
- @asset_host
19
- end
20
-
21
- def self.asset_host=(host)
22
- @asset_host = host
23
- end
24
-
25
- def self.asset_paths
26
- @asset_paths || build_asset_paths
27
- end
28
-
29
- def self.asset_paths=(paths)
30
- @asset_paths = paths.to_a
31
- end
32
-
33
- def self.asset_prefix
34
- @asset_prefix || 'assets'
35
- end
36
-
37
- def self.asset_prefix=(prefix)
38
- @asset_prefix = prefix
39
- end
40
-
41
- def self.debug_mode
42
- @debug_mode.nil? ? (ENV['RACK_ENV'] == 'development') : @debug_mode
43
- end
44
-
45
- def self.debug_mode=(debug)
46
- @debug_mode = debug
47
- end
48
-
49
- def self.public_path
50
- @public_path || File.join(root_path, 'public')
51
- end
52
-
53
- def self.public_path=(path)
54
- @public_path = path
55
- end
56
-
57
- def self.root_path
58
- @root_path || '.'
59
- end
60
-
61
- def self.root_path=(path)
62
- @root_path = path
63
- end
64
-
65
- def self.sprockets
66
- new_sprockets_environment
67
- end
68
-
69
- private
70
-
71
- def self.build_asset_paths
72
- Dir.glob(File.join(root_path, asset_prefix, '*'))
73
- end
74
-
75
- def self.new_sprockets_environment
76
- @sprockets = ::Sprockets::Environment.new(root_path)
77
- asset_paths.each do |asset_path|
78
- unless @sprockets.paths.include?(asset_path)
79
- @sprockets.append_path asset_path
80
- end
81
- end
82
-
83
- @sprockets.context_class.class_eval do
84
- def asset_path(path, options = {})
85
- ClassyAssets.asset_url_for(path)
86
- end
87
- end
88
-
89
- @sprockets
90
- end
91
- end
92
- end
@@ -1,52 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
- require 'classy_assets'
5
-
6
- describe ClassyAssets::Configuration do
7
- before do
8
- @root_path = File.expand_path('../support', __FILE__)
9
- ClassyAssets::Configuration.configure do |config|
10
- config.root_path = @root_path
11
- end
12
- @configuration = ClassyAssets::Configuration
13
- @public_path = File.expand_path('../support/public', __FILE__)
14
- @asset_paths = Dir.glob(File.join(@configuration.root_path, @configuration.asset_prefix, '*'))
15
- end
16
-
17
- it "returns the configured root_path" do
18
- @configuration.root_path.must_equal @root_path
19
- end
20
-
21
- it "returns the computed public_path" do
22
- @configuration.public_path.must_equal @public_path
23
- end
24
-
25
- it "returns an array of asset paths" do
26
- @configuration.asset_paths.must_equal @asset_paths
27
- end
28
-
29
- it "returns the asset digest setting" do
30
- @configuration.asset_digest.must_equal false
31
- end
32
-
33
- it "returns the asset host" do
34
- @configuration.asset_host.must_equal nil
35
- end
36
-
37
- it "returns the asset prefix" do
38
- @configuration.asset_prefix.must_equal 'assets'
39
- end
40
-
41
- it "returns the debug mode setting" do
42
- @configuration.debug_mode.must_equal false
43
- end
44
-
45
- it "returns the sprockets environment" do
46
- @configuration.sprockets.must_be_kind_of Sprockets::Environment
47
- end
48
-
49
- it "returns the correct paths" do
50
- @configuration.sprockets.paths.must_equal @configuration.asset_paths
51
- end
52
- end