Class: Sprockets::Environment

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

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, #file_digest, #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(root = ".") {|_self| ... } ⇒ Environment

`Environment` should initialized with your application's root directory. This should be the same as your Rails or Rack root.

env = Environment.new(Rails.root)

Yields:

  • (_self)

Yield Parameters:


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sprockets/environment.rb', line 17

def initialize(root = ".")
  @trail = Hike::Trail.new(root)

  self.logger = Logger.new($stderr)
  self.logger.level = Logger::FATAL

  if respond_to?(:default_external_encoding)
    self.default_external_encoding = Encoding::UTF_8
  end

  # Create a safe `Context` subclass to mutate
  @context_class = Class.new(Context)

  # Set MD5 as the default digest
  require 'digest/md5'
  @digest_class = ::Digest::MD5
  @version = ''

  @mime_types        = Sprockets.registered_mime_types
  @engines           = Sprockets.engines
  @preprocessors     = Sprockets.preprocessors
  @postprocessors    = Sprockets.postprocessors
  @bundle_processors = Sprockets.bundle_processors
  @compressors       = Sprockets.compressors

  Sprockets.paths.each do |path|
    append_path(path)
  end

  @engines.each do |ext, klass|
    add_engine_to_trail(ext, klass)
  end

  @mime_types.each do |ext, type|
    @trail.append_extension(ext)
  end

  expire_index!

  yield self if block_given?
end

Instance Method Details

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

Cache `find_asset` calls


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sprockets/environment.rb', line 69

def find_asset(path, options = {})
  options[:bundle] = true unless options.key?(:bundle)

  # Ensure inmemory cached assets are still fresh on every lookup
  if (asset = @assets[cache_key_for(path, options)]) && asset.fresh?(self)
    asset
  elsif asset = index.find_asset(path, options)
    # Cache is pushed upstream by Index#find_asset
    asset
  end
end

#indexObject

Returns a cached version of the environment.

All its file system calls are cached which makes `index` much faster. This behavior is ideal in production since the file system only changes between deploys.


64
65
66
# File 'lib/sprockets/environment.rb', line 64

def index
  Index.new(self)
end