Sha256: 97ff1baa40330d14b48c0d070d4f7afd83a0225c1457e8f5f3d0d8042c14492f

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module Sprockets
  class Environment
    attr_reader :root, :load_path
    
    def initialize(root, load_path = [])
      @load_path = [@root = Pathname.new(self, root)]

      load_path.reverse_each do |location|
        register_load_location(location)
      end
    end
    
    def pathname_from(location)
      Pathname.new(self, absolute_location_from(location))
    end

    def register_load_location(location)
      pathname = pathname_from(location)
      load_path.delete(pathname)
      load_path.unshift(pathname)
      location
    end
    
    def find(location)
      if absolute?(location) && File.exists?(location)
        pathname_from(location)
      else
        find_all(location).first
      end
    end
    
    def constants(reload = false)
      @constants = nil if reload
      @constants ||= find_all("constants.yml").inject({}) do |constants, pathname|
        contents = YAML.load(pathname.contents) rescue nil
        contents = {} unless contents.is_a?(Hash)
        constants.merge(contents)
      end
    end
    
    protected
      def absolute?(location)
        location[0, 1] == File::SEPARATOR
      end
      
      def absolute_location_from(location)
        location = location.to_s
        location = File.join(root.absolute_location, location) unless absolute?(location)
        File.expand_path(location)
      end
      
      def find_all(location)
        load_path.map { |pathname| pathname.find(location) }.compact
      end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sstephenson-sprockets-0.4.0 lib/sprockets/environment.rb
sstephenson-sprockets-0.5.0 lib/sprockets/environment.rb
sstephenson-sprockets-0.9.0 lib/sprockets/environment.rb
sprockets-0.9.0 lib/sprockets/environment.rb