Class: Sprockets::ProcessedAsset

Inherits:
Asset
  • Object
show all
Defined in:
lib/sprockets/processed_asset.rb

Instance Attribute Summary (collapse)

Attributes inherited from Asset

#content_type, #digest, #length, #logical_path, #mtime, #pathname

Instance Method Summary (collapse)

Methods inherited from Asset

#body, #dependencies, #digest_path, #each, #eql?, from_hash, #hash, #inspect, #stale?, #to_a, #to_s, #write_to

Constructor Details

- (ProcessedAsset) initialize(environment, logical_path, pathname)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sprockets/processed_asset.rb', line 6

def initialize(environment, logical_path, pathname)
  super

  start_time = Time.now.to_f

  context = environment.context_class.new(environment, logical_path, pathname)
  @source = context.evaluate(pathname)
  @length = Rack::Utils.bytesize(source)
  @digest = environment.digest.update(source).hexdigest

  build_required_assets(environment, context)
  build_dependency_paths(environment, context)

  @dependency_digest = compute_dependency_digest(environment)

  elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i
  environment.logger.debug "Compiled #{logical_path}  (#{elapsed_time}ms)  (pid #{Process.pid})"
end

Instance Attribute Details

- (Object) dependency_digest (readonly)

Interal: Used to check equality



26
27
28
# File 'lib/sprockets/processed_asset.rb', line 26

def dependency_digest
  @dependency_digest
end

- (Object) source (readonly)

Returns the value of attribute source



28
29
30
# File 'lib/sprockets/processed_asset.rb', line 28

def source
  @source
end

Instance Method Details

- (Object) encode_with(coder)

Serialize custom attributes in `BundledAsset`.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sprockets/processed_asset.rb', line 52

def encode_with(coder)
  super

  coder['source'] = source
  coder['dependency_digest'] = dependency_digest

  coder['required_paths'] = required_assets.map { |a|
    relativize_root_path(a.pathname).to_s
  }
  coder['dependency_paths'] = dependency_paths.map { |d|
    { 'path' => relativize_root_path(d.pathname).to_s,
      'mtime' => d.mtime.iso8601,
      'digest' => d.digest }
  }
end

- (Boolean) fresh?(environment)

Checks if Asset is stale by comparing the actual mtime and digest to the inmemory model.



70
71
72
73
# File 'lib/sprockets/processed_asset.rb', line 70

def fresh?(environment)
  # Check freshness of all declared dependencies
  @dependency_paths.all? { |dep| dependency_fresh?(environment, dep) }
end

- (Object) init_with(environment, coder)

Initialize `BundledAsset` from serialized `Hash`.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sprockets/processed_asset.rb', line 31

def init_with(environment, coder)
  super

  @source = coder['source']
  @dependency_digest = coder['dependency_digest']

  @required_assets = coder['required_paths'].map { |p|
    p = expand_root_path(p)

    unless environment.paths.detect { |path| p[path] }
      raise UnserializeError, "#{p} isn't in paths"
    end

    p == pathname.to_s ? self : environment.find_asset(p, :bundle => false)
  }
  @dependency_paths = coder['dependency_paths'].map { |h|
    DependencyFile.new(expand_root_path(h['path']), h['mtime'], h['digest'])
  }
end