Class: Sinatra::SimpleAssets::Bundle
- Inherits:
-
Object
- Object
- Sinatra::SimpleAssets::Bundle
- Defined in:
- lib/sinatra/simple_assets.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
Instance Method Summary collapse
- #combined ⇒ Object
- #compile ⇒ Object
- #content ⇒ Object
- #hash ⇒ Object
- #hash_name ⇒ Object
- #hashed_path ⇒ Object
-
#initialize(name, type, root, files) ⇒ Bundle
constructor
A new instance of Bundle.
- #name ⇒ Object
- #path ⇒ Object
Constructor Details
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
20 21 22 |
# File 'lib/sinatra/simple_assets.rb', line 20 def files @files end |
Instance Method Details
#combined ⇒ Object
62 63 64 65 66 |
# File 'lib/sinatra/simple_assets.rb', line 62 def combined @combined ||= @files.map do |file| File.open(@root + file) { |f| f.read } end.join("\n") end |
#compile ⇒ Object
72 73 74 75 76 |
# File 'lib/sinatra/simple_assets.rb', line 72 def compile File.open("#{@root}/#{hashed_path}", 'w') do |f| f << content end end |
#content ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sinatra/simple_assets.rb', line 51 def content case @type when :js require 'uglifier' @content ||= Uglifier.new.compress combined when :css require 'cssmin' @content ||= CSSMin.minify combined end end |
#hash ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/sinatra/simple_assets.rb', line 41 def hash @hash ||= begin Digest::SHA1.hexdigest content rescue # Find the most recent compressed version if the JS runtime is unavailable fname = Dir.glob("#{@root}/#{path}/#{@name}-*.#{@type}").sort_by {|f| File.mtime(f)}.last fname.split('-').last.sub(".#{@type}", "") end end |
#hash_name ⇒ Object
33 34 35 |
# File 'lib/sinatra/simple_assets.rb', line 33 def hash_name "#{@name}-#{hash}.#{@type}" end |
#hashed_path ⇒ Object
37 38 39 |
# File 'lib/sinatra/simple_assets.rb', line 37 def hashed_path "#{path}/#{hash_name}" end |
#name ⇒ Object
29 30 31 |
# File 'lib/sinatra/simple_assets.rb', line 29 def name "#{@name}.#{@type}" end |
#path ⇒ Object
68 69 70 |
# File 'lib/sinatra/simple_assets.rb', line 68 def path @type == :js ? 'javascripts' : 'stylesheets' end |