Module: Sprockets::Utils

### Home Classes Methods

### In Files

- lib/sprockets/utils.rb

### Methods

- #concat_javascript_sources
- #dfs
- #dfs_paths
- #duplicable?
- #hash_reassoc
- #hash_reassoc1
- #module_include
- #normalize_extension
- #string_end_with_semicolon?

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Utils

Internal: Utils, we didn't know where else to put it! Functions may eventually be shuffled into more specific drawers.

### Constants

UNBOUND_METHODS_BIND_TO_ANY_OBJECT

  Internal: Feature detect if UnboundMethods can bind to any Object or just Objects that share the same super class. Basically if RUBY_VERSION >= 2.

### Public Instance Methods

concat_javascript_sources(buf, source) click to toggle source

Internal: Accumulate asset source to buffer and append a trailing semicolon if necessary.

buf - String buffer to append to source - String source to append

Returns buf String.

    # File lib/sprockets/utils.rb, line 97
    def concat_javascript_sources(buf, source)
      if buf.bytesize > 0
        buf << ";" unless string_end_with_semicolon?(buf)
        buf << "\n" unless buf.end_with?("\n")
      end
      buf << source
    end

dfs(initial) click to toggle source

Internal: Post-order Depth-First search algorithm.

Used for resolving asset dependencies.

initial - Initial Array of nodes to traverse. block -

    node  - Current node to get children of

Returns a Set of nodes.

    # File lib/sprockets/utils.rb, line 173
    def dfs(initial)
      nodes, seen = Set.new, Set.new
      stack = Array(initial).reverse

      while node = stack.pop
        if seen.include?(node)
          nodes.add(node)
        else
          seen.add(node)
          stack.push(node)
          stack.concat(Array(yield node).reverse)
        end
      end

      nodes
    end

dfs_paths(path) click to toggle source

Internal: Post-order Depth-First search algorithm that gathers all paths along the way.

TODO: Rename function.

path - Initial Array node path block -

    node - Current node to get children of

Returns an Array of node Arrays.

    # File lib/sprockets/utils.rb, line 200
    def dfs_paths(path)
      paths = []
      stack, seen = [path], Set.new

      while path = stack.pop
        if !seen.include?(path.last)
          seen.add(path.last)
          paths << path if path.size > 1

          Array(yield path.last).reverse_each do |node|
            stack.push(path + [node])
          end
        end
      end

duplicable?(obj) click to toggle source

Internal: Check if object can safely be .dup'd.

Similar to ActiveSupport duplicable? check.

obj - Any Object

Returns false if .dup would raise a TypeError, otherwise true.

    # File lib/sprockets/utils.rb, line 16
    def duplicable?(obj)
      case obj
      when NilClass, FalseClass, TrueClass, Symbol, Numeric
        false
      else
        true
      end
    end

hash_reassoc(hash, *keys, &block) click to toggle source

Internal: Duplicate and store key/value on new frozen hash.

Similar to Hash#store for nested frozen hashes.

hash - Hash key - Object keys. Use multiple keys for nested hashes. block - Receives current value at key.

Examples

    config = {paths: ["/bin", "/sbin"]}.freeze
    new_config = hash_reassoc(config, :paths) do |paths|
      paths << "/usr/local/bin"
    end

Returns duplicated frozen Hash.

    # File lib/sprockets/utils.rb, line 59
    def hash_reassoc(hash, *keys, &block)
      if keys.size == 1
        hash_reassoc1(hash, keys[0], &block)
      else
        hash_reassoc1(hash, keys[0]) do |value|
          hash_reassoc(value, *keys[1..-1], &block)
        end
      end
    end

hash_reassoc1(hash, key) click to toggle source

Internal: Duplicate and store key/value on new frozen hash.

Seperated for recursive calls, always use hash_reassoc(hash, *keys).

hash - Hash key - Object key

Returns Hash.

    # File lib/sprockets/utils.rb, line 33
    def hash_reassoc1(hash, key)
      hash = hash.dup if hash.frozen?
      old_value = hash[key]
      old_value = old_value.dup if duplicable?(old_value)
      new_value = yield old_value
      new_value.freeze if duplicable?(new_value)
      hash.store(key, new_value)
      hash.freeze
    end

module_include(base, mod) click to toggle source

Internal: Inject into target module for the duration of the block.

mod - Module

Returns result of block.

    # File lib/sprockets/utils.rb, line 138
    def module_include(base, mod)
      old_methods = {}

      mod.instance_methods.each do |sym|
        old_methods[sym] = base.instance_method(sym) if base.method_defined?(sym)
      end

      unless UNBOUND_METHODS_BIND_TO_ANY_OBJECT
        base.send(:include, mod) unless base < mod
      end

      mod.instance_methods.each do |sym|
        method = mod.instance_method(sym)
        base.send(:define_method, sym, method)
      end

      yield
    ensure
      mod.instance_methods.each do |sym|
        base.send(:undef_method, sym) if base.method_defined?(sym)
      end
      old_methods.each do |sym, method|
        base.send(:define_method, sym, method)
      end
    end

normalize_extension(extension) click to toggle source

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

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

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

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

string_end_with_semicolon?(str) click to toggle source

Internal: Check if string has a trailing semicolon.

str - String

Returns true or false.

    # File lib/sprockets/utils.rb, line 74
    def string_end_with_semicolon?(str)
      i = str.size - 1
      while i >= 0
        c = str[i]
        i -= 1
        if c == "\n" || c == " " || c == "\t"
          next
        elsif c != ";"
          return false
        else
          return true
        end
      end
      true
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.