sprockets-rack 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sprockets-rack/middleware.rb +79 -0
- data/lib/sprockets-rack/rails.yml +9 -0
- data/lib/sprockets-rack/railtie.rb +13 -0
- metadata +6 -3
@@ -0,0 +1,79 @@
|
|
1
|
+
class Sprockets::Middleware
|
2
|
+
def initialize(app, options = {})
|
3
|
+
@app = app
|
4
|
+
@config_files = options.delete(:config_files) ||
|
5
|
+
options.delete(:config_file).to_a
|
6
|
+
@options = options
|
7
|
+
|
8
|
+
if @config_files.empty?
|
9
|
+
if @options.include? :root
|
10
|
+
if File.exist? default_config_file
|
11
|
+
@config_files = [default_config_file]
|
12
|
+
end
|
13
|
+
else
|
14
|
+
raise ArgumentError, "Sprockets::Middleware needs :root option."
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
if env['PATH_INFO'] == configuration[:path]
|
21
|
+
response
|
22
|
+
else
|
23
|
+
@app.call(env)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def default_config_file
|
30
|
+
File.join(@options[:root], 'sprockets.yml')
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.def_cached(name, &block)
|
34
|
+
if ENV['RACK_ENV'] == 'development'
|
35
|
+
define_method(name, &block)
|
36
|
+
else
|
37
|
+
content = nil
|
38
|
+
define_method(name) { content ||= instance_eval(&block) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def_cached :response do
|
43
|
+
[200, headers, content]
|
44
|
+
end
|
45
|
+
|
46
|
+
def headers
|
47
|
+
{
|
48
|
+
'Cache-Control' => 'public, max-age=86400',
|
49
|
+
'Content-Type' => 'text/javascript',
|
50
|
+
'Content-Length' => content.size.to_s
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def content
|
55
|
+
Sprockets::Secretary.new(configuration).concatenation.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
def_cached :configuration do
|
59
|
+
default_configuration.merge(normalized_configuration)
|
60
|
+
end
|
61
|
+
|
62
|
+
def default_configuration
|
63
|
+
{ :path => '/sprockets.js', :source_files => ['*.js'] }
|
64
|
+
end
|
65
|
+
|
66
|
+
def normalized_configuration
|
67
|
+
normalize(file_configuration.merge(@options))
|
68
|
+
end
|
69
|
+
|
70
|
+
def normalize(options)
|
71
|
+
Hash[options.map { |key, value| [key.to_sym, value] }]
|
72
|
+
end
|
73
|
+
|
74
|
+
def file_configuration
|
75
|
+
@config_files.inject({}) do |configuration, file_name|
|
76
|
+
configuration.merge(YAML.load(File.read(file_name)))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Sprockets::Railtie < Rails::Railtie
|
2
|
+
initializer "sprockets" do |app|
|
3
|
+
default_config_file = File.join(File.dirname(__FILE__), 'rails.yml')
|
4
|
+
user_config_file = Rails.root.join('config', 'sprockets.yml')
|
5
|
+
|
6
|
+
config_files = [default_config_file]
|
7
|
+
config_files << user_config_file if File.exist?(user_config_file)
|
8
|
+
|
9
|
+
app.middleware.use Sprockets::Middleware,
|
10
|
+
:root => Rails.root,
|
11
|
+
:config_files => config_files
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel Brockman
|
@@ -57,6 +57,9 @@ extra_rdoc_files: []
|
|
57
57
|
|
58
58
|
files:
|
59
59
|
- lib/sprockets-rack.rb
|
60
|
+
- lib/sprockets-rack/middleware.rb
|
61
|
+
- lib/sprockets-rack/rails.yml
|
62
|
+
- lib/sprockets-rack/railtie.rb
|
60
63
|
has_rdoc: true
|
61
64
|
homepage: http://github.com/dbrock/sprockets-rack
|
62
65
|
licenses: []
|