Sha256: df61d3e6a4def9f638e434eae2c9c89b4a343a45c28171fb77ce550d86babd07

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true
require 'sprockets/digest_utils'
require 'sprockets/path_digest_utils'
require 'sprockets/uri_utils'

module Sprockets
  # `Dependencies` is an internal mixin whose public methods are exposed on the
  # `Environment` and `CachedEnvironment` classes.
  module Dependencies
    include DigestUtils, PathDigestUtils, URIUtils

    # Public: Mapping dependency schemes to resolver functions.
    #
    # key   - String scheme
    # value - Proc.call(Environment, String)
    #
    # Returns Hash.
    def dependency_resolvers
      config[:dependency_resolvers]
    end

    # Public: Default set of dependency URIs for assets.
    #
    # Returns Set of String URIs.
    def dependencies
      config[:dependencies]
    end

    # Public: Register new dependency URI resolver.
    #
    # scheme - String scheme
    # block  -
    #   environment - Environment
    #   uri - String dependency URI
    #
    # Returns nothing.
    def register_dependency_resolver(scheme, &block)
      self.config = hash_reassoc(config, :dependency_resolvers) do |hash|
        hash.merge(scheme => block)
      end
    end

    # Public: Add environmental dependency inheirted by all assets.
    #
    # uri - String dependency URI
    #
    # Returns nothing.
    def add_dependency(uri)
      self.config = hash_reassoc(config, :dependencies) do |set|
        set + Set.new([uri])
      end
    end
    alias_method :depend_on, :add_dependency

    # Internal: Resolve dependency URIs.
    #
    # Returns resolved Object.
    def resolve_dependency(str)
      # Optimize for the most common scheme to
      # save 22k allocations on an average Spree app.
      scheme = if str.start_with?('file-digest:'.freeze)
        'file-digest'.freeze
      else
        str[/([^:]+)/, 1]
      end

      if resolver = config[:dependency_resolvers][scheme]
        resolver.call(self, str)
      else
        nil
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 6 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/sprockets-4.0.2/lib/sprockets/dependencies.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/sprockets-4.0.2/lib/sprockets/dependencies.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/sprockets-4.0.2/lib/sprockets/dependencies.rb
sprockets-4.0.2 lib/sprockets/dependencies.rb
sprockets-4.0.1 lib/sprockets/dependencies.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/sprockets-4.0.0/lib/sprockets/dependencies.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/sprockets-4.0.0/lib/sprockets/dependencies.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/sprockets-4.0.0/lib/sprockets/dependencies.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/sprockets-4.0.0/lib/sprockets/dependencies.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/sprockets-4.0.0/lib/sprockets/dependencies.rb
sprockets-4.0.0 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta10 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta9 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta8 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta7 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta6 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta5 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta4 lib/sprockets/dependencies.rb
sprockets-4.0.0.beta3 lib/sprockets/dependencies.rb