class MiGA::TaxIndexTaxon

Helper class for MiGA::TaxIndex.

Attributes

children[R]

Children of the taxon.

datasets[R]

Datasets directly classified at the taxon (not at children).

name[R]

Name of the taxon.

rank[R]

Rank of the taxon.

Public Class Methods

new(rank, name) click to toggle source

Initalize taxon at rank with name.

# File lib/miga/tax_index.rb, line 93
def initialize(rank, name)
  @rank = rank.to_sym
  @name = (name.nil? ? nil : name.miga_name)
  @children = []
  @datasets = []
end

Public Instance Methods

add_child(rank, name) click to toggle source

Add child at rank with name.

# File lib/miga/tax_index.rb, line 106
def add_child(rank, name)
  rank = rank.to_sym
  name = name.miga_name unless name.nil?
  child = children.find { |it| it.rank == rank and it.name == name }
  if child.nil?
    child = MiGA::TaxIndexTaxon.new(rank, name)
    @children << child
  end
  child
end
add_dataset(dataset) click to toggle source

Add dataset at the current taxon (not children).

# File lib/miga/tax_index.rb, line 119
def add_dataset(dataset) @datasets << dataset; end
all_datasets() click to toggle source

Get all the datasets in the taxon (including children).

# File lib/miga/tax_index.rb, line 129
def all_datasets
  children.map(&:datasets).reduce(datasets, :+)
end
datasets_count() click to toggle source

Get the number of datasets in the taxon (including children).

# File lib/miga/tax_index.rb, line 123
def datasets_count
  children.map(&:datasets_count).reduce(datasets.size, :+)
end
tax_str() click to toggle source

String representation of the taxon.

# File lib/miga/tax_index.rb, line 102
def tax_str; "#{rank}:#{name.nil? ? '?' : name}"; end
to_hash() click to toggle source

Hash representation of the taxon.

# File lib/miga/tax_index.rb, line 145
def to_hash
  {
    str: tax_str,
    datasets: datasets.map(&:name),
    children: children.map(&:to_hash)
  }
end
to_json(*a) click to toggle source

JSON String of the taxon.

# File lib/miga/tax_index.rb, line 135
def to_json(*a)
  {
    str: tax_str,
    datasets: datasets.map(&:name),
    children: children
  }.to_json(a)
end
to_tab(unknown, indent = 0) click to toggle source

Tabular String of the taxon.

# File lib/miga/tax_index.rb, line 155
def to_tab(unknown, indent = 0)
  o = ''
  if unknown or not datasets.empty? or not name.nil?
    o = "#{' ' * indent}#{tax_str}: #{datasets_count}\n"
  end
  indent += 2
  datasets.each { |ds| o << "#{' ' * indent}# #{ds.name}\n" }
  children.each { |it| o << it.to_tab(unknown, indent) }
  o
end