Technology Agnostic Rubyist : Open Source Lights The Way

Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

Saturday, May 30, 2009

Extending the String class

Feeling inspired after 2009 Gotham Ruby Conference, I finally wrote this.

class String

def nationalize
# retrieve a full xml list of countries and attributes
# if string matches the country name on the list return the
# nationality of the goods that originate from that country
# may not be the most efficient way, but it sure is extensible
@xml = open('http://originify.us/originify.us.xml')
country = REXML::Document.new(@xml)
country.elements.each('/countries/country') do |p|
return p.attributes['nationality'] if self.to_s == p.text.to_s
end
# returns self or superclass
self || super
end

end

It's an extension of the String class. I wanted to present the idea at the Reject Conference session but I'm glad I didn't. There were about 10 reject presentations and some were really good.

The way it works is simple. Just take any text string and use the nationalize method on it. For example, "France".nationalize => "French", "Switzerland".nationalize => "Swiss", "Australia".nationalize => "Australian".

This could be useful. Suppose you are a clothing retailer and have data for sweater in your database. The sweater has been assigned the attributes "Wool" and originating from "Australia" or "Switzerland" using a dropdown.

In describing the sweater on a web site, one might want to say that it is made of Swiss Wool.

This could be refactored.

Friday, April 3, 2009

Take away the RAD tools, TDD and BDD may be the best way to ensure good specifications before commencing a project.

Tuesday, March 31, 2009

Ruby on Rails Inflector for Nationality

I've given myself until midnight to complete the task I wrote about on November 25th.

The idea is for a Ruby Gem that provides a nationalize method for the String Class.

Any country code would be nationalized by simply typing something akin to "France".nationalize => "French" likewise "US".nationalize => "American"

As soon as I found a list country codes... I also realized I wouldn't have enough time to write the code and Gemify it. So I started hacking away at an Environment.rb file.

That got me something like this:
class String
# list of countries derived from http://www.internetworldstats.com/list2.htm#af
def nationalize
{
:Afghanistan => "Afghani",
:Africa => "African",
:Albania => "Albanian",
:Algeria => "Algerian",
:Andorra => "Andorra",
:Angola => "Angolan",
:Anguilla => "Anguilla",
:Antarctica => "Antarctic",
...
}[self.gsub(/_id$/, "").to_sym] || super
end


I decided this was getting a little cumbersome. Especially when I realized that any country with a space (char(32)) or other odd character in the name would need to be typecast as a :symbol in order for my code to work.

Options:
1) Devise out a work around, or
2) Plug away at the entire list of 230+ ISO country names and code for the exceptions

Of course one would prefer a more elegant and flexible solution. Have only two choices seemed awkward. So I did a little bit more research and found an XML list of countries.

This enabled another option:
3) Modify an XML country list to include nationality adjectives.

Now that I've modified the XML country list the data can be parsed, utlized, and published however one sees fit, in any computer program.

This is a flexible solution and completely language agnostic. I like it that way.

While I was thinking about this, I registered the domain name http://originfy.us. I will upload the XML file that I created to that domain.

Monday, March 24, 2008

Web site maintenance - global message

About once a week we upload and update all of the products in the database.

This intensive process takes about 15 minutes, during which time any pages that requires database access may time out.

There has to be a simple way to establish a "web site maintenance" message on the site while these processes run. Additionally cached pages need to be expired if they contain updated products.

I'm still working on a way to achieve this result.

Friday, February 22, 2008

Visually impaired software development

This is my work blog.

I will present ideas (very) informally, as well as shortcuts and tips that I learn and use for application development.

Since working at Lighthouse International during 2006 and 2007 I have become enamored with Ruby as a programming language. Mainly I love the fact that it's open source, though I'm still using Microsoft SQL Server for the databased operations and have been happy with this database server for a long time.

The idea for Ruby on Braille came to me when as a team, we three developers, (Mike Hill, Rathnavel Kandaswami, and myself) were toying with the idea of building something new, a program that had never been built before. As is often the case with open source software there are a lot of free resources, but it can be hard to separate signal from noise and often as developers we must simply "roll our own" code, modify existing programs, and evaluate the effectiveness and reliability of free solutions.

So while I don't plan on offering any free solutions I will tell you about my experience as a software developer.

Tweets

Contributors