Module: Staticky::Files::Path

Defined in:
lib/staticky/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.

Constant Summary collapse

SEPARATOR =
::File::SEPARATOR
EMPTY_TOKEN =
""

Class Method Summary collapse

Class Method Details

.absolute?(path) ⇒ TrueClass, FalseClass

Check if given path is absolute

Parameters:

  • path (String, Pathname)

    the path to check

Returns:

  • (TrueClass, FalseClass)

    the result of the check



81
82
83
# File 'lib/staticky/files/path.rb', line 81

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

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

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

transform

Examples:

Portable Path

require "staticky/files/path"

path = "path/to/file"

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

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

Join Nested Tokens

require "staticky/files/path"

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

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

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

Separator path

require "staticky/files/path"

path = ::File::SEPARATOR

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

Parameters:

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

    the path to

Returns:

  • (String)

    the resulting path



52
53
54
55
56
57
58
59
60
61
# File 'lib/staticky/files/path.rb', line 52

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

  tokens
    .flatten
    .join(SEPARATOR)
end

.dirname(path) ⇒ String

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



90
91
92
# File 'lib/staticky/files/path.rb', line 90

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

.split(path) ⇒ Array<String>

Split path according to the current Operating System directory separator

Parameters:

  • path (String, Pathname)

    the path to split

Returns:

  • (Array<String>)

    the split path



70
71
72
73
74
# File 'lib/staticky/files/path.rb', line 70

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

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