Class: Fixtury::PathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/fixtury/path_resolver.rb

Overview

Takes a namespace as context and a search string and resolves the possible absolute paths that a user could be referring to.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, search:) ⇒ PathResolver

Returns a new instance of PathResolver.



10
11
12
13
# File 'lib/fixtury/path_resolver.rb', line 10

def initialize(namespace:, search:)
  @namespace = namespace.to_s
  @search = search.to_s
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



8
9
10
# File 'lib/fixtury/path_resolver.rb', line 8

def namespace
  @namespace
end

#searchObject (readonly)

Returns the value of attribute search.



8
9
10
# File 'lib/fixtury/path_resolver.rb', line 8

def search
  @search
end

Instance Method Details

#possible_absolute_pathsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fixtury/path_resolver.rb', line 15

def possible_absolute_paths
  @possible_absolute_paths ||= begin
    out = []
    # If the search starts with a slash it's an absolute
    # path and it should be the only possible path.
    if search.start_with?("/")
      out << search

    # Otherwise we need to consider the namespace.
    else
      # Try the namespace as a prefix for the search.
      # This should take priority because it is the most specific.
      out << ::File.join(namespace, search)

      # In addition, someone may be referencing a path relative
      # to root but not including the leading slash. We should
      # consider this case as well.
      out << ::File.join("/", search) unless search.include?(".")
    end

    # Get rid of any `.` and `..` in the paths.
    out.map! { |path| File.expand_path(path, "/").to_s }
    # Get rid of any duplicates.
    out.uniq!
    # voila
    out
  end
end