Class: Asset::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/assets/util.rb

Class Method Summary collapse

Class Method Details

.asset_path(path) ⇒ Object

Asset path



10
11
12
# File 'lib/assets/util.rb', line 10

def self.asset_path(path)
  File.join(::Asset.path, path)
end

.digest(string) ⇒ Object

Digest



15
16
17
# File 'lib/assets/util.rb', line 15

def self.digest(string)
  Digest::MD5.hexdigest(string)
end

.load_imagesObject

Load images into memory



50
51
52
53
54
55
56
# File 'lib/assets/util.rb', line 50

def self.load_images
  # Store the path and the timestamp
  images = Dir["#{::Asset.path}/images/**/*"].select{|f| File.file?(f)}.map do |i|
    i =~ /\/images\/(.+)/; [$1, mtime("images/#{$1}").to_i]
  end
  Hash[*images.flatten]
end

.load_manifestObject

Load manifest



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/assets/util.rb', line 20

def self.load_manifest
  manifest = []
  list = Dir["#{Asset.path}/{css,js}/**/*"].select{|f| File.file?(f)}.each do |file|
    # Extract type and name
    file =~ /(js|css)\/(.+)$/; type, name = $1, $2

    # Get the modified time
    # Get the modified time of the asset
    modified = mtime("#{type}/#{name}")

    # Loading manifest with items
    manifest << ::Asset::Item.new(name, type, digest(File.read(file)), modified)
  end

  # Insert the css bundle
  css = manifest.select{|r| r.type == 'css'}
  # Get the max modified date and the keys
  max, keys = css.map{|r| r.modified}.max, css.map{|r| r.key}.join
  manifest.insert(0, ::Asset::Item.new('bundle.css', 'css', digest(keys), max))

  # Insert the js bundle
  js = manifest.select{|r| r.type == 'js'}
  # Get the max modified date and the keys
  max, keys = js.map{|r| r.modified}.max, js.map{|r| r.key}.join
  manifest.insert(0, ::Asset::Item.new('bundle.js', 'js', digest(keys), max))

  manifest
end

.mtime(path) ⇒ Object

Get timestamp



5
6
7
# File 'lib/assets/util.rb', line 5

def self.mtime(path)
  File.mtime(asset_path(path)).utc
end