Class: Sprockets::Index

Inherits:
Base
  • Object
show all
Defined in:
lib/sprockets/index.rb

Overview

‘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`.

Instance Attribute Summary

Attributes inherited from Base

#cache, #context_class, #logger

Instance Method Summary collapse

Methods inherited from Base

#[], #attributes_for, #content_type_of, #each_entry, #each_file, #each_logical_path, #entries, #inspect, #stat

Methods included from Caching

#asset_from_hash, #cache_hash

Methods included from Processing

#bundle_processors, #css_compressor, #css_compressor=, #format_extensions, #js_compressor, #js_compressor=, #postprocessors, #preprocessors, #processors, #register_bundle_processor, #register_engine, #register_mime_type, #register_postprocessor, #register_preprocessor, #register_processor, #unregister_bundle_processor, #unregister_postprocessor, #unregister_preprocessor, #unregister_processor

Methods included from Engines

#engine_extensions, #engines, #register_engine

Methods included from Mime

#extension_for_mime_type, #mime_types, #register_mime_type

Methods included from Server

#call

Methods included from Trail

#append_path, #clear_paths, #extensions, #paths, #prepend_path, #resolve, #root

Methods included from Digest

#digest, #digest_class, #digest_class=, #version, #version=

Constructor Details

#initialize(environment) ⇒ Index

Returns a new instance of Index.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sprockets/index.rb', line 14

def initialize(environment)
  # Copy environment attributes
  @logger            = environment.logger
  @context_class     = environment.context_class
  @cache             = environment.cache
  @trail             = environment.trail.index
  @digest            = environment.digest
  @digest_class      = environment.digest_class
  @version           = environment.version
  @mime_types        = environment.mime_types
  @engines           = environment.engines
  @preprocessors     = environment.preprocessors
  @postprocessors    = environment.postprocessors
  @bundle_processors = environment.bundle_processors

  # Initialize caches
  @assets  = {}
  @digests = {}
end

Instance Method Details

#file_digest(pathname, data = nil) ⇒ Object

Cache calls to ‘file_digest`



40
41
42
# File 'lib/sprockets/index.rb', line 40

def file_digest(pathname, data = nil)
  memoize(@digests, pathname.to_s) { super }
end

#find_asset(path, options = {}) ⇒ Object

Cache ‘find_asset` calls



45
46
47
48
49
50
51
52
53
# File 'lib/sprockets/index.rb', line 45

def find_asset(path, options = {})
  if asset = @assets[path.to_s]
    asset
  elsif asset = super
    # Cache at logical path and expanded path
    @assets[path.to_s] = @assets[asset.pathname.to_s] = asset
    asset
  end
end

#indexObject

No-op return self as index



35
36
37
# File 'lib/sprockets/index.rb', line 35

def index
  self
end