Class: Jim::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/jim/rack.rb

Overview

Jim::Rack is a Rack middleware for allowing live bundling and compression of the bunldes in your Jimfile without having to rebundle using the command line. You can specify a number of options:

:jimfile: Path to your Jimfile (default ./Jimfile) :jimhome: Path to your JIMHOME directory (default ENV or ~/.jim) :bundle_uri: URI to serve the bundled requirements (default '/javascripts/')

The `bundle_uri` should be ~ a directory and a bundle can be generated by requesting the bundlename.js behind the directory. For example, given a Jimfile like:

{
  "bundles": {
    "default": [
      "jquery",
      "jquery.color",
      ["sammy", "0.6.3"]
    ]
  }
}

You could get the 'default' bundle by

GET /javascripts/default.js

Or the compressed version with:

GET /javascripts/default.min.js

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rack.


36
37
38
39
40
41
42
43
44
# File 'lib/jim/rack.rb', line 36

def initialize(app, options = {})
  @app = app
  jimfile = Pathname.new(options[:jimfile] || 'Jimfile')
  jimhome = Pathname.new(options[:jimhome] || ENV['JIMHOME'] || '~/.jim').expand_path
  @bundler = Jim::Bundler.new(jimfile, Jim::Index.new(jimhome), options)
  # unset the bundlers bundle dir so it returns a string
  @bundler.bundle_dir = nil
  @bundle_uri = options[:bundle_uri] || '/javascripts/'
end

Instance Method Details

#_call(env) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jim/rack.rb', line 50

def _call(env)
  uri = env['PATH_INFO']
  if uri =~ bundle_matcher
    name = $1
    if name =~ compressed_matcher
      run_action(:compress!, name.gsub(compressed_matcher, ''))
    else
      run_action(:bundle!, name)
    end
  else
    @app.call(env)
  end
end

#call(env) ⇒ Object


46
47
48
# File 'lib/jim/rack.rb', line 46

def call(env)
  dup._call(env)
end