Skip to content

Commit

Permalink
simplified modules/classes setup
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesbowkett committed Jul 12, 2011
1 parent c006793 commit e552bc7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 42 deletions.
7 changes: 2 additions & 5 deletions lib/buscando_el_viento.rb
Expand Up @@ -4,10 +4,7 @@
require 'lib/buscando_el_viento/vectors'
require 'lib/buscando_el_viento/triggers'

module BuscandoElViento
class Migration < ActiveRecord::Migration
extend Vectors
extend Triggers
end
class BuscandoMigration < ActiveRecord::Migration
extend BuscandoElViento
end

24 changes: 8 additions & 16 deletions lib/buscando_el_viento/triggers.rb
@@ -1,16 +1,9 @@
module BuscandoElViento
module Triggers
include BuscandoElViento::Vectors
# FIXME: I'm including this module because it enables trigger_name to use
# vector_name. however, it's lame to include the module twice, and
# the main file also includes it. so I think I'll keep the distinct
# files but do away with the whole modules thingamabob.

def trigger_name(table, column)
"#{table}_#{vector_name(column)}_update"
end
def add_trigger(table, column)
execute <<TRIGGER
def trigger_name(table, column)
"#{table}_#{vector_name(column)}_update"
end
def add_trigger(table, column)
execute <<TRIGGER
CREATE TRIGGER #{trigger_name(table, column)}
BEFORE INSERT OR UPDATE
ON #{table}
Expand All @@ -19,10 +12,9 @@ def add_trigger(table, column)
'pg_catalog.english',
#{column});
TRIGGER
end
def remove_trigger(table, column)
execute "DROP TRIGGER IF EXISTS #{trigger_name(table, column)} on #{table};"
end
end
def remove_trigger(table, column)
execute "DROP TRIGGER IF EXISTS #{trigger_name(table, column)} on #{table};"
end
end

26 changes: 12 additions & 14 deletions lib/buscando_el_viento/vectors.rb
@@ -1,19 +1,17 @@
module BuscandoElViento
module Vectors
def vector_name(name)
case name
when String, Symbol
"#{name}_search_vector".to_sym
when Array
"#{name.join("_")}_search_vector".to_sym
end
end
def add_search_vector(table, column)
add_column(table, vector_name(column), "tsvector")
end
def remove_search_vector(table, column)
remove_column(table, vector_name(column))
def vector_name(name)
case name
when String, Symbol
"#{name}_search_vector".to_sym
when Array
"#{name.join("_")}_search_vector".to_sym
end
end
def add_search_vector(table, column)
add_column(table, vector_name(column), "tsvector")
end
def remove_search_vector(table, column)
remove_column(table, vector_name(column))
end
end

6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -8,7 +8,7 @@ http://peepcode.com/products/postgresql
usage
-----

Inherit from BuscandoElViento::Migration instead of ActiveRecord::Migration. The BuscandoElViento version includes additional class methods. For example,
Inherit from BuscandoMigration instead of ActiveRecord::Migration. The BuscandoElViento version includes additional class methods. For example,

add_search_vector :users, :username, :fuzzy => true

Expand All @@ -34,15 +34,15 @@ http://peepcode.com/products/postgresql
wtf? is that name spanish?
--------------------------

yes. buscando el viento gets its name from a line in a poem by Pablo Neruda.
yes. "buscando el viento" means "seeking the wind" and gets its name from a line in a poem by Pablo Neruda.

mi voz buscaba el viento para tocar su oido

which means

my voice sought the wind to touch her hearing

there's a nuance here: where in English, you say a person "plays" a musical instrument, in order to bring forth sound, in Spanish, you say a person "touches" a musical instrument.
(there's a nuance here: where in English, you say a person "plays" a musical instrument, in order to bring forth sound, in Spanish, you say a person "touches" a musical instrument.)

authors
-------
Expand Down
4 changes: 2 additions & 2 deletions spec/buscando_el_viento_spec.rb
Expand Up @@ -2,15 +2,15 @@

describe BuscandoElViento do
before(:each) do
class SearchMigration < BuscandoElViento::Migration
class SearchMigration < BuscandoMigration
end
end

it "exists" do
BuscandoElViento.class.should == Module
end
it "inherits" do
BuscandoElViento::Migration.new.should be_a(ActiveRecord::Migration)
BuscandoMigration.new.should be_a(ActiveRecord::Migration)
end
end

2 changes: 1 addition & 1 deletion spec/triggers_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe BuscandoElViento do
before(:each) do
class SearchMigration < BuscandoElViento::Migration
class SearchMigration < BuscandoMigration
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/vectors_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe BuscandoElViento do
before(:each) do
class SearchMigration < BuscandoElViento::Migration
class SearchMigration < BuscandoMigration
end
end

Expand Down

0 comments on commit e552bc7

Please sign in to comment.