My first Ruby gem - ruby-reads-php
Iv being meaning to experiment with building my own Ruby Gems for a while now but have been too busy to actually give it ago. This weekend I took the plunge and wrote a gem called ruby-reads-php.
Iv got quite a lot of applications and websites online that are written in PHP and most of them have some kind of configuration file to define things like database settings to keep everything in one place. I occasionally want to bolt an extra temporary feature onto an app (eg for the next month send a CSV file of people registered to somebody by email).
As Ruby is now my language of choice for most things and due to the fact that I could do something like this faster in Ruby than PHP it makes sense for me to do so. The problem I have is that I need to replicate my database configurations into a Ruby file which isn’t very DRY. I could convert into a more standardised format such as YAML but this would mean changing the PHP code that I know currently works.
So this gives me an ideal oppertunity to write a Ruby Gem. I wrote some Rspec tests for what I wanted to do and then wrote the code. I used TechnicalPickles Jeweler gem to manage my Gem Spec and publish to Github. Within 15 minutes my Gem is available to download
To install it
#First add Github to your gem sources gem sources -a http://gems.github.com #Now install the Gem gem install mattfawcett-ruby-reads-php
To use it
#require rubygems and the ruby_reads_php gem
require 'rubygems'
require 'ruby_reads_php'
#load and parse your PHP configuration file
config = RubyReadsPHP.read(File.dirname(__FILE__) + "/config.php")
my_database_hostname = config.constants['DB_HOST'] Example PHP configuration file (config.php)
<?php
define('DB_HOST' ,'localhost');
define('DB_USER' ,'web');
define('DB_USER' ,'my_password');
?>
At the moment it only supports constants but could be built on to handle arrays etc. Please feel free to fork, hack, send pull request.