Skip to content
Permalink
Browse files

New methods for MiGA::Taxonomy

- `#reset`
- `#delete_alternative`
- New option `replace` in `#add_alternative`
  • Loading branch information...
lmrodriguezr committed Aug 18, 2019
1 parent c218a0f commit b36551719b7ebf9657560fb119c430d12c4402e5
Showing with 55 additions and 6 deletions.
  1. +26 −4 lib/miga/taxonomy.rb
  2. +29 −2 test/taxonomy_test.rb
@@ -21,13 +21,21 @@ class MiGA::Taxonomy < MiGA::MiGA
# String, Array, or Hash entries as defined above (except +ranks+ are not
# allowed).
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 = {}
if ranks.nil?
initialize_by_str(str)
else
initialize_by_ranks(str, ranks)
end
@alt = (alt || []).map { |i| Taxonomy.new(i) }
initialize_by_str(str)
end

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

##
# Add an alternative taxonomy.
def add_alternative(tax)
# Add an alternative taxonomy. If the namespace matches an existing namespace,
# 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
@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

##
@@ -1,5 +1,5 @@
require "test_helper"
require "miga/taxonomy"
require 'test_helper'
require 'miga/taxonomy'

class TaxonomyTest < Test::Unit::TestCase

@@ -83,6 +83,33 @@ def test_alternative
# Add
tx.add_alternative(tx.alternative(3))
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

0 comments on commit b365517

Please sign in to comment.
You can’t perform that action at this time.