Invoice DSL: Now with time and currencies!

Posted by Chris Thu, 05 Oct 2006 09:56:00 GMT

Thanks for the useful comments on the DSL.

Inspired by what I’ve achieved so far, I’ve pushed ahead and added currency support and time stuff to it. You don’t have to use the currency stuff if you don’t want to… all previous operations still work.

Currency support is pretty dynamic – check out the currency_spec.rb for a full spec, but the long and short of it is that you can do this:

1.pound.is_worth 1.88.dollars

Or, if you fancy it:

1.pound | 1.88.dollars

You can even make up your own currencies if it takes your fancy….

1.sofa.is_worth 20.pounds
20.sofas.in.pounds # prints '400'

This then extends to invoices in the following way:

i = new_invoice do
  1.pound | 1.5.euros

  work_at 20.pounds / hour do
    spent 2.hours.on('Some work')
    spent 2.days.on('More work') # Note: assumes a day is 8 hours long
  end
end

print i.total.in.pounds # prints '360.00'
print i.total.in.euros # prints '540.00'

Neat, huh?

Get the code here. Let me know what you think.

Comments

  1. Christian Romney said 25 days later:

    Food for thought… def pound= :)

    1.pound = 1.88 dollars

  2. Chris said 26 days later:

    Yeah, interesting thought… I could add a method_missing clause to Numeric to do this…

Comments are disabled

This is the obligatory blog of Eden Development. We write here about Pin in the Map updates and other stuff we get up to.

Search

Advertisements

Powered