module Collapsium::Support::PathComponents

@api private Defines functions for path prefixes and components. This is mainly used by PathedAccess, but it helps keeping everything separate so that ViralCapabilities can also apply it to Arrays.

Constants

DEFAULT_SEPARATOR

@api private Default path separator

Attributes

separator[W]

Public Instance Methods

filter_components(components) click to toggle source

Given path components, filters out unnecessary ones.

# File lib/collapsium/support/path_components.rb, line 61
def filter_components(components)
  return components.select { |c| not c.nil? and not c.empty? }
end
join_path(components) click to toggle source

Join path components with the `#separator`.

# File lib/collapsium/support/path_components.rb, line 67
def join_path(components)
  return components.join(separator)
end
normalize_path(path) click to toggle source

Normalizes a String path so that there are no empty components, and it starts with a separator.

# File lib/collapsium/support/path_components.rb, line 81
def normalize_path(path)
  components = []
  if path.respond_to?(:split) # likely a String
    components = path_components(path)
  elsif path.respond_to?(:join) # likely an Array
    components = filter_components(path)
  end
  return separator + join_path(components)
end
parent_path(path) click to toggle source

Get the parent path of the given path.

# File lib/collapsium/support/path_components.rb, line 73
def parent_path(path)
  components = path_components(normalize_path(path))
  return normalize_path(components.slice(0, components.length - 1))
end
path_components(path) click to toggle source

Break path into components. Expects a String path separated by the `#separator`, and returns the path split into components (an Array of String).

# File lib/collapsium/support/path_components.rb, line 55
def path_components(path)
  return filter_components(path.split(split_pattern))
end
path_prefix() click to toggle source
# File lib/collapsium/support/path_components.rb, line 29
def path_prefix
  @path_prefix ||= separator
  return @path_prefix
end
path_prefix=(value) click to toggle source

Assume any pathed access has this prefix.

# File lib/collapsium/support/path_components.rb, line 25
def path_prefix=(value)
  @path_prefix = normalize_path(value)
end
separator() click to toggle source

@return [String] the separator is the character or pattern splitting paths.

# File lib/collapsium/support/path_components.rb, line 45
def separator
  @separator ||= DEFAULT_SEPARATOR
  return @separator
end
split_pattern() click to toggle source

@return [RegExp] the pattern to split paths at; based on `separator`

# File lib/collapsium/support/path_components.rb, line 40
def split_pattern
  /(?<!\\)#{Regexp.escape(separator)}/
end