Module: Extensions::PathnameHelpers::Pathname::ClassMethods

Defined in:
lib/extensions/pathname_helpers/pathname.rb

Instance Method Summary collapse

Instance Method Details

#normalize_filename(filename) ⇒ String

Normalizes the filename. Replace following characters with whitespace: / \ ? * : | " < > Remove leading and trailing whitespaces. Collapse intra-string whitespaces into single whitespace. Remove trailing dots.

Parameters:

  • filename (String)

Returns:

  • (String)

    The normalized filename.


12
13
14
# File 'lib/extensions/pathname_helpers/pathname.rb', line 12

def normalize_filename(filename)
  filename.tr('/"?*:|<>\\', ' ').squish.gsub(/\.+$/, '')
end

#normalize_path(path) ⇒ String

Normalizes the path. Replace '\' with '/'. Collapse adjacent slashes('/') into single slash. Adopt +normalize_filename+ to each part of the path.

Parameters:

  • path (String)

Returns:

  • (String)

    The normalized path.


23
24
25
26
27
# File 'lib/extensions/pathname_helpers/pathname.rb', line 23

def normalize_path(path)
  path.tr('\\', '/').
    gsub(/\/+/, '/').
    split('/').map { |name| normalize_filename(name) }.join('/')
end