Module: Dry::Files::Path

Defined in:
lib/dry/files/path.rb

Overview

Cross Operating System path

It’s used by the memory adapter to ensure that hardcoded string paths are transformed into portable paths that respect the Operating System directory separator.

Since:

  • 0.1.0

Constant Summary collapse

SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

::File::SEPARATOR

Class Method Summary collapse

Class Method Details

.absolute?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if given path is absolute

Parameters:

  • path (String, Pathname)

    the path to check

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



95
96
97
# File 'lib/dry/files/path.rb', line 95

def self.absolute?(path)
  path.start_with?(SEPARATOR)
end

.call(*path) ⇒ String Also known as: []

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transform the given path into a path that respect the Operating System directory separator.

Examples:

Portable Path

require "dry/files/path"

path = "path/to/file"

Dry::Files::Path.call(path)
  # => "path/to/file" on UNIX based Operating System

Dry::Files::Path.call(path)
  # => "path\to\file" on Windows Operating System

Join Nested Tokens

require "dry/files/path"

path = ["path", ["to", ["nested", "file"]]]

Dry::Files::Path.call(path)
  # => "path/to/nested/file" on UNIX based Operating System

Dry::Files::Path.call(path)
  # => "path\to\nested\file" on Windows Operating System

Separator path

require "dry/files/path"

path = ::File::SEPARATOR

Dry::Files::Path.call(path)
  # => ""

Parameters:

  • path (String, Pathname, Array<String,Pathname>)

    the path to transform

Returns:

  • (String)

    the resulting path

Since:

  • 0.1.0



60
61
62
63
64
65
66
67
68
69
# File 'lib/dry/files/path.rb', line 60

def call(*path)
  path = Array(path).flatten
  tokens = path.map do |token|
    split(token)
  end

  tokens
    .flatten
    .join(SEPARATOR)
end

.dirname(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all the path, except for the last token

Parameters:

  • path (String, Pathname)

    the path to extract directory name from

Returns:

  • (String)

    the directory name

Since:

  • 0.1.0



107
108
109
# File 'lib/dry/files/path.rb', line 107

def self.dirname(path)
  ::File.dirname(path)
end

.split(path) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Split path according to the current Operating System directory separator

Parameters:

  • path (String, Pathname)

    the path to split

Returns:

  • (Array<String>)

    the split path

Since:

  • 0.1.0



81
82
83
84
85
# File 'lib/dry/files/path.rb', line 81

def self.split(path)
  return EMPTY_TOKEN if path == SEPARATOR

  path.to_s.split(%r{\\|/})
end