rack-sprocketize 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,63 +2,16 @@ require 'rack'
2
2
 
3
3
  module Rack
4
4
  class Sprocketize
5
+ autoload :Config, 'rack/sprocketize/config'
6
+ autoload :Railtie, 'rack/sprocketize/railtie'
5
7
  autoload :Sprocket, 'rack/sprocketize/sprocket'
6
8
  autoload :VERSION, 'rack/sprocketize/version'
7
9
 
8
- DEFAULT_OPTIONS = {
9
- :source_path => 'app/javascripts',
10
- :output_path => 'public/javascripts',
11
- :load_path => %w(vendor/javascripts)
12
- }.freeze
13
-
14
- class << self
15
- attr_accessor :options, :compression_options, :source_path, :output_path, :always_check, :always_compress
16
-
17
- def configure(options = {})
18
- self.options = DEFAULT_OPTIONS.dup.merge(options)
19
- self.compression_options = self.options.delete(:compression_options) || {}
20
- self.source_path = ::File.expand_path self.options.delete(:source_path)
21
- self.output_path = ::File.expand_path self.options.delete(:output_path)
22
-
23
- self.always_check = if self.options.key?(:always_check)
24
- self.options.delete(:always_check)
25
- else
26
- self.environment == 'development'
27
- end
28
-
29
- self.always_compress = if self.options.key?(:always_compress)
30
- self.options.delete(:always_compress)
31
- else
32
- self.environment == 'production'
33
- end
34
- end
35
-
36
- def environment
37
- if defined?(RAILS_ENV)
38
- RAILS_ENV # Rails 2
39
- elsif defined?(Rails) && defined?(Rails.env)
40
- Rails.env.to_s # Rails 3
41
- elsif defined?(@app.settings) && defined?(@app.settings.environment)
42
- @app.settings.environment # Sinatra
43
- elsif ENV.key?('RACK_ENV')
44
- ENV['RACK_ENV']
45
- else
46
- 'development'
47
- end
48
- end
49
-
50
- def always_check?
51
- !!@always_check
52
- end
53
-
54
- def always_compress?
55
- !!@always_compress
56
- end
57
- end
10
+ attr_accessor :config
58
11
 
59
12
  def initialize(app, options = {})
60
- @app = app
61
- Sprocketize.configure(options)
13
+ @app = app
14
+ @config = Config.new(options)
62
15
  end
63
16
 
64
17
  def call(env)
@@ -70,19 +23,19 @@ module Rack
70
23
  protected
71
24
 
72
25
  def skip?
73
- return false if Sprocketize.always_check?
26
+ return false if config.always_check?
74
27
  @sprocketized && @request.params['sprocketize'].nil?
75
28
  end
76
29
 
77
30
  def source_files
78
- files = Dir.glob ::File.join(Sprocketize.source_path, '**/*.js')
31
+ files = Dir.glob ::File.join(config.source_path, '**/*.js')
79
32
  files.reject! { |file| ::File.basename(file) =~ /^_/ }
80
33
  files
81
34
  end
82
35
 
83
36
  def sprocketize
84
37
  source_files.each do |source_file|
85
- sprocket = Sprocket.new(source_file)
38
+ sprocket = Sprocket.new(source_file, config)
86
39
  sprocket.sprocketize if sprocket.stale?
87
40
  end
88
41
  @sprocketized = true
@@ -0,0 +1,60 @@
1
+ require 'pathname'
2
+ require 'valuable'
3
+
4
+ module Rack
5
+ class Sprocketize
6
+ # Configuration options for Rack::Sprocketize.
7
+ class Config < Valuable
8
+ def initialize(*args)
9
+ super
10
+ self.always_compress = environment == 'production' if always_compress.nil?
11
+ self.always_check = environment == 'development' if always_check.nil?
12
+ end
13
+
14
+ # Options that will be passed directly to Sprockets.
15
+ #
16
+ # Default: { :load_path => %w(vendor/javascripts) }
17
+ has_value :sprockets, :default => { :load_path => %w(vendor/javascripts) }
18
+
19
+ # Options that will be passed to the javascript compressor, if used.
20
+ #
21
+ # Default: {}
22
+ has_value :compression, :default => {}
23
+
24
+ # Enables javascript compression.
25
+ #
26
+ # Default: true in production environments, false otherwise
27
+ has_value :always_compress, :klass => :boolean
28
+
29
+ # When enabled, the javascripts will be checked for changes on every request.
30
+ #
31
+ # Default: true in development environments, false otherwise
32
+ has_value :always_check, :klass => :boolean
33
+
34
+ # Path to directory where the source javascripts are located.
35
+ #
36
+ # Default: 'app/javascripts'
37
+ has_value :source_path, :default => 'app/javascripts'
38
+
39
+ # Path to the directory where the sprocketized javascripts should be output.
40
+ #
41
+ # Default: 'public/javascripts'
42
+ has_value :output_path, :default => 'public/javascripts'
43
+
44
+ # Determines which environment we are currently in.
45
+ #
46
+ # Default: 'development'
47
+ def environment
48
+ if defined?(RAILS_ENV)
49
+ RAILS_ENV # Rails 2
50
+ elsif defined?(Rails) && defined?(Rails.env)
51
+ Rails.env.to_s # Rails 3
52
+ elsif ENV.key?('RACK_ENV')
53
+ ENV['RACK_ENV']
54
+ else
55
+ 'development'
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -4,10 +4,13 @@ require 'sprockets'
4
4
  module Rack
5
5
  class Sprocketize
6
6
  class Sprocket
7
- def initialize(source_file)
7
+ attr_accessor :source_file, :output_path, :config, :secretary
8
+
9
+ def initialize(source_file, config)
8
10
  @source_file = source_file
9
- @output_path = @source_file.sub /^#{Regexp.escape(Sprocketize.source_path)}/, Sprocketize.output_path
10
- @secretary = Sprockets::Secretary.new Sprocketize.options.merge(:source_files => [ @source_file ])
11
+ @config = config
12
+ @output_path = source_file.sub /^#{Regexp.escape(config.source_path)}/, config.output_path
13
+ @secretary = Sprockets::Secretary.new config.sprockets.merge(:source_files => [ @source_file ])
11
14
  end
12
15
 
13
16
  def compress(output)
@@ -25,18 +28,18 @@ module Rack
25
28
  end
26
29
 
27
30
  def concat
28
- @secretary.concatenation.to_s
31
+ secretary.concatenation.to_s
29
32
  end
30
33
 
31
34
  def stale?
32
- !::File.exist?(@output_path) || @secretary.source_last_modified > ::File.mtime(@output_path)
35
+ !::File.exist?(output_path) || secretary.source_last_modified > ::File.mtime(output_path)
33
36
  end
34
37
 
35
38
  def sprocketize
36
- FileUtils.mkdir_p ::File.dirname(@output_path)
37
- ::File.open(@output_path, 'w') do |file|
39
+ FileUtils.mkdir_p ::File.dirname(output_path)
40
+ ::File.open(output_path, 'w') do |file|
38
41
  output = concat
39
- output = compress(output) if Sprocketize.always_compress? && !already_compressed?
42
+ output = compress(output) if config.always_compress? && !already_compressed?
40
43
  file.write(output.strip)
41
44
  end
42
45
  end
@@ -48,7 +51,7 @@ module Rack
48
51
  protected
49
52
 
50
53
  def compression_options(defaults = {})
51
- defaults.merge Sprocketize.compression_options
54
+ defaults.merge(config.compression)
52
55
  end
53
56
  end
54
57
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Sprocketize
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = %w(Pete Browne)
10
10
  s.email = %w(me@petebrowne.com)
11
- s.homepage = ''
11
+ s.homepage = 'http://github.com/petebrowne/rack-sprocketize'
12
12
  s.summary = %(Rack::Sprocketize is a piece of Rack Middleware which uses Sprockets to concatenate javascript files and then optionally compresses them.)
13
13
  s.description = %(Rack::Sprocketize is a piece of Rack Middleware which uses Sprockets to concatenate javascript files and then optionally compresses them. In a development environment, the files will be sprocketized on each request if there have been changes to the source files. In a production environment, the files will only be sprocketized one time, and only if there have been changes. Also, in a production environment, the files will be compressed by whichever javascript compressor is available.)
14
14
 
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency 'rack', '~> 1.2.1'
18
18
  s.add_dependency 'sprockets', '~> 1.0.2'
19
+ s.add_dependency 'valuable', '~> 0.8.5'
19
20
  s.add_development_dependency 'rspec', '~> 2.5.0'
20
21
  s.add_development_dependency 'test-construct', '~> 1.2.0'
21
22
  s.add_development_dependency 'jsmin', '~> 1.0.1'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-sprocketize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pete
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-16 00:00:00 -05:00
19
+ date: 2011-03-17 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -52,9 +52,25 @@ dependencies:
52
52
  type: :runtime
53
53
  version_requirements: *id002
54
54
  - !ruby/object:Gem::Dependency
55
- name: rspec
55
+ name: valuable
56
56
  prerelease: false
57
57
  requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 53
63
+ segments:
64
+ - 0
65
+ - 8
66
+ - 5
67
+ version: 0.8.5
68
+ type: :runtime
69
+ version_requirements: *id003
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ prerelease: false
73
+ requirement: &id004 !ruby/object:Gem::Requirement
58
74
  none: false
59
75
  requirements:
60
76
  - - ~>
@@ -66,11 +82,11 @@ dependencies:
66
82
  - 0
67
83
  version: 2.5.0
68
84
  type: :development
69
- version_requirements: *id003
85
+ version_requirements: *id004
70
86
  - !ruby/object:Gem::Dependency
71
87
  name: test-construct
72
88
  prerelease: false
73
- requirement: &id004 !ruby/object:Gem::Requirement
89
+ requirement: &id005 !ruby/object:Gem::Requirement
74
90
  none: false
75
91
  requirements:
76
92
  - - ~>
@@ -82,11 +98,11 @@ dependencies:
82
98
  - 0
83
99
  version: 1.2.0
84
100
  type: :development
85
- version_requirements: *id004
101
+ version_requirements: *id005
86
102
  - !ruby/object:Gem::Dependency
87
103
  name: jsmin
88
104
  prerelease: false
89
- requirement: &id005 !ruby/object:Gem::Requirement
105
+ requirement: &id006 !ruby/object:Gem::Requirement
90
106
  none: false
91
107
  requirements:
92
108
  - - ~>
@@ -98,11 +114,11 @@ dependencies:
98
114
  - 1
99
115
  version: 1.0.1
100
116
  type: :development
101
- version_requirements: *id005
117
+ version_requirements: *id006
102
118
  - !ruby/object:Gem::Dependency
103
119
  name: packr
104
120
  prerelease: false
105
- requirement: &id006 !ruby/object:Gem::Requirement
121
+ requirement: &id007 !ruby/object:Gem::Requirement
106
122
  none: false
107
123
  requirements:
108
124
  - - ~>
@@ -114,11 +130,11 @@ dependencies:
114
130
  - 0
115
131
  version: 3.1.0
116
132
  type: :development
117
- version_requirements: *id006
133
+ version_requirements: *id007
118
134
  - !ruby/object:Gem::Dependency
119
135
  name: yui-compressor
120
136
  prerelease: false
121
- requirement: &id007 !ruby/object:Gem::Requirement
137
+ requirement: &id008 !ruby/object:Gem::Requirement
122
138
  none: false
123
139
  requirements:
124
140
  - - ~>
@@ -130,11 +146,11 @@ dependencies:
130
146
  - 4
131
147
  version: 0.9.4
132
148
  type: :development
133
- version_requirements: *id007
149
+ version_requirements: *id008
134
150
  - !ruby/object:Gem::Dependency
135
151
  name: closure-compiler
136
152
  prerelease: false
137
- requirement: &id008 !ruby/object:Gem::Requirement
153
+ requirement: &id009 !ruby/object:Gem::Requirement
138
154
  none: false
139
155
  requirements:
140
156
  - - ~>
@@ -146,7 +162,7 @@ dependencies:
146
162
  - 0
147
163
  version: 1.0.0
148
164
  type: :development
149
- version_requirements: *id008
165
+ version_requirements: *id009
150
166
  description: Rack::Sprocketize is a piece of Rack Middleware which uses Sprockets to concatenate javascript files and then optionally compresses them. In a development environment, the files will be sprocketized on each request if there have been changes to the source files. In a production environment, the files will only be sprocketized one time, and only if there have been changes. Also, in a production environment, the files will be compressed by whichever javascript compressor is available.
151
167
  email:
152
168
  - me@petebrowne.com
@@ -164,6 +180,7 @@ files:
164
180
  - Rakefile
165
181
  - lib/rack-sprocketize.rb
166
182
  - lib/rack/sprocketize.rb
183
+ - lib/rack/sprocketize/config.rb
167
184
  - lib/rack/sprocketize/railtie.rb
168
185
  - lib/rack/sprocketize/sprocket.rb
169
186
  - lib/rack/sprocketize/version.rb
@@ -171,7 +188,7 @@ files:
171
188
  - spec/rack/sprocketize_spec.rb
172
189
  - spec/spec_helper.rb
173
190
  has_rdoc: true
174
- homepage: ""
191
+ homepage: http://github.com/petebrowne/rack-sprocketize
175
192
  licenses: []
176
193
 
177
194
  post_install_message: