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

DEFAULT_OPTIONS =
{
  :public_dir           => 'public',
  :always_update        => false,
  :always_compress      => false,
  :js_compression       => {},
  :css_compression      => {},
  :environment          => :development,
  :add_default_packages => true
}.freeze
VERSION =
'0.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pack.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rack/pack.rb', line 62

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.



21
22
23
# File 'lib/rack/pack.rb', line 21

def environment
  @environment
end

.optionsObject

Returns the value of attribute options.



21
22
23
# File 'lib/rack/pack.rb', line 21

def options
  @options
end

.packagesObject

Returns the value of attribute packages.



21
22
23
# File 'lib/rack/pack.rb', line 21

def packages
  @packages
end

Class Method Details

.add_package(output_file, source_files) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/rack/pack.rb', line 45

def add_package(output_file, source_files)
  if source_files.nil?
    packages.delete(output_file)
  else
    public_output_file    = ::File.join(self.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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/pack.rb', line 23

def configure(options = {})
  self.packages = {}
  self.options  = DEFAULT_OPTIONS.dup

  self.options.each_key do |key|
    self.options[key] = options.delete(key) if options.key?(key)
  end
  
  unless self.options[:add_default_packages] == false
    add_package 'javascripts/application.js',  '{vendor,app,.}/javascripts/*.js'
    add_package 'stylesheets/application.css', '{vendor,app,.}/stylesheets/*.css'
  end

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

.production?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rack/pack.rb', line 41

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

.update_packagesObject



55
56
57
58
59
# File 'lib/rack/pack.rb', line 55

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

Instance Method Details

#call(env) ⇒ Object



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

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