Monday, February 16, 2009

Code Generation using Rails, Rake and ERB

Two great things about Ruby are it's brevity and it's dynamic nature. A great way to introduce Ruby to a project written in another language is some simple code generation.

Say we need some XML files that represent data currently stored in a database. To make it simple, our desired file will use the table name pluralized as the parent element, and contain nested singular elements, one for each record in the database. Within the record elements, each field name is an element, and the value of that field is the text value of that element.

3. Create a file named something like generator.rb in RAILS_ROOT/lib/tasks4. Require ERB at the top of this file.

require 'erb'

5. Add a method to send the contents of a table to an ERB template named table.type.erb . Notice the second to last line of the method below. It's where we pass the binding method to ERB. this will make all our variables and methods available to the ERB template we create later, including the dataset variable and the table variable.

8. Our method expects a table name at the command line , so we'll call it withrake create_xml TABLE=bond

The result is a bonds.xml file with our desired data!

9. Notice how our template name contains our desired file type, xml, then our method takes this as a parameter. How easy will it be now to create a table.java.erb template, and call it? How about a template called table.php.erb ?

10. How about running this generator from jRuby to access an Oracle database?

142 comments:

Hi , Yes Builder is great just for creating XML, but erb, or (or any template system) gives you the flexibility to create templates for multiple file types. On this project we were also generating .java files. I need to do a follow up post one of these days.

Cloud is one of the tremendous technology that any company in this world would rely on(Salesforce Certification). Using this technology many tough tasks can be accomplished easily in no time. Your content are also explaining the same(Salesforce crm training in chennai). Thanks for sharing this in here. You are running a great blog, keep up this good work(hadoop training).

I was looking about the Oracle Training in Chennai for something like this ,Thank you for posting the great content..I found it quiet interesting, hopefully you will keep posting such blogs… Oracle Training in chennai

Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.I get a lot of great information from this blog. Thank you for your sharing this informative blog. Pega Training in Chennai

Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing.. QTP Training in Chennai

This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topicGreens Technologies In Chennai

Hey, nice site you have here!We provide world-class Oracle certification and placement training course as i wondered Keep up the excellent work experience!Please visit Greens Technologies located at Chennai Adyar Oracle Training in chennai

Awesome blog if our training additional way as an SQL and PL/SQL trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blog Green Technologies In Chennai

Nice site....Please refer this site also Our vision succes!Training are focused on perfect improvement of technical skills for Freshers and working professional. Our Training classes are sure to help the trainee with COMPLETE PRACTICAL TRAINING and Realtime methodologies. Green Technologies In Chennai

let's Jump Start Your Career & Get Ahead. Choose sas training method that works for you. This course is designed for professionals looking to move to a role as a business analyst, and students looking to pursue business analytics as a career. SAS Training in Chennai

Our HP Quick Test Professional course includes basic to advanced level and our QTP course is designed to get the placement in good MNC companies in chennai as quickly as once you complete the QTP certification training course.

A Best Pega Training course that is exclusively designed with Basics through Advanced Pega Concepts.With our Pega Training in Chennai you’ll learn concepts in expert level with practical manner.We help the trainees with guidance for Pega System Architect Certification and also provide guidance to get placed in Pega jobs in the industry.

There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this

Oracle Training in Chennai is one of the best oracle training institute in Chennai which offers complete Oracle training in Chennai by well experienced Oracle Consultants having more than 12+ years of IT experience.

1hai you have to learned to lot of information about c# .net Gain the knowledge and hands-on experience you need to successfully design, build and deploy applications with c#.net. C-Net-training-in-chennai

Amazing blog if our training additional way as an silverlight training trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blogsilverlight-training.htmlgreenstechnologies.in:

Amazing blog if our training additional way as an silverlight training trained as individual, you will be able to understand other applications more quickly and continue to build your skill set which will assist you in getting hi-tech industry jobs as possible in future courese of action..visit this blogsilverlight-training.htmlgreenstechnologies.in:

awesome Job oriented sharepoint training in Chennai is offered by our institue is mainly focused on real time and industry oriented. We provide training from beginner’s level to advanced level techniques thought by our experts.if you have more details visit this blog. SharePoint-training-in-chennai.html

In database computing, Oracle Real Application Clusters (RAC) — an option for the Oracle Database software produced by Oracle Corporation and introduced in 2001 with Oracle9i — provides software for clustering and high availability in Oracle database environments. Oracle Corporation includes RAC with the Standard Edition, provided the nodes are clustered using Oracle Clusterware.Oracle RAC allows multiple computers to run Oracle RDBMS software simultaneously while accessing a single database, thus providing clustering.

In a non-RAC Oracle database, a single instance accesses a single database. The database consists of a collection of data files, control files, and redo logs located on disk. The instance comprises the collection of Oracle-related memory and operating system processes that run on a computer system.

Best SQL Query Tuning Training Center In ChennaiIt’s too informative blog and I am getting conglomerations of info’s about Oracle interview questions and answer .Thanks for sharing, I would like to see your updates regularly so keep blogging.

thanku for giving this information..SEO training in hyderabad by experts in digital markeing And by prosessional experts in seo.All the training by placement and also guide by the professionals.SEO training in hyderabad

Here we have gathered couple of HAPPY DIWALI IMAGES 2016 Wallpapers SMS in English Hindi Marathi Bhojpuri Punjabi.So that you can duplicate these Happy Diwali Messages and forward to your Loved Ones.Best Happy New year status