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



25
26
27
# File 'lib/assets/util.rb', line 25

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

.digest(string) ⇒ Object

Digest



30
31
32
# File 'lib/assets/util.rb', line 30

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

.load_bundle(type) ⇒ Object

Load bundles for js and css



46
47
48
49
50
51
52
53
54
55
# File 'lib/assets/util.rb', line 46

def self.load_bundle(type)
  # Find the items
  items = ::Asset.manifest.select{|r| r.type == type}

  # Find keys for digest and max modified time
  keys, max = items.map(&:key).join, items.map(&:modified).max

  # Insert the bundle into the manifest
  ::Asset.manifest.insert(0, ::Asset::Item.new("bundle.#{type}", type, digest(keys), max))
end

.load_imagesObject

Load images into memory



58
59
60
61
62
63
64
# File 'lib/assets/util.rb', line 58

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

.load_manifestObject

Load manifest



35
36
37
38
39
40
41
42
43
# File 'lib/assets/util.rb', line 35

def self.load_manifest
  Dir["#{Asset.path}/{css,js}/#{pattern}"].uniq.map do |f|
    # Extract type and name
    f =~ /(js|css)\/(.+)$/; type, name = $1, $2

    # Loading manifest with items
    ::Asset::Item.new(name, type, digest(File.read(f)), mtime("#{type}/#{name}"))
  end
end

.mtime(path) ⇒ Object

Get timestamp



20
21
22
# File 'lib/assets/util.rb', line 20

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

.p?Boolean

Production mode?

Returns:

  • (Boolean)


72
73
74
# File 'lib/assets/util.rb', line 72

def self.p?
  %w[staging production].include?(::Asset.mode)
end

.patternObject

File match pattern



67
68
69
# File 'lib/assets/util.rb', line 67

def self.pattern
  ::Asset.symlinks ? '**{,/*/**}/*.*' : '**/*.*'
end

.setup!Object

Setup assets



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/assets/util.rb', line 5

def self.setup!
  # Load the manifest
  ::Asset.manifest = load_manifest

  # Insert bundles
  %w[css js].each{|type| load_bundle(type)}

  # Load the bundle
  ::Asset.bundle = YAML.load_file(File.join(::Asset.path, 'manifest.yml'))

  # Load the images
  ::Asset.images = load_images
end