Permalink
Browse files

AssetPathname -> AssetAttributes

  • Loading branch information...
1 parent 70241d6 commit 20d77448b5f838af99e3aef81958ee0435f846ea @sstephenson sstephenson committed May 21, 2011
View
2 lib/sprockets.rb
@@ -1,7 +1,7 @@
# autoload the various classes used in Sprockets
module Sprockets
autoload :ArgumentError, "sprockets/errors"
- autoload :AssetPathname, "sprockets/asset_pathname"
+ autoload :AssetAttributes, "sprockets/asset_attributes"
autoload :BundledAsset, "sprockets/bundled_asset"
autoload :CharsetNormalizer, "sprockets/charset_normalizer"
autoload :CircularDependencyError, "sprockets/errors"
View
10 lib/sprockets/asset_pathname.rb → lib/sprockets/asset_attributes.rb
@@ -1,14 +1,12 @@
require 'pathname'
module Sprockets
- class AssetPathname
- def self.new(path, environment)
- path.is_a?(self) ? path : super(path, environment)
- end
+ class AssetAttributes
+ attr_reader :environment, :pathname
- def initialize(path, environment)
- @pathname = path.is_a?(Pathname) ? path : Pathname.new(path.to_s)
+ def initialize(environment, path)
@environment = environment
+ @pathname = path.is_a?(Pathname) ? path : Pathname.new(path.to_s)
end
def basename_without_extensions
View
3 lib/sprockets/bundled_asset.rb
@@ -1,4 +1,3 @@
-require 'sprockets/asset_pathname'
require 'sprockets/errors'
require 'digest/md5'
require 'set'
@@ -16,7 +15,7 @@ def initialize(environment, logical_path, pathname, options)
@logical_path = logical_path.to_s
@pathname = pathname
- @content_type = AssetPathname.new(pathname, environment).content_type
+ @content_type = environment.content_type_of(pathname)
@assets = []
@source = nil
View
23 lib/sprockets/context.rb
@@ -1,4 +1,3 @@
-require 'sprockets/asset_pathname'
require 'sprockets/errors'
require 'pathname'
require 'set'
@@ -30,28 +29,28 @@ def logical_path
end
def content_type
- AssetPathname.new(pathname, environment).content_type
+ environment.content_type_of(pathname)
end
def resolve(path, options = {}, &block)
- pathname = Pathname.new(path)
- asset_pathname = AssetPathname.new(pathname, environment)
+ pathname = Pathname.new(path)
+ attributes = environment.attributes_for(pathname)
if pathname.absolute?
pathname
elsif content_type = options[:content_type]
content_type = self.content_type if content_type == :self
- if asset_pathname.format_extension
- if content_type != asset_pathname.content_type
+ if attributes.format_extension
+ if content_type != attributes.content_type
raise ContentTypeMismatch, "#{path} is " +
- "'#{asset_pathname.content_type}', not '#{content_type}'"
+ "'#{attributes.content_type}', not '#{content_type}'"
end
end
resolve(path) do |candidate|
- if self.content_type == AssetPathname.new(candidate, environment).content_type
+ if self.content_type == environment.content_type_of(candidate)
return candidate
end
end
@@ -67,12 +66,12 @@ def depend_on(path)
end
def evaluate(filename, options = {})
- pathname = resolve(filename)
- asset_pathname = AssetPathname.new(pathname, environment)
+ pathname = resolve(filename)
+ attributes = environment.attributes_for(pathname)
data = options[:data] || pathname.read
engines = options[:engines] || environment.processors(content_type) +
- asset_pathname.engines.reverse
+ attributes.engines.reverse
engines.inject(data) do |result, engine|
template = engine.new(pathname.to_s) { result }
@@ -82,7 +81,7 @@ def evaluate(filename, options = {})
def asset_requirable?(path)
pathname = resolve(path)
- content_type = AssetPathname.new(pathname, environment).content_type
+ content_type = environment.content_type_of(pathname)
pathname.file? && (self.content_type.nil? || self.content_type == content_type)
end
View
10 lib/sprockets/environment.rb
@@ -1,4 +1,4 @@
-require 'sprockets/asset_pathname'
+require 'sprockets/asset_attributes'
require 'sprockets/context'
require 'sprockets/directive_processor'
require 'sprockets/environment_index'
@@ -102,6 +102,14 @@ def find_asset(logical_path, options = {})
end
alias_method :[], :find_asset
+ def attributes_for(path)
+ AssetAttributes.new(self, path)
+ end
+
+ def content_type_of(path)
+ attributes_for(path).content_type
+ end
+
protected
def expire_index!
@cache = {}
View
26 lib/sprockets/environment_index.rb
@@ -1,4 +1,4 @@
-require 'sprockets/asset_pathname'
+require 'sprockets/asset_attributes'
require 'sprockets/bundled_asset'
require 'sprockets/errors'
require 'sprockets/static_asset'
@@ -75,6 +75,14 @@ def find_asset(path, options = {})
end
alias_method :[], :find_asset
+ def attributes_for(path)
+ AssetAttributes.new(self, path)
+ end
+
+ def content_type_of(path)
+ attributes_for(path).content_type
+ end
+
protected
def expire_index!
raise TypeError, "can't modify immutable index"
@@ -104,10 +112,9 @@ def build_asset(logical_path, pathname, options)
return asset
end
- pathname = Pathname.new(pathname)
- asset_pathname = AssetPathname.new(pathname, self)
+ pathname = Pathname.new(pathname)
- if processors(asset_pathname.content_type).any?
+ if processors(content_type_of(pathname)).any?
logger.info "[Sprockets] #{logical_path} building"
asset = BundledAsset.new(self, logical_path, pathname, options)
else
@@ -119,13 +126,13 @@ def build_asset(logical_path, pathname, options)
private
def logical_index_path(logical_path)
- pathname = Pathname.new(logical_path)
- asset_pathname = AssetPathname.new(logical_path, self)
+ pathname = Pathname.new(logical_path)
+ attributes = attributes_for(logical_path)
- if asset_pathname.basename_without_extensions.to_s == 'index'
+ if attributes.basename_without_extensions.to_s == 'index'
logical_path
else
- basename = "#{asset_pathname.basename_without_extensions}/index#{asset_pathname.extensions.join}"
+ basename = "#{attributes.basename_without_extensions}/index#{attributes.extensions.join}"
pathname.dirname.to_s == '.' ? basename : pathname.dirname.join(basename).to_s
end
end
@@ -139,8 +146,7 @@ def detect_logical_path(filename)
end
def path_without_engine_extensions(pathname)
- asset_pathname = AssetPathname.new(pathname, self)
- asset_pathname.engine_extensions.inject(pathname) do |p, ext|
+ attributes_for(pathname).engine_extensions.inject(pathname) do |p, ext|
p.sub(ext, '')
end
end
View
5 lib/sprockets/static_asset.rb
@@ -1,4 +1,3 @@
-require 'sprockets/asset_pathname'
require 'digest/md5'
require 'time'
@@ -10,9 +9,7 @@ class StaticAsset
def initialize(environment, logical_path, pathname, digest = nil)
@logical_path = logical_path.to_s
@pathname = Pathname.new(pathname)
-
- asset_pathname = AssetPathname.new(pathname, environment)
- @content_type = asset_pathname.content_type
+ @content_type = environment.content_type_of(pathname)
@mtime = @pathname.mtime
@length = @pathname.size
View
9 lib/sprockets/static_compilation.rb
@@ -1,4 +1,3 @@
-require 'sprockets/asset_pathname'
require 'fileutils'
require 'pathname'
@@ -42,8 +41,8 @@ def precompile(*paths)
def find_asset_in_static_root(logical_path)
return unless static_root
- pathname = Pathname.new(static_root.join(logical_path))
- asset_pathname = AssetPathname.new(pathname, self)
+ pathname = Pathname.new(static_root.join(logical_path))
+ attributes = attributes_for(pathname)
entries = entries(pathname.dirname)
@@ -52,9 +51,9 @@ def find_asset_in_static_root(logical_path)
end
if !path_fingerprint(pathname)
- pattern = /^#{Regexp.escape(asset_pathname.basename_without_extensions.to_s)}
+ pattern = /^#{Regexp.escape(attributes.basename_without_extensions.to_s)}
-([0-9a-f]{7,40})
- #{Regexp.escape(asset_pathname.extensions.join)}$/x
+ #{Regexp.escape(attributes.extensions.join)}$/x
entries.each do |filename|
if filename.to_s =~ pattern
View
9 test/test_asset_pathname.rb → test/test_asset_attributes.rb
@@ -1,11 +1,6 @@
require 'sprockets_test'
-class TestAssetPathname < Sprockets::TestCase
- test "identity initialization" do
- path = pathname("javascripts/application.js.coffee")
- assert pathname(path).equal?(path)
- end
-
+class TestAssetAttributes < Sprockets::TestCase
test "basename with extensions" do
assert_equal "empty",
pathname("empty").basename_without_extensions.to_s
@@ -86,6 +81,6 @@ class TestAssetPathname < Sprockets::TestCase
private
def pathname(path)
- Sprockets::AssetPathname.new(path, Sprockets::Environment.new)
+ Sprockets::Environment.new.attributes_for(path)
end
end

0 comments on commit 20d7744

Please sign in to comment.