Creating a DSL in ruby

S

snacktime

I'm getting back to a project I've wanted to do for a while now, which
is to create a DSL for recurring payments. Something like the
following:

Charge $10 on date X then $20 every month for 12 months starting on date Y.

or

Charge $10 prorated to end of this month then charge $10 every month
on the first day starting next month for 5 years.

I'm thinking I'm better off taking the time to learn a real parser and
handle it that way. I have a feeling it would be easier to debug a
dsl created with a parser then one using regular expressions.

Any suggestions?

Chris
 
Y

Yossef Mendelssohn

If I may, you could look into a fluent interface (http://
www.martinfowler.com/bliki/FluentInterface.html) instead of a DSL. I
make this distinction because "DSL" seems to have become a term
meaning "something with lots of class methods and blocks" because
that's one simple Ruby implementation.

Michael Hollins suggested something like

charge 10.dollars do
on "10 July 2007"
repeat 12 do
charge 20.dollars
every :month
starting "30 July 2007"
end
end


but how about something like


charge(10.dollars).on(date_x).then.charge(20.dollars).every:)month).times(12).starting(date2)

instead?


If that's not enough to turn you on, look at how well it works with
mocking/stubbing in places like RSpec and Mocha (http://
mocha.rubyforge.org/examples/mocha.html).
 
D

David Chelimsky

If I may, you could look into a fluent interface (http://
www.martinfowler.com/bliki/FluentInterface.html) instead of a DSL. I
make this distinction because "DSL" seems to have become a term
meaning "something with lots of class methods and blocks" because
that's one simple Ruby implementation.

Michael Hollins suggested something like

charge 10.dollars do
on "10 July 2007"
repeat 12 do
charge 20.dollars
every :month
starting "30 July 2007"
end
end


but how about something like


charge(10.dollars).on(date_x).then.charge(20.dollars).every:)month).times(12).starting(date2)

You can even get more natural language using eval - imagine this expressed as:

charge 10 dollars on 07/02/2007 then charge 20 dollars every month for
12 months starting on 08/01/2007

Check out Jay Fields' writing on Business Natural Languages:

http://bnl.jayfields.com/01_introduction.html
 
Y

Yossef Mendelssohn

You can even get more natural language using eval - imagine this expressed as:

charge 10 dollars on 07/02/2007 then charge 20 dollars every month for
12 months starting on 08/01/2007

Check out Jay Fields' writing on Business Natural Languages:

http://bnl.jayfields.com/01_introduction.html

Tricky. It's like a super-fluent interface hidden behind a parser.
Personally, I have no trouble with all the dots and parens, but I can
definitely appreciate how it would be nicer for domain experts not to
necessarily have to deal with them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top