Skip to content

Instantly share code, notes, and snippets.

@duskhacker
Created May 9, 2009 20:53
Show Gist options
  • Save duskhacker/109392 to your computer and use it in GitHub Desktop.
Save duskhacker/109392 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'activerecord'
File.unlink('biology_data.db') rescue nil
File.unlink('life_form_data.db') rescue nil
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => "biology_data.db"
)
ActiveRecord::Schema.define do
create_table :phyla do |t|
t.string :name
end
insert("insert into phyla(name) values('Acanthocephala')")
insert("insert into phyla(name) values('Brachiopoda')")
insert("insert into phyla(name) values('Chaetognatha')")
create_table :divisions do | t |
t.string :name
end
insert("insert into divisions(name) values('Anthocerotophyta')")
insert("insert into divisions(name) values('Bryophyta')")
insert("insert into divisions(name) values('Marchantiophyta')")
create_table :species do | t |
t.string :name
t.integer :phylum_id
t.integer :division_id
end
end
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => "life_form_data.db"
)
ActiveRecord::Schema.define do
create_table :life_forms do |t|
t.string :name
end
insert("insert into life_forms(name) values('Polymorphus spp')")
insert("insert into life_forms(name) values('Syringothyris sp')")
insert("insert into life_forms(name) values('Spadella cephaloptera')")
insert("insert into life_forms(name) values('Phaeoceros laevis')")
insert("insert into life_forms(name) values('Sphagnum moss')")
insert("insert into life_forms(name) values('Riccia fluitans')")
end
#!/usr/bin/env ruby
require 'rubygems'
require 'activerecord'
require 'spec'
class BiologyData < ActiveRecord::Base
self.abstract_class = true
end
class Species < BiologyData
set_table_name :species
belongs_to :division
belongs_to :phylum
end
class Division < BiologyData
has_many :species
end
class Phylum < BiologyData
set_table_name :phyla
has_many :species
end
class LifeForm < ActiveRecord::Base;end
describe "MultipleArDatabases" do
before(:all) do
require 'create_databases'
BiologyData.establish_connection(
:adapter => 'sqlite3',
:dbfile => 'biology_data.db'
)
LifeForm.establish_connection(
:adapter => 'sqlite3',
:dbfile => 'life_form_data.db'
)
end
it "should classify life forms into Division or Phylum" do
LifeForm.find(:all).each do | life_form |
case life_form.name # Simulate code that would actually classify the life form
when 'Polymorphus spp'
Species.create!(:name => 'Polymorphus spp', :phylum => Phylum.find_by_name('Acanthocephala'))
when 'Syringothyris sp'
Species.create!(:name => 'Syringothyris sp', :phylum => Phylum.find_by_name('Brachiopoda'))
when 'Spadella cephaloptera'
Species.create!(:name => 'Spadella cephaloptera', :phylum => Phylum.find_by_name('Chaetognatha'))
when 'Phaeoceros laevis'
Species.create!(:name => 'Phaeoceros laevis', :division => Division.find_by_name('Anthocerotophyta'))
when 'Sphagnum moss'
Species.create!(:name => 'Sphagnum moss', :division => Division.find_by_name('Bryophyta'))
when 'Riccia fluitans'
Species.create!(:name => 'Riccia fluitans', :division => Division.find_by_name('Marchantiophyta'))
end
end
puts "\n" * 3
Phylum.find(:all).each do | phylum |
phylum.species.each do | species |
puts "-" * 80
puts "Species: #{species.name}, Phylum: #{species.phylum.name}"
end
end
Division.find(:all).each do | division |
division.species.each do | species |
puts "-" * 80
puts "Species: #{species.name}, Division: #{species.division.name}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment