Sha256: 296d04dff9bea8f5e265f72b815b0eba03f2edf0065d66944e1284f9309d0a3a

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

require "pathname"

module Sprockets
  class Plugin
    require "sprockets/plugin/version"
    require "sprockets/plugin/aware"
    
    class << self
      def inherited(plugin)
        plugins << plugin
      end
      
      # Returns all of the plugins inheriting from
      # Sprockets::Plugin.
      def plugins
        @@plugins ||= []
      end
      
      # Sets the root path or returns the current one.
      # A root path is required for appending relative
      # paths.
      def root(path = nil)
        if path
          @root = Pathname.new(path).expand_path
        else
          @root
        end
      end
      
      # Appends a path to the Plugin. The path will
      # later be appended to the Sprockets::Environment.
      def append_paths(*paths)
        self.paths.push *normalize_paths(paths)
      end
      alias_method :append_path, :append_paths
      
      # Prepends a path to the Plugin. The path will
      # later be appended to the Sprockets::Environment.
      def prepend_paths(*paths)
        self.paths.unshift *normalize_paths(paths)
      end
      alias_method :prepend_path, :prepend_paths
    
      # All of the paths registered by the plugin.
      def paths
        @paths ||= []
      end
      
      protected
      
      def normalize_paths(paths)
        normalized_paths = []
        paths.each do |path|
          path = Pathname.new(path)
          path = root.join(path) if root && path.relative?
          path = path.expand_path
          normalized_paths.push(path.to_s) if path.exist?
        end
        normalized_paths
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sprockets-plugin-0.1.2 lib/sprockets/plugin.rb
sprockets-plugin-0.1.1 lib/sprockets/plugin.rb