From 9087b860e723602596cf7a02a4ef9645695d45ee Mon Sep 17 00:00:00 2001 From: Javier Toledo Date: Tue, 19 Jun 2012 14:19:32 +0100 Subject: [PATCH] Simplified API --- README.md | 16 ++--- lib/provincias.rb | 157 +++++++++++++++++++++++---------------------- provincias.gemspec | 4 +- 3 files changed, 87 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index b82f92e..b640790 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Provincias -Stop storing spanish provinces in a static table in your database. Use this gem with its class Provincia and reclaim a few bytes of your storage quota. +Stop storing spanish provinces in a static table in your database. Use this gem and reclaim a few bytes of your storage quota. ## Thanks @@ -22,17 +22,13 @@ Or install it yourself as: ## Usage -Install the gem as described before and use Province class to find and show province names. - -You may want to include the Provincias module in your classes to access the Provicia class directly. In other case it will be namespaced under Provincias::Provincia. - The interface is simmilar to ActiveRecord's and provides provinces numeric codes you can use to search or easily link provinces to your classes. - Provincia.find(id) => "Some Province Name" - Provincia.find_by_name('Madrid') => Provincia(:id => 35, :name => 'Palmas, Las') - Provincia.find_by_name('wombat') => nil - Provincia.all => [...] # An array of Provincia instances - Provincia.all_for_select => [...] # An array in form [name, id] to use in select helpers in Rails + Provincias.find(28) => # + Provincias.find_by_name('Palmas, Las') => # + Provincias.find_by_name('wombat') => nil + Provincias.all => [...] # An array of Provincia instances + Provincias.all_for_select => [...] # An array in form [name, id] to use in your select helpers in Rails ## Contributing diff --git a/lib/provincias.rb b/lib/provincias.rb index bbba431..d755ecf 100644 --- a/lib/provincias.rb +++ b/lib/provincias.rb @@ -2,90 +2,91 @@ require "provincias/version" module Provincias - class Provincia - - attr_reader :id, :name - def initialize(hash) - @id = hash[:id] - @name = hash[:name] + def self.find(id) + @@provincias.each do |p| + return Provincia.new(p) if id == p[:id] end + nil + end - def self.find(id) - @@provincias.each do |p| - return Provincia.new(p) if id == p[:id] - end - nil + def self.find_by_name(name) + @@provincias.each do |p| + return Provincia.new(p) if name == p[:name] end + nil + end - def self.find_by_name(name) - @@provincias.each do |p| - return Provincia.new(p) if name == p[:name] - end - nil - end + def self.all + @@provincias.map { |p| Provincia.new(p) } + end - def self.all - @@provincias.map { |p| Provincia.new(p) } - end + def self.all_for_select + @@provincias.map { |p| [p[:name], p[:id]] } + end - def self.all_for_select - @@provincias.map { |p| [p[:name], p[:id]] } - end + class Provincia + + attr_reader :id, :name - private - @@provincias = [ - {:id => 1, :name => 'Álava'}, - {:id => 2, :name => 'Albacete'}, - {:id => 3, :name => 'Alicante'}, - {:id => 4, :name => 'Almería'}, - {:id => 5, :name => 'Avila'}, - {:id => 6, :name => 'Badajoz'}, - {:id => 7, :name => 'Illes Baleares'}, - {:id => 8, :name => 'Barcelona'}, - {:id => 9, :name => 'Burgos'}, - {:id => 10, :name => 'Cáceres'}, - {:id => 11, :name => 'Cádiz'}, - {:id => 12, :name => 'Cástellón'}, - {:id => 13, :name => 'Ciudad Real'}, - {:id => 14, :name => 'Córdoba'}, - {:id => 15, :name => 'Coruña, A'}, - {:id => 16, :name => 'Cuenca'}, - {:id => 17, :name => 'Girona'}, - {:id => 18, :name => 'Granada'}, - {:id => 19, :name => 'Guadalajara'}, - {:id => 20, :name => 'Guipuzcoa'}, - {:id => 21, :name => 'Huelva'}, - {:id => 22, :name => 'Huesca'}, - {:id => 23, :name => 'Jaén'}, - {:id => 24, :name => 'León'}, - {:id => 25, :name => 'Lleida'}, - {:id => 26, :name => 'Rioja, La'}, - {:id => 27, :name => 'Lugo'}, - {:id => 28, :name => 'Madrid'}, - {:id => 29, :name => 'Málaga'}, - {:id => 30, :name => 'Murcia'}, - {:id => 31, :name => 'Navarra'}, - {:id => 32, :name => 'Ourense'}, - {:id => 33, :name => 'Asturias'}, - {:id => 34, :name => 'Palencia'}, - {:id => 35, :name => 'Palmas, Las'}, - {:id => 36, :name => 'Pontevedra'}, - {:id => 37, :name => 'Salamanca'}, - {:id => 38, :name => 'Santa Cruz de Tenerife'}, - {:id => 39, :name => 'Cantabria'}, - {:id => 40, :name => 'Segovia'}, - {:id => 41, :name => 'Sevilla'}, - {:id => 42, :name => 'Soria'}, - {:id => 43, :name => 'Tarragona'}, - {:id => 44, :name => 'Teruel'}, - {:id => 45, :name => 'Toledo'}, - {:id => 46, :name => 'Valencia'}, - {:id => 47, :name => 'Valladolid'}, - {:id => 48, :name => 'Vizcaya'}, - {:id => 49, :name => 'Zamora'}, - {:id => 50, :name => 'Zaragoza'}, - {:id => 51, :name => 'Ceuta'}, - {:id => 52, :name => 'Melilla'}] + def initialize(hash) + @id = hash[:id] + @name = hash[:name] + end end -end + +private + @@provincias = [ + {:id => 1, :name => 'Álava'}, + {:id => 2, :name => 'Albacete'}, + {:id => 3, :name => 'Alicante'}, + {:id => 4, :name => 'Almería'}, + {:id => 5, :name => 'Avila'}, + {:id => 6, :name => 'Badajoz'}, + {:id => 7, :name => 'Illes Baleares'}, + {:id => 8, :name => 'Barcelona'}, + {:id => 9, :name => 'Burgos'}, + {:id => 10, :name => 'Cáceres'}, + {:id => 11, :name => 'Cádiz'}, + {:id => 12, :name => 'Cástellón'}, + {:id => 13, :name => 'Ciudad Real'}, + {:id => 14, :name => 'Córdoba'}, + {:id => 15, :name => 'Coruña, A'}, + {:id => 16, :name => 'Cuenca'}, + {:id => 17, :name => 'Girona'}, + {:id => 18, :name => 'Granada'}, + {:id => 19, :name => 'Guadalajara'}, + {:id => 20, :name => 'Guipuzcoa'}, + {:id => 21, :name => 'Huelva'}, + {:id => 22, :name => 'Huesca'}, + {:id => 23, :name => 'Jaén'}, + {:id => 24, :name => 'León'}, + {:id => 25, :name => 'Lleida'}, + {:id => 26, :name => 'Rioja, La'}, + {:id => 27, :name => 'Lugo'}, + {:id => 28, :name => 'Madrid'}, + {:id => 29, :name => 'Málaga'}, + {:id => 30, :name => 'Murcia'}, + {:id => 31, :name => 'Navarra'}, + {:id => 32, :name => 'Ourense'}, + {:id => 33, :name => 'Asturias'}, + {:id => 34, :name => 'Palencia'}, + {:id => 35, :name => 'Palmas, Las'}, + {:id => 36, :name => 'Pontevedra'}, + {:id => 37, :name => 'Salamanca'}, + {:id => 38, :name => 'Santa Cruz de Tenerife'}, + {:id => 39, :name => 'Cantabria'}, + {:id => 40, :name => 'Segovia'}, + {:id => 41, :name => 'Sevilla'}, + {:id => 42, :name => 'Soria'}, + {:id => 43, :name => 'Tarragona'}, + {:id => 44, :name => 'Teruel'}, + {:id => 45, :name => 'Toledo'}, + {:id => 46, :name => 'Valencia'}, + {:id => 47, :name => 'Valladolid'}, + {:id => 48, :name => 'Vizcaya'}, + {:id => 49, :name => 'Zamora'}, + {:id => 50, :name => 'Zaragoza'}, + {:id => 51, :name => 'Ceuta'}, + {:id => 52, :name => 'Melilla'}] +end \ No newline at end of file diff --git a/provincias.gemspec b/provincias.gemspec index a0be708..a9da917 100644 --- a/provincias.gemspec +++ b/provincias.gemspec @@ -4,8 +4,8 @@ require File.expand_path('../lib/provincias/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["Javier Toledo"] gem.email = ["javier@theagilemonkeys.com"] - gem.description = %q{Provides a Provincia class to search for and use spanish provinces in your apps.} - gem.summary = %q{Stop storing spanish provinces in a static table in your database. Use this gem with its class Provincia, reclaim a few bytes of your storage quota and sanitize your DB structure from unuseful static data.} + gem.description = %q{Provides a way to easily manage spanish provinces in your apps without creating additional tables in your database.} + gem.summary = %q{Stop storing spanish provinces in a static table in your database. Use this gem and reclaim a few bytes of your storage quota.} gem.homepage = "https://github.com/agilemonkeys/provincias" gem.files = `git ls-files`.split($\)