lib/sprockets/context.rb in sprockets-4.0.0.beta3 vs lib/sprockets/context.rb in sprockets-4.0.0.beta4
- old
+ new
@@ -16,10 +16,28 @@
# <%= asset_url "foo.png" %>
#
# The `Context` also collects dependencies declared by
# assets. See `DirectiveProcessor` for an example of this.
class Context
+ # Internal: Proxy for ENV that keeps track of the environment variables used
+ class ENVProxy < SimpleDelegator
+ def initialize(context)
+ @context = context
+ super(ENV)
+ end
+
+ def [](key)
+ @context.depend_on_env(key)
+ super
+ end
+
+ def fetch(key, *)
+ @context.depend_on_env(key)
+ super
+ end
+ end
+
attr_reader :environment, :filename
def initialize(input)
@environment = input[:environment]
@metadata = input[:metadata]
@@ -40,10 +58,14 @@
stubbed: @stubbed,
links: @links,
dependencies: @dependencies }
end
+ def env_proxy
+ ENVProxy.new(self)
+ end
+
# Returns the environment path that contains the file.
#
# If `app/javascripts` and `app/stylesheets` are in your path, and
# current file is `app/javascripts/foo/bar.js`, `load_path` would
# return `app/javascripts`.
@@ -118,9 +140,18 @@
# invalidate the dependency asset will invalidate the source
# file. Unlike `depend_on`, this will recursively include
# the target asset's dependencies.
def depend_on_asset(path)
load(resolve(path))
+ end
+
+ # `depend_on_env` allows you to state a dependency on an environment
+ # variable.
+ #
+ # This is used for caching purposes. Any changes in the value of the
+ # environment variable will invalidate the cache of the source file.
+ def depend_on_env(key)
+ @dependencies << "env:#{key}"
end
# `require_asset` declares `path` as a dependency of the file. The
# dependency will be inserted before the file and will only be
# included once.