Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appropriate DSL syntax

Tags:

ruby

dsl

Im trying to code my own DSL for file manipulation, just for the sake of learning.

My goal is to make it understandable and easy to code.

Here are 3 alternatives for appending a string to a database.yml:

1. append("windows").to("database.yml")

2. append(string: "windows").to(file: "database.yml")

3. append_string("windows").to_file("database.yml")

4. append_string "windows", to_file: "database.yml"

5. append string: "windows", to_file: "database.yml"

Im a little bit lost in all these alternatives.

Could someone with experience in DSL give me some guidance and explain what the pros and cons are with each one?

Everyone are read the same, but I want to know which one follows best practice for DRY and good coding standard.

EDIT: I think it will be good if I could specify some optional parameters eg.

append(string: "windows").to(file: "database.yml", :force => true)

So taken this into account, I guess that I have to use the method calls. Because if I use alternatives 4-5 then when I specify :force => true, I can't know if it's for the string or the file.

like image 921
never_had_a_name Avatar asked Aug 19 '10 06:08

never_had_a_name


1 Answers

For me, any option seems fine.

If you(or user) always write to "database.yml" but appending contents are different, following may better.

on "database.yml" {
    append "windows"
    append "ubuntsu"
    append "Leopard" 

    remove_if "bsd"  do |..|
         ....#if condition satisfied, "bsd" will be removed
    end
    ..
}

If you(or user) want to append "windows" always for different database files, following may be fine. (Maybe rare case,,)

append "windows".to {
    to "database.yml"
    to "database2.yml"
    to "database3.xml", :force=>true
}

Anyway, I think better choice is to use it yourself or ask your user, then refine.

like image 68
kyab Avatar answered Sep 27 '22 23:09

kyab