Sprockets::Index

`Index` is a special cached version of `Environment`.

The expection is that all of its file system methods are cached for the instances lifetime. This makes `Index` much faster. This behavior is ideal in production environments where the file system is immutable.

`Index` should not be initialized directly. Instead use `Environment#index`.

Public Class Methods

new(environment) click to toggle source
    # File lib/sprockets/index.rb, line 14
14:     def initialize(environment)
15:       @environment = environment
16: 
17:       if environment.respond_to?(:default_external_encoding)
18:         @default_external_encoding = environment.default_external_encoding
19:       end
20: 
21:       # Copy environment attributes
22:       @logger            = environment.logger
23:       @context_class     = environment.context_class
24:       @cache             = environment.cache
25:       @trail             = environment.trail.index
26:       @digest            = environment.digest
27:       @digest_class      = environment.digest_class
28:       @version           = environment.version
29:       @mime_types        = environment.mime_types
30:       @engines           = environment.engines
31:       @preprocessors     = environment.preprocessors
32:       @postprocessors    = environment.postprocessors
33:       @bundle_processors = environment.bundle_processors
34: 
35:       # Initialize caches
36:       @assets  = {}
37:       @digests = {}
38:     end

Public Instance Methods

file_digest(pathname) click to toggle source

Cache calls to `file_digest`

    # File lib/sprockets/index.rb, line 46
46:     def file_digest(pathname)
47:       key = pathname.to_s
48:       if @digests.key?(key)
49:         @digests[key]
50:       else
51:         @digests[key] = super
52:       end
53:     end
find_asset(path, options = {}) click to toggle source

Cache `find_asset` calls

    # File lib/sprockets/index.rb, line 56
56:     def find_asset(path, options = {})
57:       options[:bundle] = true unless options.key?(:bundle)
58:       if asset = @assets[cache_key_for(path, options)]
59:         asset
60:       elsif asset = super
61:         logical_path_cache_key = cache_key_for(path, options)
62:         full_path_cache_key    = cache_key_for(asset.pathname, options)
63: 
64:         # Cache on Index
65:         @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
66: 
67:         # Push cache upstream to Environment
68:         @environment.instance_eval do
69:           @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
70:         end
71: 
72:         asset
73:       end
74:     end
index() click to toggle source

No-op return self as index

    # File lib/sprockets/index.rb, line 41
41:     def index
42:       self
43:     end

Protected Instance Methods

build_asset(path, pathname, options) click to toggle source

Cache asset building in memory and in persisted cache.

    # File lib/sprockets/index.rb, line 84
84:       def build_asset(path, pathname, options)
85:         # Memory cache
86:         key = cache_key_for(pathname, options)
87:         if @assets.key?(key)
88:           @assets[key]
89:         else
90:           @assets[key] = begin
91:             # Persisted cache
92:             cache_asset(key) do
93:               super
94:             end
95:           end
96:         end
97:       end
expire_index!() click to toggle source

Index is immutable, any methods that try to clear the cache should bomb.

    # File lib/sprockets/index.rb, line 79
79:       def expire_index!
80:         raise TypeError, "can't modify immutable index"
81:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.