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
Public Instance Methods
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 with the `#separator`.
# File lib/collapsium/support/path_components.rb, line 67 def join_path(components) return components.join(separator) end
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
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
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
# File lib/collapsium/support/path_components.rb, line 29 def path_prefix @path_prefix ||= separator return @path_prefix end
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
@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
@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