Class: AssetHash::Base
- Inherits:
-
Object
- Object
- AssetHash::Base
- Defined in:
- lib/asset_hash.rb
Constant Summary collapse
- DEFAULT_ASSET_PATHS =
['favicon.ico', 'images', 'javascripts', 'stylesheets', 'assets']
- DEFAULT_GZIP_TYPES =
['text/css', 'application/javascript']
- @@asset_paths =
DEFAULT_ASSET_PATHS
- @@gzip_types =
DEFAULT_GZIP_TYPES
Class Method Summary collapse
- .absolute_path(path) ⇒ Object
- .asset_paths ⇒ Object
- .asset_paths=(paths) ⇒ Object
- .assets ⇒ Object
- .fingerprint(path, original_path = nil) ⇒ Object
- .gzip_types ⇒ Object
- .gzip_types=(types) ⇒ Object
- .original_path(path) ⇒ Object
- .path_prefix ⇒ Object
- .process! ⇒ Object
Class Method Details
.absolute_path(path) ⇒ Object
42 43 44 |
# File 'lib/asset_hash.rb', line 42 def self.absolute_path(path) File.join path_prefix, path end |
.asset_paths ⇒ Object
38 39 40 |
# File 'lib/asset_hash.rb', line 38 def self.asset_paths @@asset_paths end |
.asset_paths=(paths) ⇒ Object
34 35 36 |
# File 'lib/asset_hash.rb', line 34 def self.asset_paths=(paths) @@asset_paths = paths end |
.assets ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/asset_hash.rb', line 46 def self.assets asset_paths.inject([]) {|assets, path| path = absolute_path(path) assets << path if File.exists? path and !File.directory? path assets += Dir.glob(path+'/**/*').inject([]) {|m, file| m << file unless File.directory? file; m } } end |
.fingerprint(path, original_path = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/asset_hash.rb', line 56 def self.fingerprint(path, original_path = nil) path = File.join path_prefix, path unless path =~ /#{path_prefix}/ begin d = Digest::MD5.file(original_path || path).hexdigest path = path.gsub(path_prefix, '') extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path)) File.join File.dirname(path), "#{File.basename(path, extension)}-#{d}#{extension}" rescue Errno::ENOENT return original_path(path)#path.gsub(path_prefix, '') end end |
.gzip_types ⇒ Object
26 27 28 |
# File 'lib/asset_hash.rb', line 26 def self.gzip_types @@gzip_types end |
.gzip_types=(types) ⇒ Object
22 23 24 |
# File 'lib/asset_hash.rb', line 22 def self.gzip_types=(types) @@gzip_types = types end |
.original_path(path) ⇒ Object
68 69 70 71 72 |
# File 'lib/asset_hash.rb', line 68 def self.original_path(path) path = path.gsub(path_prefix, '') extension = (path =~ /\.gz$/ ? File.extname(File.basename(path, ".gz")) + ".gz" : File.extname(path)) File.join File.dirname(path), "#{File.basename(path, extension)}#{extension}" end |
.path_prefix ⇒ Object
30 31 32 |
# File 'lib/asset_hash.rb', line 30 def self.path_prefix defined?(Rails) ? File.join(Rails.root, 'public') : '' end |
.process! ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/asset_hash.rb', line 74 def self.process! assets.each do |asset| mime_type = MIME::Types.of(asset).first.to_s if gzip_types.include?(mime_type) && !File.exists?(asset + ".gz") # You can GZIP the type of this file and no gzipped version was found already # Gzip the original asset then move it to the right filename gz_path = asset + ".gz" `gzip -c "#{asset}" > "#{gz_path}"` FileUtils.mv(gz_path, File.join(path_prefix, fingerprint(asset) + ".gz")) end FileUtils.cp(asset, File.join(path_prefix, fingerprint(asset))) end end |