flowerbox-delivery/lib/flowerbox/delivery/sprockets_handler.rb

58 lines
1.3 KiB
Ruby
Raw Permalink Normal View History

2012-01-23 16:58:20 +00:00
require 'sprockets'
2012-02-08 17:41:18 +00:00
require 'sprockets/engines'
2012-02-29 19:24:07 +00:00
require 'forwardable'
2012-03-09 15:06:27 +00:00
require 'sprockets-vendor_gems'
2012-01-23 16:58:20 +00:00
module Flowerbox::Delivery
class SprocketsHandler
2012-02-29 19:24:07 +00:00
extend Forwardable
2012-01-23 16:58:20 +00:00
attr_reader :files, :options
2012-03-14 17:07:28 +00:00
def_delegators :environment, :append_path, :register_engine, :[]
2012-02-29 19:24:07 +00:00
2012-03-14 21:41:02 +00:00
def self.gem_asset_paths
@gem_asset_paths ||= Sprockets.find_gem_vendor_paths
end
2012-01-23 16:58:20 +00:00
def initialize(options)
@options = options
2012-01-23 17:21:11 +00:00
@files = UniqueAssetList.new
2012-01-23 16:58:20 +00:00
end
def add(asset)
2012-03-08 15:14:45 +00:00
paths_for(asset).each { |path| add_paths_for_compiled_asset(path) }
2012-01-23 16:58:20 +00:00
end
def paths_for(asset)
environment.find_asset(asset).to_a.collect(&:pathname)
end
2012-03-14 17:07:28 +00:00
def expire_index!
@environment.send(:expire_index!)
end
2012-01-23 16:58:20 +00:00
def environment
return @environment if @environment
2012-03-14 21:41:02 +00:00
@environment = Sprockets::Environment.new
@environment.cache = Sprockets::Cache::FileStore.new(".tmp")
2012-02-08 17:41:18 +00:00
2012-03-14 21:41:02 +00:00
self.class.gem_asset_paths.each { |path| append_path(path) }
2012-02-29 19:24:07 +00:00
options[:asset_paths].each { |path| append_path(path) }
2012-01-30 16:34:57 +00:00
2012-01-23 16:58:20 +00:00
@environment
end
2012-01-30 16:34:57 +00:00
def asset_for(*args)
environment.find_asset(*args)
end
2012-03-08 15:14:45 +00:00
def add_paths_for_compiled_asset(path)
asset_for(path, :bundle => false).to_a.each { |file_path| @files.add(file_path) }
2012-01-30 16:34:57 +00:00
end
2012-01-23 16:58:20 +00:00
end
end
2012-02-29 19:24:07 +00:00