Module: CFBundle::PathUtils

Defined in:
lib/cfbundle/path_utils.rb

Overview

Utility methods for manipulating paths

Class Method Summary collapse

Class Method Details

.join(*args) ⇒ String

Returns a new path formed by joining the strings using File::SEPARATOR.

The methods also makes sure to remove any trailing separator along with path components that are empty, nil or a single dot (.) in order to generate a canonical path.

Parameters:

  • args (Array)

    An array of strings.

Returns:

  • (String)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cfbundle/path_utils.rb', line 16

def join(*args)
  args.compact!
  args.map! { |arg| arg.split(File::SEPARATOR) }
  args.flatten!
  args.reject! { |arg| arg == '.' }
  return '.' if args.empty?
  absolute = args.first == ''
  args.reject! { |arg| arg == '' }
  args.unshift('/') if absolute
  File.join(args)
end

.join_resource(directory, name, product, extension) ⇒ String

Returns a new path formed by joining the resource components.

Parameters:

  • directory (String)

    The resource’s directory.

  • name (String)

    The resource’s name.

  • product (String)

    The resource’s product. It should be empty or start with a tilde.

  • extension (String)

    The resource’s extension. It should be empty or start with a dot.

Returns:

  • (String)

See Also:



53
54
55
56
# File 'lib/cfbundle/path_utils.rb', line 53

def join_resource(directory, name, product, extension)
  filename = [name, product, extension].join
  join(directory, filename)
end

.split_resource(path) ⇒ Array

Splits the resource path into four components.

The components are the resource’s directory, name, product and extension. The product is either empty or starts with a tilde. The extension is either empty or starts with a dot.

Parameters:

  • path (String)

    The path to the resource.

Returns:

  • (Array)

See Also:



36
37
38
39
40
41
42
# File 'lib/cfbundle/path_utils.rb', line 36

def split_resource(path)
  directory = File.dirname(path)
  extension = File.extname(path)
  basename = File.basename(path, extension)
  name, product = split_resource_name_and_product(basename)
  [directory, name, product, extension]
end