Class: Sprockets::Base

Inherits:
Object
  • Object
show all
Includes:
Bower, Configuration, DigestUtils, Loader, PathDependencyUtils, PathDigestUtils, PathUtils, Resolve, Server
Defined in:
lib/sprockets/base.rb,
lib/sprockets/legacy.rb

Overview

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

Direct Known Subclasses

CachedEnvironment, Environment

Constant Summary

Constants included from Bower

Sprockets::Bower::POSSIBLE_BOWER_JSONS

Constants included from PathUtils

PathUtils::SEPARATOR_PATTERN

Constants included from Utils

Utils::UNBOUND_METHODS_BIND_TO_ANY_OBJECT

Constants included from ProcessorUtils

ProcessorUtils::VALID_METADATA_COMPOUND_TYPES, ProcessorUtils::VALID_METADATA_COMPOUND_TYPES_HASH, ProcessorUtils::VALID_METADATA_TYPES, ProcessorUtils::VALID_METADATA_VALUE_TYPES, ProcessorUtils::VALID_METADATA_VALUE_TYPES_HASH

Constants included from DigestUtils

DigestUtils::DIGEST_SIZES, DigestUtils::HASH_ALGORITHMS

Instance Attribute Summary collapse

Attributes included from Configuration

#computed_config, #config, #context_class, #logger

Instance Method Summary collapse

Methods included from Bower

#read_bower_main, #resolve_alternates

Methods included from Resolve

#resolve!

Methods included from HTTPUtils

#find_best_mime_type_match, #find_best_q_match, #find_mime_type_matches, #find_q_matches, #match_mime_type?, #match_mime_type_keys, #parse_q_values

Methods included from PathDependencyUtils

#entries_with_dependencies, #file_digest_dependency_set, #stat_directory_with_dependencies, #stat_sorted_tree_with_dependencies

Methods included from URIUtils

#build_asset_uri, #build_file_digest_uri, #encode_uri_query_params, #join_file_uri, #join_uri, #parse_asset_uri, #parse_file_digest_uri, #parse_uri_query_params, #split_file_uri, #split_uri, #valid_asset_uri?

Methods included from PathUtils

#absolute_path?, #atomic_write, #directory?, #entries, #file?, #find_upwards, #match_path_extname, #path_extnames, #path_parents, #paths_split, #relative_path?, #split_subpath, #stat, #stat_directory, #stat_sorted_tree, #stat_tree

Methods included from Loader

#load

Methods included from Engines

#engine_mime_types, #engines, #register_engine

Methods included from Utils

#concat_javascript_sources, #dfs, #dfs_paths, #duplicable?, #hash_reassoc, #hash_reassoc1, #module_include, #normalize_extension, #string_end_with_semicolon?

Methods included from Mime

#mime_exts, #mime_type_charset_detecter, #mime_types, #read_file, #register_mime_type

Methods included from Processing

#bundle_processors, #pipelines, #postprocessors, #preprocessors, #register_bundle_metadata_reducer, #register_bundle_processor, #register_pipeline, #register_postprocessor, #register_preprocessor, #unregister_bundle_processor, #unregister_postprocessor, #unregister_preprocessor

Methods included from ProcessorUtils

#call_processor, #call_processors, #compose_processors, #processor_cache_key, #processors_cache_keys, #valid_processor_metadata_value?, #validate_processor_result!

Methods included from Transformers

#compose_transformers, #expand_transform_accepts, #register_transformer, #resolve_transform_type, #transformers

Methods included from DigestUtils

#detect_digest_class, #digest, #digest_class, #hexdigest_integrity_uri, #integrity_uri, #pack_base64digest, #pack_hexdigest, #pack_urlsafe_base64digest, #unpack_hexdigest

Methods included from Server

#call

Methods included from Configuration

#digest_class, #digest_class=, #initialize_configuration, #version, #version=

Methods included from Paths

#append_path, #clear_paths, #each_file, #paths, #prepend_path, #root

Methods included from Compressing

#compressors, #css_compressor, #css_compressor=, #gzip=, #gzip?, #js_compressor, #js_compressor=, #register_compressor, #skip_gzip?

Methods included from Dependencies

#add_dependency, #dependencies, #dependency_resolvers, #register_dependency_resolver, #resolve_dependency

Methods included from PathDigestUtils

#files_digest, #stat_digest

Instance Attribute Details

#cacheObject

Get persistent cache store



26
27
28
# File 'lib/sprockets/base.rb', line 26

def cache
  @cache
end

Instance Method Details

#[](*args) ⇒ Object

Preferred ‘find_asset` shorthand.

environment['application.js']


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

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

#cache_get(key) ⇒ Object



123
124
125
# File 'lib/sprockets/legacy.rb', line 123

def cache_get(key)
  cache.get(key)
end

#cache_set(key, value) ⇒ Object



127
128
129
# File 'lib/sprockets/legacy.rb', line 127

def cache_set(key, value)
  cache.set(key, value)
end

#cachedObject Also known as: index

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



38
39
40
# File 'lib/sprockets/base.rb', line 38

def cached
  raise NotImplementedError
end

#compress_from_root(uri) ⇒ Object



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

def compress_from_root(uri)
  URITar.new(uri, self).compress
end

#each_logical_path(*args, &block) ⇒ Object

Deprecated: Iterate over all logical paths with a matcher.

Remove from 4.x.

args - List of matcher objects.

Returns Enumerator if no block is given.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sprockets/legacy.rb', line 79

def each_logical_path(*args, &block)
  return to_enum(__method__, *args) unless block_given?

  filters = args.flatten.map { |arg| Manifest.compile_match_filter(arg) }
  logical_paths.each do |a, b|
    if filters.any? { |f| f.call(a, b) }
      if block.arity == 2
        yield a, b
      else
        yield a
      end
    end
  end

  nil
end

#expand_from_root(uri) ⇒ Object



106
107
108
# File 'lib/sprockets/base.rb', line 106

def expand_from_root(uri)
  URITar.new(uri, self).expand
end

#file_digest(path) ⇒ Object

Internal: Compute digest for path.

path - String filename or directory path.

Returns a String digest or nil.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sprockets/base.rb', line 48

def file_digest(path)
  if stat = self.stat(path)
    # Caveat: Digests are cached by the path's current mtime. Its possible
    # for a files contents to have changed and its mtime to have been
    # negligently reset thus appearing as if the file hasn't changed on
    # disk. Also, the mtime is only read to the nearest second. It's
    # also possible the file was updated more than once in a given second.
    key = UnloadedAsset.new(path, self).file_digest_key(stat.mtime.to_i)
    cache.fetch(key) do
      self.stat_digest(path, stat)
    end
  end
end

#find_all_linked_assets(path, options = {}) {|asset| ... } ⇒ Object

Yields:

  • (asset)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sprockets/base.rb', line 70

def find_all_linked_assets(path, options = {})
  return to_enum(__method__, path, options) unless block_given?

  asset = find_asset(path, options)
  return unless asset

  yield asset
  stack = asset.links.to_a

  while uri = stack.shift
    yield asset = load(uri)
    stack = asset.links.to_a + stack
  end

  nil
end

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

Find asset by logical path or expanded path.



63
64
65
66
67
68
# File 'lib/sprockets/base.rb', line 63

def find_asset(path, options = {})
  uri, _ = resolve(path, options.merge(compat: false))
  if uri
    load(uri)
  end
end

#inspectObject

Pretty inspect



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

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

#logical_pathsObject

Deprecated: Enumerate over all logical paths in the environment.

Returns an Enumerator of [logical_path, filename].



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sprockets/legacy.rb', line 99

def logical_paths
  return to_enum(__method__) unless block_given?

  seen = Set.new

  paths.each do |load_path|
    stat_tree(load_path).each do |filename, stat|
      next unless stat.file?

      path = split_subpath(load_path, filename)
      path, mime_type, _, _ = parse_path_extnames(path)
      path = normalize_logical_path(path)
      path += mime_types[mime_type][:extensions].first if mime_type

      if !seen.include?(path)
        yield path, filename
        seen << path
      end
    end
  end

  nil
end

#normalize_logical_path(path) ⇒ Object



131
132
133
134
135
# File 'lib/sprockets/legacy.rb', line 131

def normalize_logical_path(path)
  dirname, basename = File.split(path)
  path = dirname if basename == 'index'
  path
end

#resolve_with_compat(path, options = {}) ⇒ Object Also known as: resolve

Deprecated: Change default return type of resolve() to return 2.x compatible plain filename String. 4.x will always return an Asset URI and a set of file system dependencies that had to be read to compute the result.

2.x

  resolve("foo.js")
  # => "/path/to/app/javascripts/foo.js"

3.x

  resolve("foo.js")
  # => "/path/to/app/javascripts/foo.js"

  resolve("foo.js", compat: true)
  # => "/path/to/app/javascripts/foo.js"

  resolve("foo.js", compat: false)
  # => [
  #   "file:///path/to/app/javascripts/foo.js?type=application/javascript"
  #    #<Set: {"file-digest:/path/to/app/javascripts/foo.js"}>
  # ]

4.x

  resolve("foo.js")
  # => [
  #   "file:///path/to/app/javascripts/foo.js?type=application/javascript"
  #    #<Set: {"file-digest:/path/to/app/javascripts/foo.js"}>
  # ]


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sprockets/legacy.rb', line 55

def resolve_with_compat(path, options = {})
  options = options.dup
  if options.delete(:compat) { true }
    uri, _ = resolve_without_compat(path, options)
    if uri
      path, _ = parse_asset_uri(uri)
      path
    else
      nil
    end
  else
    resolve_without_compat(path, options)
  end
end