Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby yaml ypath like xpath?

Tags:

ruby

yaml

Hi i have a yaml file like so

---
data:
  - date: "2004-06-11"
    description: First description

  - date: "2008-01-12"
    description: Another descripion

How can i do a "ypath" query similar to xpath for xml ? Something like "get the description where the date is 2004-06-11"

YAML.parse_file('myfile.yml').select('/data/*/date == 2004-06-11')

How do you do it, and if that's possible how can i similarly edit the description by 'ypath' ?

Thanks

like image 912
gurpal2000 Avatar asked Mar 20 '10 16:03

gurpal2000


2 Answers

There is indeed such thing as YPath: github.com/peterkmurphy/YPath-Specification

And it's implemented in Ruby's YAML lib; see the doc for BaseNode#search.

like image 62
Noé Rubinstein Avatar answered Oct 25 '22 00:10

Noé Rubinstein


If Ruby is not a hard constraint you might take a look at the dpath tool. It provides an xpath-like query language to YAML (and other) files. Maybe call it externally to filter your data.

like image 23
Renormalist Avatar answered Oct 25 '22 00:10

Renormalist