Class: Rack::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/pack.rb,
lib/rack/pack/package.rb,
lib/rack/pack/version.rb,
lib/rack/pack/javascript.rb,
lib/rack/pack/stylesheet.rb

Defined Under Namespace

Classes: Javascript, Package, Stylesheet

Constant Summary collapse

VERSION =
'0.3.2'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Pack

Interface for creating the Rack::Pack middleware.

Parameters:

  • []


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rack/pack.rb', line 93

def initialize(app, options = {})
  @app = app
  Pack.configure(options)
  
  # Set environment based on Application environment
  if defined?(RAILS_ENV)
    Pack.environment = RAILS_ENV # Rails 2
  elsif defined?(Rails) && defined?(Rails.env)
    Pack.environment = Rails.env # Rails 3
  elsif defined?(app.settings) && defined?(app.settings.environment)
    Pack.environment = app.settings.environment # Sinatra
  elsif ENV.key?('RACK_ENV')
    Pack.environment = ENV['RACK_ENV']
  end
end

Class Attribute Details

.environmentObject

Returns the value of attribute environment.



11
12
13
# File 'lib/rack/pack.rb', line 11

def environment
  @environment
end

.optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/rack/pack.rb', line 11

def options
  @options
end

.packagesObject

Returns the value of attribute packages.



11
12
13
# File 'lib/rack/pack.rb', line 11

def packages
  @packages
end

Class Method Details

.add_default_packagesObject

Adds the default Packages, which essentially look in the ‘vendor`, `app`, and current directories for javascripts & stylesheets and packages them into `javascripts/application.js` & `stylesheets/application.css`



77
78
79
80
# File 'lib/rack/pack.rb', line 77

def add_default_packages
  add_package 'javascripts/application.js',  '{vendor,app,.}/javascripts/*.js'
  add_package 'stylesheets/application.css', '{vendor,app,.}/stylesheets/*.css'
end

.add_package(output_file, source_files) ⇒ Object

Adds a new Package for Rack::Pack to update. The Package class is determined based on the filename.

Parameters:

  • output_file (String)

    The path to the file the Package will output.

  • source_files (Array, String)

    The source files to package. Can be either an Array of individual source files or a string that will be used in a glob.



64
65
66
67
68
69
70
71
72
# File 'lib/rack/pack.rb', line 64

def add_package(output_file, source_files)
  if source_files.nil?
    packages.delete(output_file)
  else
    public_output_file    = ::File.join(options[:public_dir], output_file.to_s)
    package_class         = Package[output_file]
    packages[output_file] = package_class.new(public_output_file, source_files)
  end
end

.configure(options = {}) ⇒ Object

Sets up Rack::Pack, optionally adding packages.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :public_dir (String) — default: 'public'

    The directory to output the packaged file.

  • :add_default_packages (Boolean) — default: true

    Wether or not to add the default packages.

  • :always_update (Boolean) — default: false

    Updates the packages on every request.

  • :always_compress (Boolean) — default: false

    Compress the packages on every request.

  • :js_compression (Hash)

    Options to pass directly to the Javascript compression engine.

  • :css_compression (Hash)

    Options to pass directly to the Stylesheet compression engine.

  • :environment (String)

    Manually set the environment.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/pack.rb', line 39

def configure(options = {})
  self.options.each_key do |key|
    self.options[key] = options.delete(key) if options.key?(key)
  end
  
  self.environment = options.delete(:environment) if options.key?(:environment)
  
  add_default_packages if self.options[:add_default_packages]

  options.each do |to_file, from_files|
    add_package(to_file, from_files)
  end
end

.production?Boolean

Determines if we’re running in a production environment.

Returns:

  • (Boolean)


54
55
56
# File 'lib/rack/pack.rb', line 54

def production?
  self.environment.to_s == 'production'
end

.update_packagesObject

Loops through each added Package and updates them when stale.



83
84
85
86
87
# File 'lib/rack/pack.rb', line 83

def update_packages
  Pack.packages.each_value do |package|
    package.update if package.stale?
  end
end

Instance Method Details

#call(env) ⇒ Object



109
110
111
112
# File 'lib/rack/pack.rb', line 109

def call(env)
  update_packages unless skip_update?
  @app.call(env)
end