Class: SprockAssets

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

Constant Summary collapse

VERSION =

Public: String version number of SprockAsset gem.

'0.9.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, settings = {}) ⇒ SprockAssets

Public: Initialize SprockAssets a middleware.

app - The current Rack app. settings - A hash of the asset path settings.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sprock_assets.rb', line 17

def initialize(app, settings={})
  @app = app
  @settings = {
    assets_path:      'app',
    compiled_path:    'public',
    javascripts_path: '/assets/javascripts',
    stylesheets_path: '/assets/stylesheets',
  }.merge(settings)

  javascript_file = "#{Dir.pwd}/#{@settings[:assets_path]}/#{@settings[:javascripts_path]}/application.js"
  stylesheet_file = "#{Dir.pwd}/#{@settings[:assets_path]}/#{@settings[:stylesheets_path]}/application.css"
  javascript_compiled = "#{Dir.pwd}/public/#{@settings[:javascripts_path]}/application.js"
  stylesheet_compiled = "#{Dir.pwd}/public/#{@settings[:stylesheets_path]}/application.css"

  unless File.exists? javascript_file
    raise "Javascript application file (#{javascript_file}) was not found"
  end

  unless File.exists? stylesheet_file
    raise "Stylesheet application file (#{stylesheet_file}) was not found"
  end

  @assets = Sprockets::Environment.new(Dir.pwd) do |assets|
    assets.logger = Logger.new(STDOUT)
    assets.append_path @settings[:assets_path]
    assets.append_path "#{@settings[:assets_path]}/#{@settings[:javascripts_path]}"
    assets.append_path "#{@settings[:assets_path]}/#{@settings[:stylesheets_path]}"
  end
end

Instance Attribute Details

#appObject

Public: Gets/Sets the Rack application instance.



7
8
9
# File 'lib/sprock_assets.rb', line 7

def app
  @app
end

#assetsObject

Public: Gets/Sets the Sprockets Enviroment.



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

def assets
  @assets
end

#settingsObject

Public: Gets/Sets the middleware settings.



9
10
11
# File 'lib/sprock_assets.rb', line 9

def settings
  @settings
end

Instance Method Details

#call(env) ⇒ Object

Public: A rack middleware call method routing URLs under /assets/ to the SprockAssets middleware.



48
49
50
51
52
53
54
# File 'lib/sprock_assets.rb', line 48

def call(env)
  if env['PATH_INFO'][/^\/assets/].nil?
    @app.call env
  else
    @assets.call env
  end
end