Class: Rack::Spriter

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

Constant Summary collapse

File =
::File

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Spriter.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rack/spriter.rb', line 8

def initialize(app, options = {})
  options = default_options.merge(options)
  @app = app
  @stylesheets_path = options[:stylesheets_path]
  @stylesheets_url_pattern = options[:stylesheets_url_pattern]
  @images = []
  ::Spriter.assets_path = options[:assets_path]
  ::Spriter.sprite_image_path = options[:sprite_image_path]
  ::Spriter.sprite_image_url = options[:sprite_image_url]
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  if css = generate_css(env)
    return [200, {"Content-Type" => "text/css", "Cache-Control" => "private"}, [css]]
  else
    @app.call(env)
  end
end

#default_optionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/spriter.rb', line 19

def default_options
  defaults = {
    :sprite_image_url => '/images/sprites.png',
    :stylesheets_url_pattern => %r{stylesheets\/(.+)\.css$}
  }

  if defined? Rails
    defaults.merge(
      :stylesheets_path => File.join(Rails.root, *%w[ public stylesheets ]),
      :assets_path => File.join(Rails.root, *%w[ public images sprite_assets ]),
      :sprite_image_path => File.join(Rails.root, *%w[ public images sprites.png ])
    )
  else
    defaults
  end
end