Sha256: 86da7f1766225940c4ebc2ea2d28bbbcf6349b03674a7fbc11c80f113fd55519

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

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

    attr_reader :namespace, :search

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

    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

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fixtury-2.0.2 lib/fixtury/path_resolver.rb
fixtury-2.0.0 lib/fixtury/path_resolver.rb
fixtury-1.0.1 lib/fixtury/path_resolver.rb
fixtury-1.0.0 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta7 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta6 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta5 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta4 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta3 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta2 lib/fixtury/path_resolver.rb
fixtury-1.0.0.beta1 lib/fixtury/path_resolver.rb