Class: Rack::ScriptStacker

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/scriptstacker.rb,
lib/rack/scriptstacker/version.rb

Defined Under Namespace

Modules: InjectMode

Constant Summary collapse

DEFAULT_CONFIG =
{
  configure_static: true,
  inject_mode: InjectMode::TAG,
  stackers: {
    css: {
      template: '<link rel="stylesheet" type="text/css" href="%s" />',
      glob: '*.css',
      slot: 'CSS',
      inject_before_tag: '</head>',
    },
    javascript: {
      template: '<script type="text/javascript" src="%s"></script>',
      glob: '*.js',
      slot: 'JAVASCRIPT',
      inject_before_tag: '</body>',
    }
  }
}
VERSION =
"0.1.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, config = {}, &stack_spec) ⇒ ScriptStacker

Returns a new instance of ScriptStacker.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/scriptstacker.rb', line 33

def initialize app, config={}, &stack_spec
  @config = DEFAULT_CONFIG.recursive_merge config
  @path_specs = ScriptStackerUtils::SpecSolidifier.new(
    @config[:stackers].keys
  ).call stack_spec
  @runner = ScriptStackerUtils::Runner.new(
    @config[:stackers],
    @config[:inject_mode]
  )
  @app = @config[:configure_static] ? configure_static(app) : app
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack/scriptstacker.rb', line 45

def call env
  response = @app.call env

  if response[1]['Content-Type'] != 'text/html'
    response
  else
    [
      response[0],
      response[1],
      @runner.replace_in_body(response[2], @path_specs)
    ]
  end
end