# OT: help with business natural language

**URL:** <https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916>\
**Category:** Ruby\
**Created:** [July 20, 2007, 6:45am UTC](https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916 "2007-07-20T06:45:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 20, 2007, 6:45am UTC](https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916/1 "2007-07-20T06:45:45Z")

</div>

So today I finally had the time to sit down with racc and work on a  
natural language for defining recurring billing cycles. The racc part  
was actually pretty easy once I got a handle on how yacc works, but  
it’s the wording and syntax I’m struggling with a bit.

I’m trying to include most of the different billing scenario’s that  
I’ve seen used out there. The basic flow is that you have different  
stages of a billing cycle. The first stage is what to do when a  
customer first signs up. You can charge them immediately, charge them  
in a number of days, or give them a free trial. Subsequent stages  
define a billing cycle over a period of time. You can create several  
stages where you can for example bill someone every month for 3  
months, then bill them every 6 months for 3 years, etc…

What I’m having problems with is finding a way to use a similar syntax  
to describe two different scenarios.

First we have the first stage which is something like this:

charge X amount now|charge X amount in 5 days|charge nothing now

Then we have subsequent stages:

THEN charge X amount every X day[s] for X times|until cancelled

So far so good. But here is another scenario that uses a different  
syntax and could span the first two stages depending on how you look  
at it.

charge $40 on the 1st of every month 12 times|until cancelled

In this scenario the first payment is usually prorated to whatever day  
the billing cycle starts on. This example actually assumes a prorated  
amount. I could also write it something like this, which would fit in  
a bit better with the rest of the language syntax:

charge prorated amount now then charge $40 on the 1st of every month 12  
times

I don’t really like this last one, but it fits in better as far as how  
stages are defined, where the first stage is always present and  
defines how the first payment is handled.

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 20, 2007, 7:21am UTC](https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916/2 "2007-07-20T07:21:48Z")

</div>

On Jul 19, 2007, at 10:44 PM, snacktime wrote:

> define a billing cycle over a period of time. You can create several  
> stages where you can for example bill someone every month for 3  
> months, then bill them every 6 months for 3 years, etc…
> 
> What I’m having problems with is finding a way to use a similar syntax  
> to describe two different scenarios.
> 
> First we have the first stage which is something like this:
> 
> charge X amount now|charge X amount in 5 days|charge nothing now

seen this

[http://runt.rubyforge.org/](http://runt.rubyforge.org/)

??

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 20, 2007, 8:02am UTC](https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916/3 "2007-07-20T08:02:50Z")

</div>

On 7/20/07, snacktime [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> define a billing cycle over a period of time. You can create several  
> stages where you can for example bill someone every month for 3  
> months, then bill them every 6 months for 3 years, etc…
> 
> What I’m having problems with is finding a way to use a similar syntax  
> to describe two different scenarios.

8\<

Hi Chris,

Sorry not to be helpful at all with the “racc” issue.

Your current challenge has a business rule / process taste. You could  
have different rulesets or processes for each business case.  
There are some of those tools written in Ruby.

Or you could take inspiration in Rake which defines compilation rules,  
and taking a similar approach, define an engine for interpreting  
billing rules defined in Ruby (directly).  
Let the Ruby interpreter do the grunt work and focus on providing an  
abstraction level, in Ruby, for your billing issues.

—8\<—  
Snacktime::BillingTask.new do |t|  
t.charge = {  
:now =\> “$40”,  
:after\_one\_month =\> :the\_rest  
}  
end  
—\>8—

Best regards,

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 20, 2007, 9:30am UTC](https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916/4 "2007-07-20T09:30:08Z")

</div>

Thanks guys. I had seen runt but not the recurring library on  
rubyforge, which looks closer to what I’m looking for to do the actual  
date calculations. Right now the main challenge is just making the  
language easy to use for a laymen.

The best way I’ve found so far to handle billing is at every interval  
you calculate the next interval and just store that in the database.  
This is for credit card billing so the exact dates can and do change a  
lot even though the interval stays the same. Cards get declined and  
then reactivated again a few days later, people cancel, change billing  
cycles, etc…

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 20, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/ot-help-with-business-natural-language/109916/5 "2007-07-20T20:03:53Z")

</div>

On 7/20/07, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 7/20/07, snacktime [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So today I finally had the time to sit down with racc and work on a  
> > natural language for defining recurring billing cycles.

> and taking a similar approach, define an engine for interpreting  
> billing rules defined in Ruby (directly).  
> Let the Ruby interpreter do the grunt work and focus on providing an  
> abstraction level, in Ruby, for your billing issues.

> **[Agile DSL Development in Ruby](https://www.infoq.com/presentations/agile-dsl-development-in-ruby/)**
>
> The presentation shows how to involve on-site customers in the evolution of their DSL syntax and how it leads to higher-quality, more correct software. The process of moving from a 
> draft DSL syntax to implementation via TDD will be explained in...

has some good stuff on doing just this.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
