Skip to content

Commit

Permalink
New methods for MiGA::Taxonomy
Browse files Browse the repository at this point in the history
- `#reset`
- `#delete_alternative`
- New option `replace` in `#add_alternative`
  • Loading branch information
lmrodriguezr committed Aug 18, 2019
1 parent c218a0f commit b365517
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
30 changes: 26 additions & 4 deletions lib/miga/taxonomy.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ class MiGA::Taxonomy < MiGA::MiGA
# String, Array, or Hash entries as defined above (except +ranks+ are not # String, Array, or Hash entries as defined above (except +ranks+ are not
# allowed). # allowed).
def initialize(str, ranks = nil, alt = []) def initialize(str, ranks = nil, alt = [])
reset(str, ranks)
@alt = (alt || []).map { |i| Taxonomy.new(i) }
end

##
# Reset ranks (including namespace) while leaving alternatives untouched.
# See #initialize for +str+ and +ranks+.
def reset(str, ranks = nil)
@ranks = {} @ranks = {}
if ranks.nil? if ranks.nil?
initialize_by_str(str) initialize_by_str(str)
else else
initialize_by_ranks(str, ranks) initialize_by_ranks(str, ranks)
end end
@alt = (alt || []).map { |i| Taxonomy.new(i) } initialize_by_str(str)
end end


## ##
Expand Down Expand Up @@ -76,10 +84,24 @@ def alternative(which = nil)
end end


## ##
# Add an alternative taxonomy. # Add an alternative taxonomy. If the namespace matches an existing namespace,
def add_alternative(tax) # the alternative (or master) is replaced instead if +replace+ is true.
def add_alternative(tax, replace = true)
raise 'Unsupported taxonomy class.' unless tax.is_a? MiGA::Taxonomy raise 'Unsupported taxonomy class.' unless tax.is_a? MiGA::Taxonomy
@alt << tax alt_ns = alternative(tax.namespace)
if !replace || tax.namespace.nil? || alt_ns.nil?
@alt << tax
else
alt_ns.reset(tax.to_s)
end
end

##
# Removes (and returns) all alternative taxonomies.
def delete_alternative
alt = @alt.dup
@alt = []
alt
end end


## ##
Expand Down
31 changes: 29 additions & 2 deletions test/taxonomy_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
require "test_helper" require 'test_helper'
require "miga/taxonomy" require 'miga/taxonomy'


class TaxonomyTest < Test::Unit::TestCase class TaxonomyTest < Test::Unit::TestCase


Expand Down Expand Up @@ -83,6 +83,33 @@ def test_alternative
# Add # Add
tx.add_alternative(tx.alternative(3)) tx.add_alternative(tx.alternative(3))
assert_equal(4, tx.alternative.size) assert_equal(4, tx.alternative.size)
tx.add_alternative(tx.alternative(2))
assert_equal(4, tx.alternative.size)
# Delete
alt = tx.delete_alternative
assert_equal(4, alt.size)
assert(tx.alternative.empty?)
end

def test_reset
tx = MiGA::Taxonomy.new('ns:Letters d:Latin s:A', nil,
['ns:Words d:English s:A', 'ns:Music d:Tone s:A'])
# Reset
assert_equal(2, tx.alternative.size)
assert_equal('Letters', tx.namespace)
tx.reset('g:A')
assert_equal(2, tx.alternative.size)
assert_nil(tx.namespace)
tx.reset('ns:Letters d:Latin s:A')
assert_equal('Letters', tx.namespace)
# Change of alternative
assert_equal('ns:Words d:English s:A', tx.alternative('Words').to_s)
tx.add_alternative(MiGA::Taxonomy.new('ns:Words d:Spanish s:A'))
assert_equal('ns:Words d:Spanish s:A', tx.alternative('Words').to_s)
# Change of master
assert_equal('ns:Letters d:Latin s:A', tx.to_s)
tx.add_alternative(MiGA::Taxonomy.new('ns:Letters d:Unicode s:A'))
assert_equal('ns:Letters d:Unicode s:A', tx.to_s)
end end


end end

0 comments on commit b365517

Please sign in to comment.