Class: Sprockets::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, load_path = []) ⇒ Environment

Returns a new instance of Environment.



5
6
7
8
9
10
11
# File 'lib/sprockets/environment.rb', line 5

def initialize(root, load_path = [])
  @load_path = [@root = Pathname.new(self, root)]

  load_path.reverse_each do |location|
    register_load_location(location)
  end
end

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



3
4
5
# File 'lib/sprockets/environment.rb', line 3

def load_path
  @load_path
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/sprockets/environment.rb', line 3

def root
  @root
end

Instance Method Details

#constants(reload = false) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/sprockets/environment.rb', line 32

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

#find(location) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/sprockets/environment.rb', line 24

def find(location)
  if absolute?(location) && File.exists?(location)
    pathname_from(location)
  else
    find_all(location).first
  end
end

#pathname_from(location) ⇒ Object



13
14
15
# File 'lib/sprockets/environment.rb', line 13

def pathname_from(location)
  Pathname.new(self, absolute_location_from(location))
end

#register_load_location(location) ⇒ Object



17
18
19
20
21
22
# File 'lib/sprockets/environment.rb', line 17

def register_load_location(location)
  pathname = pathname_from(location)
  load_path.delete(pathname)
  load_path.unshift(pathname)
  location
end