Class: Rack::Sprockets::Base

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/rack/sprockets/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
# File 'lib/rack/sprockets/base.rb', line 9

def initialize(app, options={})
  @app = app
  initialize_options options
  yield self if block_given?
  validate_options
end

Instance Method Details

#call(env) ⇒ Object

The Rack call interface. The receiver acts as a prototype and runs each request in a clone object unless the rack.run_once variable is set in the environment.



19
20
21
22
23
24
25
# File 'lib/rack/sprockets/base.rb', line 19

def call(env)
  if env['rack.run_once']
    call! env
  else
    clone.call! env
  end
end

#call!(env) ⇒ Object

The real Rack call interface. if Sprockets JS is being requested, this is an endpoint:

> generate the compiled javascripts

> respond appropriately

Otherwise, call on up to the app as normal



32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/sprockets/base.rb', line 32

def call!(env)
  @default_options.each { |k,v| env[k] ||= v }
  @env = env
  
  if (@request = Request.new(@env.dup.freeze)).for_sprockets?
    Response.new(@env.dup.freeze, @request.source.to_js).to_rack
  else
    @app.call(env)
  end
end