Matching DSL Syntax

I am trying to encode my own DSL for file manipulation, just for the sake of training.

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

Here are 3 alternatives for adding a row to the .yml database:

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 bit lost in all of these alternatives.

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

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

EDIT: I think it would be nice if I can specify some optional parameters, for example.

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

, , , . , 4-5, : force = > true, , .

+5
5

.

( ) "database.yml", , .

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

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

( ) "" , . (, ,)

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

, , - , .

+4

DSL, , . . , "", , , , "".

:

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

append "windows", :to => "database.yml"
append "windows", :to => "database.yml", :force => true

append "windows", :to_file => "database.yml" # if you really want "to_file"
append "windows", :to_file => "database.yml", :force => true # if you really want "to_file"

4-5, , : force = > true, .

, . , . : force = > true, .

+3

, , DSL. , , , , -

on 'database.yaml' do
  append 'windows'

  line 16 do
    indent 2.spaces
  end

  lines 3,6,7 do
    delete
  end
end

, (, , , ..), . , , , , " " - , .

+3

DSL , DSL-, , , .

DSL Ruby

+2

:

with file('database.yaml') do |f|
  f.append file('additions.yaml')
  f.append 'somekey: true'
  f.move_to dir('/some/where')
  cpy = f.copy_to dir('some/where/else')
  f.delete
end
+1

All Articles