Class: Sprockets::Base

Inherits:
Object
  • Object
show all
Includes:
Caching, Digest, 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 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, #path, #url

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=

Instance Attribute Details

#cacheObject

Get persistent cache store



32
33
34
# File 'lib/sprockets/base.rb', line 32

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


29
30
31
# File 'lib/sprockets/base.rb', line 29

def context_class
  @context_class
end

#loggerObject

Get and set `Logger` instance.



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

def logger
  @logger
end

Instance Method Details

#[](*args) ⇒ Object

Preferred `find_asset` shorthand.

environment['application.js']


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

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

#attributes_for(path) ⇒ Object

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



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

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

#content_type_of(path) ⇒ Object

Internal. Return content type of `path`.



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

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

#each_fileObject



113
114
115
116
117
118
119
120
121
# File 'lib/sprockets/base.rb', line 113

def each_file
  return to_enum(__method__) unless block_given?
  paths.each do |base_path|
    Dir["#{base_path}/**/*"].each do |filename|
      yield filename unless File.directory?(filename)
    end
  end
  nil
end

#each_logical_pathObject



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

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.



52
53
54
# File 'lib/sprockets/base.rb', line 52

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

#file_digest(path, data = nil) ⇒ Object

Read and compute digest of filename.

Subclasses may cache this method.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sprockets/base.rb', line 66

def file_digest(path, data = nil)
  if stat = self.stat(path)
    # `data` maybe provided
    if data
      digest.update(data)

    # If its a file, digest the contents
    elsif 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.



95
96
97
98
99
100
101
102
103
# File 'lib/sprockets/base.rb', line 95

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

  if pathname.absolute?
    build_asset(attributes_for(pathname).logical_path, pathname, options)
  else
    find_asset_in_path(pathname, options)
  end
end

#indexObject

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

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/sprockets/base.rb', line 45

def index
  raise NotImplementedError
end

#inspectObject

Pretty inspect



135
136
137
138
139
140
141
# File 'lib/sprockets/base.rb', line 135

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.



59
60
61
# File 'lib/sprockets/base.rb', line 59

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