Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Move asset_from_hash to Asset

  • Loading branch information...
josh committed Sep 30, 2011
1 parent 5bba228 commit b042c1c9c75cf5ae7c5212323115161c5e56b1df
Showing with 22 additions and 26 deletions.
  1. +18 −3 lib/sprockets/asset.rb
  2. +1 −20 lib/sprockets/caching.rb
  3. +3 −3 test/test_asset.rb
@@ -6,9 +6,24 @@ module Sprockets
class Asset
# Internal initializer to load `Asset` from serialized `Hash`.
def self.from_hash(environment, hash)
asset = allocate
asset.init_with(environment, hash)
asset
return unless hash.is_a?(Hash)

klass = case hash['class']
when 'BundledAsset'
BundledAsset
when 'ProcessedAsset'
ProcessedAsset
when 'StaticAsset'
StaticAsset
else
nil
end

if klass
asset = klass.allocate
asset.init_with(environment, hash)
asset
end
end

attr_reader :logical_path, :pathname
@@ -1,26 +1,7 @@
require 'sprockets/bundled_asset'
require 'sprockets/processed_asset'
require 'sprockets/static_asset'

module Sprockets
# `Caching` is an internal mixin whose public methods are exposed on
# the `Environment` and `Index` classes.
module Caching
# Return `Asset` instance for serialized `Hash`.
def asset_from_hash(hash)
return unless hash.is_a?(Hash)
case hash['class']
when 'BundledAsset'
BundledAsset.from_hash(index, hash)
when 'ProcessedAsset'
ProcessedAsset.from_hash(index, hash)
when 'StaticAsset'
StaticAsset.from_hash(index, hash)
else
nil
end
end

protected
# Cache helper method. Takes a `path` argument which maybe a
# logical path or fully expanded path. The `&block` is passed
@@ -31,7 +12,7 @@ def cache_asset(path)
yield

# Check cache for `path`
elsif (asset = asset_from_hash(cache_get_hash(path.to_s))) && asset.fresh?(self)
elsif (asset = Asset.from_hash(self, cache_get_hash(path.to_s))) && asset.fresh?(self)
asset

# Otherwise yield block that slowly finds and builds the asset
@@ -421,7 +421,7 @@ def setup
expected = @asset
hash = {}
@asset.encode_with(hash)
actual = @env.asset_from_hash(hash)
actual = Sprockets::Asset.from_hash(@env, hash)

assert_kind_of Sprockets::StaticAsset, actual
assert_equal expected.logical_path, actual.logical_path
@@ -510,7 +510,7 @@ def setup
expected = @asset
hash = {}
@asset.encode_with(hash)
actual = @env.asset_from_hash(hash)
actual = Sprockets::Asset.from_hash(@env, hash)

assert_kind_of Sprockets::ProcessedAsset, actual
assert_equal expected.logical_path, actual.logical_path
@@ -827,7 +827,7 @@ def setup
expected = @asset
hash = {}
@asset.encode_with(hash)
actual = @env.asset_from_hash(hash)
actual = Sprockets::Asset.from_hash(@env, hash)

assert_kind_of Sprockets::BundledAsset, actual
assert_equal expected.logical_path, actual.logical_path

0 comments on commit b042c1c

Please sign in to comment.
You can’t perform that action at this time.