Class: Sprockets::Base

Inherits:
Object
  • Object
show all
Includes:
Caching, Processing, Server, Trail
Defined in:
lib/sprockets/base.rb

Overview

‘Base` class for `Environment` and `Index`.

Direct Known Subclasses

Environment, Index

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#cacheObject

Get persistent cache store



89
90
91
# File 'lib/sprockets/base.rb', line 89

def cache
  @cache
end

#context_classObject (readonly)

Get ‘Context` class.

This class maybe mutated and mixed in with custom helpers.

environment.context_class.instance_eval do
  include MyHelpers
  def asset_url; end
end


86
87
88
# File 'lib/sprockets/base.rb', line 86

def context_class
  @context_class
end

#digest_classObject

Returns a ‘Digest` implementation class.

Defaults to ‘Digest::MD5`.



19
20
21
# File 'lib/sprockets/base.rb', line 19

def digest_class
  @digest_class
end

#loggerObject

Get and set ‘Logger` instance.



75
76
77
# File 'lib/sprockets/base.rb', line 75

def logger
  @logger
end

#versionObject

The ‘Environment#version` is a custom value used for manually expiring all asset caches.

Sprockets is able to track most file and directory changes and will take care of expiring the cache for you. However, its impossible to know when any custom helpers change that you mix into the ‘Context`.

It would be wise to increment this value anytime you make a configuration change to the ‘Environment` object.



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

def version
  @version
end

Instance Method Details

#[](*args) ⇒ Object

Preferred ‘find_asset` shorthand.

environment['application.js']


170
171
172
# File 'lib/sprockets/base.rb', line 170

def [](*args)
  find_asset(*args)
end

#attributes_for(path) ⇒ Object

Internal. Return a ‘AssetAttributes` for `path`.



138
139
140
# File 'lib/sprockets/base.rb', line 138

def attributes_for(path)
  AssetAttributes.new(self, path)
end

#content_type_of(path) ⇒ Object

Internal. Return content type of ‘path`.



143
144
145
# File 'lib/sprockets/base.rb', line 143

def content_type_of(path)
  attributes_for(path).content_type
end

#digestObject

Returns a ‘Digest` instance for the `Environment`.

This value serves two purposes. If two ‘Environment`s have the same digest value they can be treated as equal. This is more useful for comparing environment states between processes rather than in the same. Two equal `Environment`s can share the same cached assets.

The value also provides a seed digest for all ‘Asset` digests. Any change in the environment digest will affect all of its assets.



64
65
66
67
68
69
70
71
72
# File 'lib/sprockets/base.rb', line 64

def digest
  # Compute the initial digest using the implementation class. The
  # Sprockets release version and custom environment version are
  # mixed in. So any new releases will affect all your assets.
  @digest ||= digest_class.new.update(VERSION).update(version.to_s)

  # Returned a dupped copy so the caller can safely mutate it with `.update`
  @digest.dup
end

#each_entry(root, &block) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sprockets/base.rb', line 174

def each_entry(root, &block)
  return to_enum(__method__, root) unless block_given?
  root = Pathname.new(root) unless root.is_a?(Pathname)

  paths = []
  entries(root).sort.each do |filename|
    path = root.join(filename)
    paths << path

    if stat(path).directory?
      each_entry(path) do |subpath|
        paths << subpath
      end
    end
  end

  paths.sort_by(&:to_s).each(&block)

  nil
end

#each_fileObject



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sprockets/base.rb', line 195

def each_file
  return to_enum(__method__) unless block_given?
  paths.each do |root|
    each_entry(root) do |path|
      if !stat(path).directory?
        yield path
      end
    end
  end
  nil
end

#each_logical_pathObject



207
208
209
210
211
212
213
214
215
216
# File 'lib/sprockets/base.rb', line 207

def each_logical_path
  return to_enum(__method__) unless block_given?
  files = {}
  each_file do |filename|
    logical_path = attributes_for(filename).logical_path
    yield logical_path unless files[logical_path]
    files[logical_path] = true
  end
  nil
end

#entries(pathname) ⇒ Object

Works like ‘Dir.entries`.

Subclasses may cache this method.



109
110
111
# File 'lib/sprockets/base.rb', line 109

def entries(pathname)
  trail.entries(pathname)
end

#file_digest(path) ⇒ Object

Read and compute digest of filename.

Subclasses may cache this method.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sprockets/base.rb', line 123

def file_digest(path)
  if stat = self.stat(path)
    # If its a file, digest the contents
    if stat.file?
      digest.file(path.to_s)

    # If its a directive, digest the list of filenames
    elsif stat.directory?
      contents = self.entries(path).join(',')
      digest.update(contents)
    end
  end
end

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

Find asset by logical path or expanded path.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sprockets/base.rb', line 148

def find_asset(path, options = {})
  logical_path = path
  pathname     = Pathname.new(path)

  if pathname.absolute?
    return unless stat(pathname)
    logical_path = attributes_for(pathname).logical_path
  else
    begin
      pathname = resolve(logical_path)
    rescue FileNotFound
      return nil
    end
  end

  build_asset(logical_path, pathname, options)
end

#indexObject

Return an ‘Index`. Must be implemented by the subclass.

Raises:

  • (NotImplementedError)


102
103
104
# File 'lib/sprockets/base.rb', line 102

def index
  raise NotImplementedError
end

#inspectObject

Pretty inspect



219
220
221
222
223
224
225
# File 'lib/sprockets/base.rb', line 219

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} " +
    "root=#{root.to_s.inspect}, " +
    "paths=#{paths.inspect}, " +
    "digest=#{digest.to_s.inspect}" +
    ">"
end

#stat(path) ⇒ Object

Works like ‘File.stat`.

Subclasses may cache this method.



116
117
118
# File 'lib/sprockets/base.rb', line 116

def stat(path)
  trail.stat(path)
end