Class: Sprockets::BundledAsset
- Defined in:
- lib/sprockets/bundled_asset.rb
Overview
‘BundledAsset`s are used for files that need to be processed and concatenated with other assets. Use for `.js` and `.css` files.
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Attributes inherited from Asset
#content_type, #digest, #length, #logical_path, #mtime, #pathname
Instance Method Summary collapse
-
#body ⇒ Object
Get asset’s own processed contents.
-
#dependencies ⇒ Object
Return an ‘Array` of `Asset` files that are declared dependencies.
-
#encode_with(coder) ⇒ Object
Serialize custom attributes in ‘BundledAsset`.
-
#fresh?(environment) ⇒ Boolean
Checks if Asset is stale by comparing the actual mtime and digest to the inmemory model.
-
#init_with(environment, coder) ⇒ Object
Initialize ‘BundledAsset` from serialized `Hash`.
-
#initialize(environment, logical_path, pathname) ⇒ BundledAsset
constructor
A new instance of BundledAsset.
-
#to_a ⇒ Object
Expand asset into an ‘Array` of parts.
Methods inherited from Asset
#digest_path, #each, #eql?, from_hash, #hash, #inspect, #stale?, #to_s, #write_to
Constructor Details
#initialize(environment, logical_path, pathname) ⇒ BundledAsset
Returns a new instance of BundledAsset.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sprockets/bundled_asset.rb', line 13 def initialize(environment, logical_path, pathname) super(environment, logical_path, pathname) @processed_asset = environment.find_asset(pathname, :bundle => false) @required_assets = @processed_asset.required_assets @source = "" # Explode Asset into parts and gather the dependency bodies to_a.each { |dependency| @source << dependency.to_s } # Run bundle processors on concatenated source context = environment.context_class.new(environment, logical_path, pathname) @source = context.evaluate(pathname, :data => @source, :processors => environment.bundle_processors(content_type)) @mtime = to_a.map(&:mtime).max @length = Rack::Utils.bytesize(source) @digest = environment.digest.update(source).hexdigest end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
11 12 13 |
# File 'lib/sprockets/bundled_asset.rb', line 11 def source @source end |
Instance Method Details
#body ⇒ Object
Get asset’s own processed contents. Excludes any of its required dependencies but does run any processors or engines on the original file.
59 60 61 |
# File 'lib/sprockets/bundled_asset.rb', line 59 def body @processed_asset.source end |
#dependencies ⇒ Object
Return an ‘Array` of `Asset` files that are declared dependencies.
64 65 66 |
# File 'lib/sprockets/bundled_asset.rb', line 64 def dependencies to_a.reject { |a| a.eql?(@processed_asset) } end |
#encode_with(coder) ⇒ Object
Serialize custom attributes in ‘BundledAsset`.
49 50 51 52 53 54 |
# File 'lib/sprockets/bundled_asset.rb', line 49 def encode_with(coder) super coder['source'] = source coder['required_assets_digest'] = @processed_asset.dependency_digest end |
#fresh?(environment) ⇒ Boolean
Checks if Asset is stale by comparing the actual mtime and digest to the inmemory model.
75 76 77 |
# File 'lib/sprockets/bundled_asset.rb', line 75 def fresh?(environment) @processed_asset.fresh?(environment) end |
#init_with(environment, coder) ⇒ Object
Initialize ‘BundledAsset` from serialized `Hash`.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sprockets/bundled_asset.rb', line 35 def init_with(environment, coder) super @processed_asset = environment.find_asset(pathname, :bundle => false) @required_assets = @processed_asset.required_assets if @processed_asset.dependency_digest != coder['required_assets_digest'] raise UnserializeError, "processed asset belongs to a stale environment" end @source = coder['source'] end |
#to_a ⇒ Object
Expand asset into an ‘Array` of parts.
69 70 71 |
# File 'lib/sprockets/bundled_asset.rb', line 69 def to_a required_assets end |