Module: Sprockets::Utils

### Home Classes Methods

### In Files

- lib/sprockets/utils.rb

### Methods

- ::normalize_extension
- ::read_unicode

### 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::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::ProcessedAsset
- Sprockets::ProcessedAsset::DependencyFile
- Sprockets::Processing
- Sprockets::Processor
- Sprockets::SafetyColons
- Sprockets::Server
- Sprockets::StaticAsset
- Sprockets::Trail
- Sprockets::UnserializeError
- Sprockets::Utils
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Utils

`Utils`, we didn't know where else to put it!

### Constants

UTF16_BOM_PATTERN
UTF8_BOM_PATTERN

  Define UTF-8 BOM pattern matcher. Avoid using a Regexp literal because it inheirts the files encoding and we want to avoid syntax errors in other interpreters.

### Public Class Methods

normalize_extension(extension) click to toggle source

Prepends a leading "." to an extension if its missing.

    normalize_extension("js")
    # => ".js"

    normalize_extension(".css")
    # => ".css"

    # File lib/sprockets/utils.rb, line 60
    def self.normalize_extension(extension)
      extension = extension.to_s
      if extension[/^\./]
        extension
      else
        ".#{extension}"
      end
    end

read_unicode(pathname, external_encoding = Encoding.default_external) click to toggle source

    # File lib/sprockets/utils.rb, line 11
    def self.read_unicode(pathname, external_encoding = Encoding.default_external)
      pathname.open("r:#{external_encoding}") do |f|
        f.read.tap do |data|
          # Eager validate the file's encoding. In most cases we
          # expect it to be UTF-8 unless `default_external` is set to
          # something else. An error is usually raised if the file is
          # saved as UTF-16 when we expected UTF-8.
          if !data.valid_encoding?
            raise EncodingError, "#{pathname} has a invalid " +
              "#{data.encoding} byte sequence"

          # If the file is UTF-8 and theres a BOM, strip it for safe concatenation.
          elsif data.encoding.name == "UTF-8" && data =~ UTF8_BOM_PATTERN
            data.sub!(UTF8_BOM_PATTERN, "")
          end
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.