Sha256: c3af2266c5d0d1c08d65090d049f22363b5fe68b4839bb15768e324bd7a25aaa

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Sprockets
  class Secretary
    DEFAULT_OPTIONS = {
      :root         => ".",
      :load_path    => [],
      :source_files => [],
      :expand_paths => true
    }

    attr_reader :environment, :preprocessor
    
    def initialize(options = {})
      @options = DEFAULT_OPTIONS.merge(options)
      @environment  = Sprockets::Environment.new(@options[:root])
      @preprocessor = Sprockets::Preprocessor.new(@environment)

      add_load_locations(@options[:load_path])
      add_source_files(@options[:source_files])
    end

    def add_load_location(load_location, options = {})
      add_load_locations([load_location], options)
    end

    def add_load_locations(load_locations, options = {})
      expand_paths(load_locations, options).each do |load_location|
        environment.register_load_location(load_location)
      end
    end
    
    def add_source_file(source_file, options = {})
      add_source_files([source_file], options)
    end
    
    def add_source_files(source_files, options = {})
      expand_paths(source_files, options).each do |source_file|
        if pathname = environment.find(source_file)
          preprocessor.require(pathname.source_file)
        else
          raise Sprockets::LoadError, "no such file `#{source_file}'"
        end
      end
    end
    
    def output_file
      preprocessor.output_file
    end
    
    protected
      def expand_paths(paths, options = {})
        if options.has_key?(:expand_paths) ? options[:expand_paths] : @options[:expand_paths]
          paths.map { |path| Dir[from_root(path)].sort }.flatten.compact
        else
          paths.map { |path| from_root(path) }
        end
      end
      
      def from_root(path)
        if path[0, 1] == File::SEPARATOR
          path
        else
          File.join(@options[:root], path)
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sstephenson-sprockets-0.4.0 lib/sprockets/secretary.rb