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

41 lines
981 B
Ruby
Raw Normal View History

2012-01-23 16:58:20 +00:00
require 'sprockets'
module Flowerbox::Delivery
class SprocketsHandler
attr_reader :files, :options
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-01-30 16:34:57 +00:00
paths_for(asset).each { |path| @files.add(path_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
def environment
return @environment if @environment
@environment = Sprockets::Environment.new
2012-01-30 16:34:57 +00:00
@environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons)
@environment.register_engine('.js', Flowerbox::Delivery::Tilt::JSTemplate)
2012-01-23 16:58:20 +00:00
options[:asset_paths].each { |path| @environment.append_path(path) }
@environment
end
2012-01-30 16:34:57 +00:00
def asset_for(*args)
environment.find_asset(*args)
end
def path_for_compiled_asset(path)
Pathname(asset_for(path, :bundle => false).to_s)
end
2012-01-23 16:58:20 +00:00
end
end