Technology Agnostic Rubyist : Open Source Lights The Way

Friday, July 31, 2009

country_select and my country of origin inflector method

Making use of the string class add-on that I developed at the recent Goruco conference.

I've put together a simple little app and a domain where some of the code is available to anyone who can use it.

Please contact me if you are interested any part of this.

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.

Tuesday, January 13, 2009

No more email campaigns for me

Today I taught my client how to send email blasts. It was a BLAST.

Tuesday, November 25, 2008

Write a nationality gem or plugin

This is just a reminder to self to write something that ties in with Ruby inflectors and the country_select helper.

Friday, November 14, 2008

Dirty numbers

/$#@!

This is a pretty basic programming concept but I haven't touched this blog in a while here we go...

Say you're working with numbers in a database field. Those numbers were keyed in by people using a different system, and the other system somehow stored non-numeric values in a numeric field.

Since Ruby does not have strong typing, an easy way to check and see if a field is an integer is to compare its integer value with its actual value.

if (somefield.to_i == somefield)

For example:
n = 1234567
if (n.to_i==n) => true

If the value of n is not keyed properly the equality test returns false
n = "12345x67"
if (n.to_i=="12345x67") => false

Easy!

Tweets

Contributors