module Rack::Less # Handles configuration for Rack::Less # Available config settings: # :cache # whether to cache the compilation output to # a corresponding static file. Also determines # what value config#combinations(:key) returns # :compress # whether to remove extraneous whitespace from # compilation output # :combinations # Rack::Less uses combinations as directives for # combining the output of many stylesheets and # serving them as a single resource. Combinations # are defined using a hash, where the key is the # resource name and the value is an array of # names specifying the stylesheets to combine # as that resource. For example: # Rack::Less.config.combinations = { # 'web' => ['reset', 'common', 'app_web'], # 'mobile' => ['reset', 'iui', 'common', 'app_mobile'] # } # :cache_bust # whether to append a timestamp to the sheet requests generated by combinations class Config ATTRIBUTES = [:cache, :compress, :combinations, :cache_bust] attr_accessor *ATTRIBUTES DEFAULTS = { :cache => false, :compress => false, :combinations => {}, :cache_bust => nil } def initialize(settings={}) ATTRIBUTES.each do |a| instance_variable_set("@#{a}", settings[a].nil? ? DEFAULTS[a] : settings[a]) end end def cache? !!@cache end def compress? !!@compress end def combinations(key=nil) if key.nil? @combinations else if cache? stylesheet_filename(key) else (@combinations[key] || []).collect do |combo| stylesheet_filename(combo) end end end end def stylesheet(key) if @combinations[key] combinations(key.to_s) else stylesheet_filename(key.to_s) end end private def stylesheet_filename(key) filename = key.strip filename += ".css" unless filename.include?('.css') if !filename.include?('?') && self.cache_bust != false if !(cb = cache_bust_value).empty? filename += "?#{cb}" end end filename end def cache_bust_value if self.cache_bust == true Time.now.to_i else self.cache_bust ||= default_cache_bust end.to_s end def default_cache_bust if defined?(::Sinatra) && defined?(::Sinatra::Application) app_root_cache_bust(::Sinatra::Application) elsif defined?(::Rails) app_root_cache_bust(::Rails) end || "" end def app_root_cache_bust(app) if app.respond_to?(:root) mtime_cache_bust(app.root.to_s) end end def mtime_cache_bust(path) if File.exists?(path) File.mtime(path).to_i end end end end