# Using YAML as a data DSL

**URL:** <https://www.ruby-forum.com/t/using-yaml-as-a-data-dsl/63385>\
**Category:** Ruby\
**Created:** [June 14, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/using-yaml-as-a-data-dsl/63385 "2006-06-14T19:30:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 14, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/using-yaml-as-a-data-dsl/63385/1 "2006-06-14T19:30:33Z")

</div>

I have an RoR application which has various GUI screens for taking input  
from users for creating some entities in my application.  
What ever information is given by the user I am putting in to a  
datastructure in ruby(I have created a set of classes in ruby for that  
purpose),

To give you an example say there is a Music store and there are various  
records of various artists available.  
we can have following classes for storing the information entered by  
user  
for records and artists,  
class Record

@name  
#array for storing objects of artist class  
@artists

end  
class Artist  
@name  
@phone  
end

Note: ofcourse we can use a db here and update the info entered by user  
directly in the database, but may be I am giving a wrong example here,  
actually the above information about entities is entered by user only  
once.

Now I want to give the end user, a way to circumvent GUI and directly  
write  
in to a file the information about the entities in a format specified by  
me(thats what a DSL is all about).  
Following is what I intend to have with respect to DSL

1. This DSL would be more of a data DSL then a business DSL which is  
meant  
for defining business rules.
2. It should be user friendly(who doesn’t want a DSL to be user  
friendly)

with these goals in mind I wrote an internal DSL(having ruby method  
calls)  
and wrote some code which populates above mentioned classes(Record and  
Artist).

I worked in other direction of using YAML as DSL; for that I created a  
YAML  
compliant file with following contents,  
###########################  
— !ruby/object:Video  
artist: Jatinder  
name: SampleVideo  
type: Pop  
###########################  
YAML provides a way to persist objects in text format which is I believe  
very user friendly, so what I thought is that a user can create a file  
with  
above contents and then use YAML::load method to load above file’s  
content  
in to ruby object.  
Well in this case I really dont have to do much, as in I dont have to  
write  
a parser.

There might be several deficiencies/probelms in the 2nd approach, few of  
them I think are,

1. YAML has a strict format, even if a “:” goes here and there then the  
whole file goes for a toss.
2. In the above file, I would definately not like to have  
“!ruby/object”…can I override that somehow?(ofcourse I can have a layer  
between YAML and file, which modifies the file to change the file  
structure/contents to make it YAML compliant).
3. is YAML stable enough? I read about syck…it seems to solve the  
problem  
of solving perf issues.

Any thought’s on above appraoch?

Regards,  
Jatinder

---

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 14, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/using-yaml-as-a-data-dsl/63385/2 "2006-06-14T21:39:06Z")

</div>

On Wednesday 14 June 2006 11:26 am, Jatinder S. wrote:

> YAML provides a way to persist objects in text format which is I believe  
> very user friendly, so what I thought is that a user can create a file with  
> above contents and then use YAML::load method to load above file’s content  
> in to ruby object.

I have an application for a customer that provides them with a system to  
store  
product information, photographs, inventory data, etc…, and then to  
search  
all of it in a freeform way.

Because different records might have different data fields which are  
important, I gave them a simple interface. Data is entered as a YAML  
hash.  
Well, mostly, anyway. To be fair, I am not actually using YAML to parse  
what  
is entered, because I don’t want to allow anything other than

customer: Corona  
description: Blinky buttons with a narrow profile and embossed face.

type key/value pairs.

However, the inspiration came from YAML and my use of YAML for config  
files.  
It’s a simple enough format that they can get it right without too much  
difficulty, and it offers flexibility.

That said, if your application is going to be used by more than a small,  
select group of people, I would be hesitant to actually use YAML in the  
way  
that you suggest. Perhaps I am overly paranoid, but I don’t want to  
leave an  
application open to Joe ScriptKiddie’s attempts to abuse YAML into doing  
painful things. If I were going to go in that direction, I’d, at the  
very  
least, build a preprocessor of some sort to help make sure the content  
loaded  
into YAML fits the expected profile. It would still make me nervous,  
though.

Kirk H.

---

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 14, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/using-yaml-as-a-data-dsl/63385/3 "2006-06-14T21:49:47Z")

</div>

On 6/14/06, Kirk H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wednesday 14 June 2006 11:26 am, Jatinder S. wrote:
> 
> > YAML provides a way to persist objects in text format which is I believe  
> > very user friendly, so what I thought is that a user can create a file with  
> > above contents and then use YAML::load method to load above file’s content  
> > in to ruby object.

I have found that when not using a database with ROR you lose a lot of  
nice automation provided by ActiveRecord. I have in fact been  
wondering what I’d have to do to make a datasource that looks like a  
database to Rails but is in fact just an array. Specifically I’d like  
to still be able to use scaffolds and other generators.

The only suggestion I have seen in this regard is to make MEMORY  
tables using MySQL, but they don’t seem to support auto-increment  
fields for some reason so I can’t use them.

Any ideas in that regard?

---

<div class="post-metadata">

**Author:** ![Jatinder\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e4f/32.png) [@Jatinder\_S](https://www.ruby-forum.com/u/Jatinder_S)\
**Post date:** [June 14, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/using-yaml-as-a-data-dsl/63385/4 "2006-06-14T21:52:58Z")

</div>

This is probably a non-legit example that will just show my stupidity,  
but:  
widget\_1\_2\_3:  
foo: \<% `frmt c:` %\>

Misspelling is intentional, to provide a thin layer of safety in case  
somebody gets “curious.”
