Class: Sprockets::AssetAttributes

### Home Classes Methods

### In Files

- lib/sprockets/asset_attributes.rb

### Parent

Object

### Methods

- ::new
- #content_type
- #engine_extensions
- #engines
- #extensions
- #format_extension
- #logical_path
- #processors
- #search_paths

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::AssetAttributes
- Sprockets::Base
- Sprockets::BundledAsset
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Caching
- Sprockets::CharsetNormalizer
- Sprockets::CircularDependencyError
- Sprockets::ClosureCompressor
- Sprockets::Compressing
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::DirectiveProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsTemplate
- Sprockets::EncodingError
- Sprockets::EngineError
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::Manifest
- Sprockets::Mime
- Sprockets::NotImplementedError
- Sprockets::Paths
- Sprockets::ProcessedAsset
- Sprockets::ProcessedAsset::DependencyFile
- Sprockets::Processing
- Sprockets::Processor
- Sprockets::SafetyColons
- Sprockets::SassCacheStore
- Sprockets::SassCompressor
- Sprockets::SassFunctions
- Sprockets::SassImporter
- Sprockets::SassTemplate
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::StaticAsset
- Sprockets::UglifierCompressor
- Sprockets::UnserializeError
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::AssetAttributes

`AssetAttributes` is a wrapper similar to `Pathname` that provides some helper accessors.

These methods should be considered internalish.

### Attributes

environment[R]
pathname[R]

### Public Class Methods

new(environment, path) click to toggle source

    # File lib/sprockets/asset_attributes.rb, line 11
    def initialize(environment, path)
      @environment = environment
      @pathname = path.is_a?(Pathname) ? path : Pathname.new(path.to_s)
    end

### Public Instance Methods

content_type() click to toggle source

Returns the content type for the pathname. Falls back to `application/octet-stream`.

    # File lib/sprockets/asset_attributes.rb, line 105
    def content_type
      @content_type ||= begin
        if format_extension.nil?
          engine_content_type || 'application/octet-stream'
        else
          @environment.mime_types(format_extension) ||
            engine_content_type ||
            'application/octet-stream'
        end
      end
    end

engine_extensions() click to toggle source

Returns an `Array` of engine extensions.

    "foo.js.coffee.erb"
    # => [".coffee", ".erb"]

    # File lib/sprockets/asset_attributes.rb, line 82
    def engine_extensions
      exts = extensions

      if offset = extensions.index(format_extension)
        exts = extensions[offset+1..-1]
      end

      exts.select { |ext| @environment.engines(ext) }
    end

engines() click to toggle source

Returns engine classes.

    # File lib/sprockets/asset_attributes.rb, line 93
    def engines
      engine_extensions.map { |ext| @environment.engines(ext) }
    end

extensions() click to toggle source

Returns `Array` of extension `String`s.

    "foo.js.coffee"
    # => [".js", ".coffee"]

    # File lib/sprockets/asset_attributes.rb, line 62
    def extensions
      @extensions ||= @pathname.basename.to_s.scan(/\.[^.]+/)
    end

format_extension() click to toggle source

Returns the format extension.

    "foo.js.coffee"
    # => ".js"

    # File lib/sprockets/asset_attributes.rb, line 71
    def format_extension
      extensions.reverse.detect { |ext|
        @environment.mime_types(ext) && !@environment.engines(ext)
      }
    end

logical_path() click to toggle source

Reverse guess logical path for fully expanded path.

This has some known issues. For an example if a file is shaddowed in the path, but is required relatively, its logical path will be incorrect.

    # File lib/sprockets/asset_attributes.rb, line 45
    def logical_path
      if root_path = environment.paths.detect { |path| pathname.to_s[path] }
        path = pathname.to_s.sub("#{root_path}/", '')
        path = pathname.relative_path_from(Pathname.new(root_path)).to_s
        path = engine_extensions.inject(path) { |p, ext| p.sub(ext, '') }
        path = "#{path}#{engine_format_extension}" unless format_extension
        path
      else
        raise FileOutsidePaths, "#{pathname} isn't in paths: #{environment.paths.join(', ')}"
      end
    end

processors() click to toggle source

Returns all processors to run on the path.

    # File lib/sprockets/asset_attributes.rb, line 98
    def processors
      environment.preprocessors(content_type) +
        engines.reverse +
        environment.postprocessors(content_type)
    end

search_paths() click to toggle source

Returns paths search the load path for.

    # File lib/sprockets/asset_attributes.rb, line 17
    def search_paths
      paths = [pathname.to_s]

      extension = format_extension
      path_without_extension = extension ?
        pathname.sub(extension, '') :
        pathname

      # optimization: bower.json can only be nested one level deep
      if !path_without_extension.to_s.index('/')
        paths << path_without_extension.join(".bower.json").to_s
        paths << path_without_extension.join("bower.json").to_s
        # DEPRECATED bower configuration file
        paths << path_without_extension.join("component.json").to_s
      end

      if pathname.basename(extension.to_s).to_s != 'index'
        paths << path_without_extension.join("index#{extension}").to_s
      end

      paths
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.