Class: Serve::FileResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/serve/file_resolver.rb

Instance Method Summary collapse

Instance Method Details

#find_extension(extensions_to_try, full_path) ⇒ Object



34
35
36
37
38
# File 'lib/serve/file_resolver.rb', line 34

def find_extension(extensions_to_try, full_path)
  extensions_to_try.find do |ext|
    File.file?("#{full_path}.#{ext}")
  end
end

#resolve(root, path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/serve/file_resolver.rb', line 8

def resolve(root, path)
  return nil if path.nil?
  path = File.join(path) # path may be array
  return nil if path =~ /\.\./
  path = path.sub(%r{/\Z}, '')
  if path =~ /\.css\Z/ && !File.file?(File.join(root, path))
    sass_path = path.sub(/\.css\Z/, '.sass')
    sass_path if File.file?(File.join(root, sass_path))
  elsif File.directory?(File.join(root, path))
    resolve(root, File.join(path, 'index'))
  else
    resolve_with_extension(root, path)
  end
end

#resolve_with_extension(root, path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/serve/file_resolver.rb', line 23

def resolve_with_extension(root, path)
  full_path = File.join(root, path)
  if File.file?(full_path)
    path
  else
    if extension = find_extension(alternate_extensions, full_path)
      "#{path}.#{extension}"
    end
  end
end