Skip to content

Commit

Permalink
cached names specs
Browse files Browse the repository at this point in the history
  • Loading branch information
proceps committed Oct 18, 2013
1 parent fe530fd commit 8ac6b62
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 44 deletions.
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/icn/above_family.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Icn::AboveFamilyGroup < NomenclaturalRank::Icn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') if not(taxon_name.name = taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') unless taxon_name.name = taxon_name.name.capitalize
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.valid_parents
Expand Down
4 changes: 3 additions & 1 deletion app/models/nomenclatural_rank/icn/family_group.rb
@@ -1,7 +1,9 @@
class NomenclaturalRank::Icn::FamilyGroup < NomenclaturalRank::Icn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') if not(taxon_name.name = taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') unless taxon_name.name = taxon_name.name.capitalize
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1

end

def self.valid_parents
Expand Down
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/icn/genus_group.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Icn::GenusGroup < NomenclaturalRank::Icn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') if not(taxon_name.name = taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') unless taxon_name.name = taxon_name.name.capitalize
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def parent_rank
Expand Down
7 changes: 4 additions & 3 deletions app/models/nomenclatural_rank/icn/infraspecific_group.rb
@@ -1,10 +1,11 @@
class NomenclaturalRank::Icn::InfraspecificGroup < NomenclaturalRank::Icn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be lower case') if not(taxon_name.name == taxon_name.name.downcase)
end
taxon_name.errors.add(:name, 'name must be lower case') unless taxon_name.name == taxon_name.name.downcase
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.valid_parents
NomenclaturalRank::Icn::InfraspecificGroup.descendants + NomenclaturalRank::Icn::Species
NomenclaturalRank::Icn::InfraspecificGroup.descendants + [NomenclaturalRank::Icn::Species]
end
end
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/icn/species.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Icn::Species < NomenclaturalRank::Icn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be lower case') if not(taxon_name.name == taxon_name.name.downcase)
taxon_name.errors.add(:name, 'name must be lower case') unless taxon_name.name == taxon_name.name.downcase
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.parent_rank
Expand Down
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/iczn/above_family_group.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Iczn::AboveFamilyGroup < NomenclaturalRank::Iczn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') unless(taxon_name.name == taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') unless taxon_name.name == taxon_name.name.capitalize
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.valid_parents
Expand Down
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/iczn/family_group.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Iczn::FamilyGroup < NomenclaturalRank::Iczn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') unless(taxon_name.name == taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') unless taxon_name.name == taxon_name.name.capitalize
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.valid_parents
Expand Down
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/iczn/genus_group.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Iczn::GenusGroup < NomenclaturalRank::Iczn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be capitalized') unless(taxon_name.name == taxon_name.name.capitalize)
taxon_name.errors.add(:name, 'name must be capitalized') unless taxon_name.name == taxon_name.name.capitalize
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.valid_parents
Expand Down
3 changes: 2 additions & 1 deletion app/models/nomenclatural_rank/iczn/species_group.rb
@@ -1,7 +1,8 @@
class NomenclaturalRank::Iczn::SpeciesGroup < NomenclaturalRank::Iczn

def self.validate_name_format(taxon_name)
taxon_name.errors.add(:name, 'name must be lower case') unless(taxon_name.name == taxon_name.name.downcase)
taxon_name.errors.add(:name, 'name must be lower case') unless taxon_name.name == taxon_name.name.downcase
taxon_name.errors.add(:name, 'name must be at least two letters') unless taxon_name.name.length > 1
end

def self.valid_parents
Expand Down
16 changes: 7 additions & 9 deletions app/models/taxon_name.rb
Expand Up @@ -72,18 +72,17 @@ def set_type_if_empty
self.type = 'Protonym' if self.type.nil?
end

# TODO: This should be based on the logic of the related Rank
def set_cached_name
genus_species_ranks = NomenclaturalRank::Iczn::GenusGroup.descendants + NomenclaturalRank::Iczn::SpeciesGroup.descendants + NomenclaturalRank::Icn::GenusGroup.descendants + [NomenclaturalRank::Icn::Species] + NomenclaturalRank::Icn::InfraspecificGroup.descendants
if !genus_species_ranks.include?(self.rank_class)
name = nil
cached_name = nil
else
genus = ''
subgenus = ''
species = ''
cached_name = nil
(self.ancestors + [self]).each do |i|

if genus_species_ranks.include?(Object.const_get(self.rank_class.to_s))
if genus_species_ranks.include?(Object.const_get(i.rank_class.to_s))
case i.rank_class.rank_name
when "genus" then genus = i.name + ' '
when "subgenus" then subgenus += i.name + ' '
Expand All @@ -99,12 +98,12 @@ def set_cached_name
when "subform" then species += 'subf. ' + i.name + ' '
else
end
subgenus = '(' + subgenus.strip! + ') ' unless subgenus.empty?
end
end
name = (genus + subgenus + species).strip!
subgenus = '(' + subgenus.strip! + ') ' unless subgenus.empty?
cached_name = (genus + subgenus + species).strip!
end
self.cached_name = name
self.cached_name = cached_name
end

def set_cached_author_year
Expand Down Expand Up @@ -137,11 +136,10 @@ def set_cached_author_year

def set_cached_higher_classification
above_family_ranks = NomenclaturalRank::Iczn::AboveFamilyGroup.descendants + NomenclaturalRank::Iczn::FamilyGroup.descendants + NomenclaturalRank::Icn::AboveFamilyGroup.descendants + NomenclaturalRank::Icn::FamilyGroup.descendants
hc = self.ancestors.select{|i| [i.rank_class] == [i.rank_class] & above_family_ranks}.collect{|i| i.name}.join(':')
hc = self.ancestors.select{|i| above_family_ranks.include?(Object.const_get(i.rank_class.to_s))}.collect{|i| i.name}.join(':')
self.cached_higher_classification = hc
end


def validate_parent_rank_is_higher
return true if self.parent.nil? || self.parent.rank_class == NomenclaturalRank
if RANKS.index(self.rank_class) < RANKS.index(self.parent.rank_class)
Expand Down
15 changes: 9 additions & 6 deletions spec/factories/taxon_name.rb
Expand Up @@ -147,11 +147,14 @@
end

factory :iczn_species, class: Protonym do
name 'aaa'
name 'vitis'
association :parent, factory: :iczn_subgenus
cached_name 'Erythroneura (Erythroneura) aaa'
cached_author_year 'McAtee, 1900'
cached_higher_classification 'Animalia:Arthropoda:Insecta:Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
cached_name 'zzz'
#cached_name 'Erythroneura (Erythroneura) aaa'
cached_author_year 'ooooooooooo'
#cached_author_year 'McAtee, 1900'
cached_higher_classification 'aaa'
#cached_higher_classification 'Animalia:Arthropoda:Insecta:Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id 10
year_of_publication 1900
verbatim_author 'McAtee'
Expand All @@ -160,9 +163,9 @@
end

factory :iczn_subspecies, class: Protonym do
name 'bbb'
name 'ssp'
association :parent, factory: :iczn_species
cached_name 'Erythroneura (Erythroneura) aaa bbb'
cached_name 'Erythroneura (Erythroneura) vitis ssp'
cached_author_year 'McAtee, 1900'
cached_higher_classification 'Animalia:Arthropoda:Insecta:Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini'
source_id 10
Expand Down
23 changes: 5 additions & 18 deletions spec/models/taxon_name_spec.rb
Expand Up @@ -161,28 +161,15 @@
end

context 'after save' do
before do
taxon_name.save
end

specify 'cached_name should be set' do
# expect(taxon_name.cached_name.nil?).to be false
pending "requires code in NomenclaturalRank subclasses"

end
specify 'cached_names should be set' do
t = FactoryGirl.create(:iczn_species)

specify 'cached_higher_classification' do
t = FactoryGirl.create(:iczn_species, cached_higher_classification: 'foo')
t.valid?
expect(t.cached_higher_classification).to eq('foo')
expect(t.cached_higher_classification).to eq('') # it is 'aaa' in FactoryGirl
t.save
expect(t.cached_higher_classification).to eq('Animalia:Arthropoda:Insecta:Hemiptera:Cicadellidae:Typhlocybinae:Erythroneurini')
end

specify 'cached_author_year' do
end

specify 'cached_name' do
expect(t.cached_author_year).to eq('McAtee, 1900')
expect(t.cached_name).to eq('Erythroneura (Erythroneura) vitis')
end

end
Expand Down

0 comments on commit 8ac6b62

Please sign in to comment.