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, #default_external_encoding, #digest_class, #logger, #version

Instance Method Summary collapse

Methods inherited from Base

#[], #append_path, #attributes_for, #clear_paths, #content_type_of, #digest, #each_entry, #each_file, #each_logical_path, #entries, #inspect, #prepend_path, #register_bundle_processor, #register_engine, #register_mime_type, #register_postprocessor, #register_preprocessor, #resolve, #stat, #unregister_bundle_processor, #unregister_postprocessor, #unregister_preprocessor

Methods included from Caching

#cache_get, #cache_set

Methods included from Paths

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

Methods included from Mime

#encoding_for_mime_type, #extension_for_mime_type, #mime_types, #register_mime_type, #registered_mime_types

Methods included from Processing

#bundle_processors, #format_extensions, #postprocessors, #preprocessors, #processors, #register_bundle_processor, #register_postprocessor, #register_preprocessor, #register_processor, #unregister_bundle_processor, #unregister_postprocessor, #unregister_preprocessor, #unregister_processor

Methods included from Compressing

#compressors, #css_compressor, #css_compressor=, #js_compressor, #js_compressor=, #register_compressor

Methods included from Engines

#engine_extensions, #engines, #register_engine

Methods included from Server

#call

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
33
34
35
36
37
38
39
# File 'lib/sprockets/index.rb', line 14

def initialize(environment)
  @environment = environment

  if environment.respond_to?(:default_external_encoding)
    @default_external_encoding = environment.default_external_encoding
  end

  # 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
  @compressors       = environment.compressors

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

Instance Method Details

#file_digest(pathname) ⇒ Object

Cache calls to ‘file_digest`



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

def file_digest(pathname)
  key = pathname.to_s
  if @digests.key?(key)
    @digests[key]
  else
    @digests[key] = super
  end
end

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

Cache ‘find_asset` calls



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sprockets/index.rb', line 57

def find_asset(path, options = {})
  options[:bundle] = true unless options.key?(:bundle)
  if asset = @assets[cache_key_for(path, options)]
    asset
  elsif asset = super
    logical_path_cache_key = cache_key_for(path, options)
    full_path_cache_key    = cache_key_for(asset.pathname, options)

    # Cache on Index
    @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset

    # Push cache upstream to Environment
    @environment.instance_eval do
      @assets[logical_path_cache_key] = @assets[full_path_cache_key] = asset
    end

    asset
  end
end

#indexObject

No-op return self as index



42
43
44
# File 'lib/sprockets/index.rb', line 42

def index
  self
end