lib/classy_assets.rb in classy_assets-0.3.2 vs lib/classy_assets.rb in classy_assets-0.4.0
- old
+ new
@@ -4,109 +4,31 @@
require 'sass'
require 'coffee_script'
require 'sprockets'
require 'sprockets-sass'
require 'sinatra/base'
+require 'classy_assets/configuration'
require 'classy_assets/version'
module ClassyAssets
- class Configuration
- def self.configure
- yield self
- end
-
- def self.asset_digest
- @asset_digest.nil? ? false : @asset_digest
- end
+ def self.asset_url_for(asset)
+ asset = asset.instance_of?(::Sprockets::BundledAsset) ? asset : Configuration.sprockets[asset].logical_path
+ asset = build_digest_filename(asset) if Configuration.asset_digest
+ debug = (Configuration.debug_mode) ? '?body=1' : ''
+ "#{Configuration.asset_host}/#{Configuration.asset_prefix}/#{asset}#{debug}"
+ end
- def self.asset_digest=(digest)
- @asset_digest = digest
- end
+ def self.build_digest_filename(asset)
+ filename = asset.split(".")
+ ext = filename.pop
+ "#{filename.join('.')}-#{Configuration.sprockets.digest}.#{ext}"
+ end
- def self.asset_paths
- @asset_paths || build_asset_path(%w(fonts images javascripts stylesheets))
- end
-
- def self.asset_paths=(paths)
- @asset_paths = paths.to_a
- end
-
- def self.asset_host
- @asset_host
- end
-
- def self.asset_host=(host)
- @asset_host = host
- end
-
- def self.asset_host_protocol
- @asset_host_protocol || :relative
- end
-
- def self.asset_host_protocol=(protocol)
- @asset_host_protocol = protocol
- end
-
- def self.asset_prefix
- @asset_prefix || 'assets'
- end
-
- def self.asset_prefix=(prefix)
- @asset_prefix = prefix
- end
-
- def self.css_compressor
- @css_compressor || :yui
- end
-
- def self.css_compressor=(compressor)
- @css_compressor = compressor
- end
-
- def self.debug_mode
- @debug_mode.nil? ? (ENV['RACK_ENV'] == 'development') : @debug_mode
- end
-
- def self.debug_mode=(debug)
- @debug_mode = debug
- end
-
- def self.js_compressor
- @js_compressor || :uglifier
- end
-
- def self.js_compressor=(compressor)
- @js_compressor = compressor
- end
-
- def self.public_path
- @public_path || File.join(root_path, 'public')
- end
-
- def self.public_path=(path)
- @public_path = path
- end
-
- def self.root_path
- @root_path || '.'
- end
-
- def self.root_path=(path)
- @root_path = path
- end
-
- def self.build_asset_path(dir_names)
- dir_names.map! do |dir_name|
- File.join(root_path, asset_prefix, dir_name)
- end
- dir_names
- end
-
- def self.sprockets
- @sprockets ||= ::Sprockets::Environment.new(root_path)
- asset_paths.each do |asset_path|
- @sprockets.append_path asset_path unless @sprockets.paths.include?(asset_path)
- end
- @sprockets
- end
+ def self.asset_tag_from(sources, ext)
+ sources = [sources] unless sources.is_a? Array
+ sources.map do |source|
+ source = "#{source}.#{ext}" unless source =~ /\.#{ext}$/
+ asset_url = (source =~ /\/|http/) ? source : ::ClassyAssets.asset_url_for(source)
+ yield(asset_url)
+ end.join('')
end
end