class Enumpath::Path::NormalizedPath
A utility for automatically normalizing string path expressions
Constants
- FILTER_EXPRESSION_REGEX
- INDEX_NOTATION_REGEX
Public Class Methods
new(path)
click to toggle source
@param path (see Enumpath::Path#initialize)
Calls superclass method
# File lib/enumpath/path/normalized_path.rb, line 11 def initialize(path) super(normalize(path)) end
Private Instance Methods
normalize(path)
click to toggle source
# File lib/enumpath/path/normalized_path.rb, line 17 def normalize(path) return path if path.is_a?(Array) normalized_path = path.dup filter_expressions = remove_filter_expressions!(normalized_path) replace_tokens!(normalized_path) restore_filter_expressions!(normalized_path, filter_expressions) remove_root!(normalized_path) normalized_path = normalized_path.split(';') # Split into segment parts normalized_path.reject! { |segment| segment.nil? || segment.size.zero? } # Remove blanks Enumpath.log('Path normalized') { { original: path, normalized: normalized_path } } normalized_path end
remove_filter_expressions!(path)
click to toggle source
Move filter expressions (`[?(expr)]`) to the temporary array and replace with an index notation
# File lib/enumpath/path/normalized_path.rb, line 32 def remove_filter_expressions!(path) filter_expressions = [] path.gsub!(FILTER_EXPRESSION_REGEX) do filter_expressions << Regexp.last_match(1) "[##{filter_expressions.size - 1}]" end filter_expressions end
remove_root!(path)
click to toggle source
# File lib/enumpath/path/normalized_path.rb, line 54 def remove_root!(path) path.gsub!(/\A\$(;|\z)/, '') # Remove root operator end
replace_tokens!(path)
click to toggle source
# File lib/enumpath/path/normalized_path.rb, line 41 def replace_tokens!(path) path.gsub!(/'?\.'?|\['?/, ';') # Replace "'?.'?" or "['?" with ";" path.gsub!(/;;;|;;/, ';..;') # Replace ";;;" or ";;" with ";..;" path.gsub!(/;\z|'?\]|'\z/, '') # Replace ";$" or "'?]" or "'$" with "" end
restore_filter_expressions!(path, filter_expressions)
click to toggle source
Replace index notations with their corresponding filter expressions (`?(expr)`) from the temporary array
# File lib/enumpath/path/normalized_path.rb, line 48 def restore_filter_expressions!(path, filter_expressions) path.gsub!(INDEX_NOTATION_REGEX) do filter_expressions[Regexp.last_match(1).to_i] end end