Class: Mayu::Resources::Resolver::Filesystem

Inherits:
Base
  • Object
show all
Defined in:
lib/mayu/resources/resolver/filesystem.rb

Instance Attribute Summary

Attributes inherited from Base

#resolved_paths

Instance Method Summary collapse

Constructor Details

#initialize(root, extensions: [""]) ⇒ Filesystem

Returns a new instance of Filesystem.



11
12
13
14
15
# File 'lib/mayu/resources/resolver/filesystem.rb', line 11

def initialize(root, extensions: [""])
  super()
  @root = root
  @extensions = extensions
end

Instance Method Details

#resolve(path, source_dir = "/") ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mayu/resources/resolver/filesystem.rb', line 20

def resolve(path, source_dir = "/")
  # TODO: Fix this!!
  # if path.start_with?("/")
  #   return resolve(".#{path}", "/")
  # end
  #
  # if !path.match(/\A\.\.?\//)
  #   return resolve("./#{path}", "/components")
  # end

  relative_to_root = File.absolute_path(path, source_dir)

  if found = @resolved_paths[relative_to_root]
    return found
  end

  absolute_path = File.join(@root, relative_to_root)

  resolve_with_extensions(absolute_path) do |extension|
    return(
      @resolved_paths.store(
        relative_to_root,
        relative_to_root + extension
      )
    )
  end

  if File.directory?(absolute_path)
    basename = File.basename(absolute_path)

    resolve_with_extensions(
      File.join(absolute_path, basename)
    ) do |extension|
      return(
        @resolved_paths.store(
          relative_to_root,
          File.join(relative_to_root, basename) + extension
        )
      )
    end
  end

  raise ResolveError,
        "Could not resolve #{path} from #{source_dir} (app root: #{@root})"
end