Class: Opal::PathReader

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/path_reader.rb

Constant Summary

RELATIVE_PATH_REGEXP =
/#{Opal::REGEXP_START}\.?\.#{Regexp.quote File::SEPARATOR}/
DEFAULT_EXTENSIONS =
['.js', '.js.rb', '.rb', '.opalerb'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(paths = Opal.paths, extensions = DEFAULT_EXTENSIONS) ⇒ PathReader

Returns a new instance of PathReader



11
12
13
14
15
# File 'opal/lib/opal/path_reader.rb', line 11

def initialize(paths = Opal.paths, extensions = DEFAULT_EXTENSIONS)
  @file_finder = Hike::Trail.new
  @file_finder.append_paths(*paths)
  @file_finder.append_extensions(*extensions)
end

Instance Method Details

#append_paths(*paths) ⇒ Object



39
40
41
# File 'opal/lib/opal/path_reader.rb', line 39

def append_paths(*paths)
  file_finder.append_paths(*paths)
end

#expand(path) ⇒ Object



23
24
25
26
27
28
29
# File 'opal/lib/opal/path_reader.rb', line 23

def expand(path)
  if Pathname.new(path).absolute? || path =~ RELATIVE_PATH_REGEXP
    path
  else
    find_path(path)
  end
end

#extensionsObject



35
36
37
# File 'opal/lib/opal/path_reader.rb', line 35

def extensions
  file_finder.extensions
end

#pathsObject



31
32
33
# File 'opal/lib/opal/path_reader.rb', line 31

def paths
  file_finder.paths
end

#read(path) ⇒ Object



17
18
19
20
21
# File 'opal/lib/opal/path_reader.rb', line 17

def read(path)
  full_path = expand(path)
  return nil if full_path.nil?
  File.open(full_path, 'rb:UTF-8', &:read)
end