Class: HashAtPath::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_at_path/path.rb

Class Method Summary collapse

Class Method Details

.extract_path_data(path_string) ⇒ Object

Extracts paths and predicates from the provide full path



18
19
20
21
# File 'lib/hash_at_path/path.rb', line 18

def extract_path_data(path_string)
  results = array_wrap(path_string.scan(/([^\[]*)(\[)*(\d|\-\d|\*)*(\])*/i))
  results.map { |match| { path: match[0], predicate: match[2] } }
end

.parse(path_string) ⇒ Object



6
7
8
9
# File 'lib/hash_at_path/path.rb', line 6

def parse(path_string)
  path_string = replace_predicates(path_string)
  extract_path_data(path_string)
end

.replace_predicates(path_string) ⇒ Object



11
12
13
14
15
# File 'lib/hash_at_path/path.rb', line 11

def replace_predicates(path_string)
  path_string = path_string.gsub(/first\(\)/, '0')
  path_string = path_string.gsub(/last\(\)/, '-1')
  path_string
end