rack-pack 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,51 +7,79 @@ module Rack
7
7
  autoload :Stylesheet, 'rack/pack/stylesheet'
8
8
  autoload :Version, 'rack/pack/version'
9
9
 
10
- DEFAULT_OPTIONS = {
10
+ class << self
11
+ attr_accessor :packages, :environment, :options
12
+ end
13
+
14
+ # Setup the defaults
15
+ @packages = {}
16
+ @environment = 'development'
17
+ @options = {
11
18
  :public_dir => 'public',
19
+ :add_default_packages => true,
12
20
  :always_update => false,
13
21
  :always_compress => false,
14
22
  :js_compression => {},
15
- :css_compression => {},
16
- :environment => :development,
17
- :add_default_packages => true
18
- }.freeze
23
+ :css_compression => {}
24
+ }
19
25
 
20
26
  class << self
21
- attr_accessor :packages, :options, :environment
22
-
27
+ # Sets up Rack::Pack, optionally adding packages.
28
+ #
29
+ # @param [Hash] options
30
+ # @option options [String] :public_dir ('public') The directory to output the packaged file.
31
+ # @option options [Boolean] :add_default_packages (true) Wether or not to add the default packages.
32
+ # @option options [Boolean] :always_update (false) Updates the packages on every request.
33
+ # @option options [Boolean] :always_compress (false) Compress the packages on every request.
34
+ # @option options [Hash] :js_compression Options to pass directly
35
+ # to the Javascript compression engine.
36
+ # @option options [Hash] :css_compression Options to pass directly
37
+ # to the Stylesheet compression engine.
38
+ # @option options [String] :environment Manually set the environment.
23
39
  def configure(options = {})
24
- self.packages = {}
25
- self.options = DEFAULT_OPTIONS.dup
26
-
27
40
  self.options.each_key do |key|
28
41
  self.options[key] = options.delete(key) if options.key?(key)
29
42
  end
30
43
 
31
- unless self.options[:add_default_packages] == false
32
- add_package 'javascripts/application.js', '{vendor,app,.}/javascripts/*.js'
33
- add_package 'stylesheets/application.css', '{vendor,app,.}/stylesheets/*.css'
34
- end
44
+ self.environment = options.delete(:environment) if options.key?(:environment)
45
+
46
+ add_default_packages if self.options[:add_default_packages]
35
47
 
36
48
  options.each do |to_file, from_files|
37
49
  add_package(to_file, from_files)
38
50
  end
39
51
  end
40
52
 
53
+ # Determines if we're running in a production environment.
41
54
  def production?
42
55
  self.environment.to_s == 'production'
43
56
  end
44
57
 
58
+ # Adds a new Package for Rack::Pack to update.
59
+ # The Package class is determined based on the filename.
60
+ #
61
+ # @param [String] output_file The path to the file the Package will output.
62
+ # @param [Array, String] source_files The source files to package. Can be either
63
+ # an Array of individual source files or a string that will be used in a glob.
45
64
  def add_package(output_file, source_files)
46
65
  if source_files.nil?
47
66
  packages.delete(output_file)
48
67
  else
49
- public_output_file = ::File.join(self.options[:public_dir], output_file.to_s)
68
+ public_output_file = ::File.join(options[:public_dir], output_file.to_s)
50
69
  package_class = Package[output_file]
51
70
  packages[output_file] = package_class.new(public_output_file, source_files)
52
71
  end
53
72
  end
54
73
 
74
+ # Adds the default Packages, which essentially look in the `vendor`, `app`,
75
+ # and current directories for javascripts & stylesheets and packages them into
76
+ # `javascripts/application.js` & `stylesheets/application.css`
77
+ def add_default_packages
78
+ add_package 'javascripts/application.js', '{vendor,app,.}/javascripts/*.js'
79
+ add_package 'stylesheets/application.css', '{vendor,app,.}/stylesheets/*.css'
80
+ end
81
+
82
+ # Loops through each added Package and updates them when stale.
55
83
  def update_packages
56
84
  Pack.packages.each_value do |package|
57
85
  package.update if package.stale?
@@ -59,6 +87,9 @@ module Rack
59
87
  end
60
88
  end
61
89
 
90
+ # Interface for creating the Rack::Pack middleware.
91
+ #
92
+ # @param []
62
93
  def initialize(app, options = {})
63
94
  @app = app
64
95
  Pack.configure(options)
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Pack
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
@@ -14,17 +14,24 @@ describe Rack::Pack do
14
14
  end
15
15
  alias_method :request, :request_for
16
16
 
17
+ describe '.add_default_packages' do
18
+ it 'adds default packages' do
19
+ Rack::Pack.add_default_packages
20
+ Rack::Pack.packages['javascripts/application.js'].should be_an_instance_of(Rack::Pack::Javascript)
21
+ Rack::Pack.packages['stylesheets/application.css'].should be_an_instance_of(Rack::Pack::Stylesheet)
22
+ end
23
+ end
24
+
17
25
  describe '.configure' do
18
26
  it 'sets the given options' do
19
27
  Rack::Pack.configure(:public_dir => '_site', :always_compress => true, :environment => :production)
20
28
  Rack::Pack.options.should == {
21
29
  :public_dir => '_site',
30
+ :add_default_packages => true,
22
31
  :always_update => false,
23
32
  :always_compress => true,
24
33
  :js_compression => {},
25
- :css_compression => {},
26
- :environment => :production,
27
- :add_default_packages => true
34
+ :css_compression => {}
28
35
  }
29
36
  end
30
37
 
@@ -23,13 +23,19 @@ $hidden_consts = {}
23
23
  Object.send :remove_const, const
24
24
  end
25
25
 
26
+ $default_config = {
27
+ :packages => Rack::Pack.packages,
28
+ :environment => Rack::Pack.environment,
29
+ :options => Rack::Pack.options
30
+ }
31
+
26
32
  RSpec.configure do |config|
27
33
  config.include Construct::Helpers
28
34
 
29
35
  config.after do
30
- Rack::Pack.packages = nil
31
- Rack::Pack.options = nil
32
- Rack::Pack.environment = nil
36
+ $default_config.each do |key, value|
37
+ Rack::Pack.send("#{key}=", value.dup)
38
+ end
33
39
  end
34
40
 
35
41
  def reveal_const(const)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-pack
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pete Browne