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

Separate asset source, length, and digest from default serialized att…

…ributes
  • Loading branch information...
josh committed Jul 8, 2011
1 parent fa333dc commit 15850a850ac5c3b2c099d5ae78f61a471e2820b5
Showing with 24 additions and 9 deletions.
  1. +4 −5 lib/sprockets/asset.rb
  2. +15 −4 lib/sprockets/bundled_asset.rb
  3. +5 −0 lib/sprockets/static_asset.rb
@@ -13,8 +13,7 @@ def self.from_hash(environment, hash)
# Define base set of attributes to be serialized.
def self.serialized_attributes
%w( environment_hexdigest
logical_path pathname
content_type mtime length digest )
logical_path pathname )
end

attr_reader :environment
@@ -34,17 +33,17 @@ def init_with(environment, coder)
instance_variable_set("@#{attr}", coder[attr].to_s) if coder[attr]
end

if @pathname
if @pathname && @pathname.is_a?(String)
# Expand `$root` placeholder and wrapper string in a `Pathname`
@pathname = Pathname.new(expand_root_path(@pathname))
end

if @mtime
if @mtime && @mtime.is_a?(String)
# Parse time string
@mtime = Time.parse(@mtime)
end

if @length
if @length && @length.is_a?(String)
# Convert length to an `Integer`
@length = Integer(@length)
end
@@ -8,19 +8,27 @@ module Sprockets
# `BundledAsset`s are used for files that need to be processed and
# concatenated with other assets. Use for `.js` and `.css` files.
class BundledAsset < Asset
# Define extra attributes to be serialized.
def self.serialized_attributes
super + %w( content_type mtime )
end

def initialize(environment, logical_path, pathname, options)
super(environment, logical_path, pathname)
@options = options || {}
end

# Initialize `BundledAsset` from serialized `Hash`.
def init_with(environment, coder)
super

@options = {}

@body = coder['body']
@length = coder['length']
@digest = coder['digest']
@source = coder['source']

super

@body = coder['body']
@assets = coder['asset_paths'].map { |p|
p = expand_root_path(p)
p == pathname.to_s ? self : environment[p, @options]
@@ -36,10 +44,13 @@ def init_with(environment, coder)

# Serialize custom attributes in `BundledAsset`.
def encode_with(coder)
coder['length'] = length
coder['digest'] = digest
coder['source'] = to_s

super

coder['body'] = body
coder['source'] = to_s
coder['asset_paths'] = to_a.map { |a| relativize_root_path(a.pathname) }
coder['dependency_files'] = dependency_files.map { |h|
h.merge('path' => relativize_root_path(h['path']))
@@ -7,6 +7,11 @@ module Sprockets
# any processing or concatenation. These are typical images and
# other binary files.
class StaticAsset < Asset
# Define extra attributes to be serialized.
def self.serialized_attributes
super + %w( content_type mtime length digest )
end

def initialize(environment, logical_path, pathname, digest = nil)
super(environment, logical_path, pathname)
@digest = digest

0 comments on commit 15850a8

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