Class: MiGA::TaxIndexTaxon

Inherits:
MiGA
  • Object
show all
Defined in:
lib/miga/tax_index.rb

Overview

Helper class for MiGA::TaxIndex.

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MiGA

CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, initialized?, #result_files_exist?, root_path, tabulate

Constructor Details

#initialize(rank, name) ⇒ TaxIndexTaxon

Initalize taxon at rank with name.



67
68
69
70
71
72
# File 'lib/miga/tax_index.rb', line 67

def initialize(rank, name)
  @rank = rank.to_sym
  @name = (name.nil? ? nil : name.miga_name)
  @children = []
  @datasets = []
end

Instance Attribute Details

#childrenObject (readonly)

Children of the taxon.



61
62
63
# File 'lib/miga/tax_index.rb', line 61

def children
  @children
end

#datasetsObject (readonly)

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



63
64
65
# File 'lib/miga/tax_index.rb', line 63

def datasets
  @datasets
end

#nameObject (readonly)

Name of the taxon.



59
60
61
# File 'lib/miga/tax_index.rb', line 59

def name
  @name
end

#rankObject (readonly)

Rank of the taxon.



57
58
59
# File 'lib/miga/tax_index.rb', line 57

def rank
  @rank
end

Instance Method Details

#add_child(rank, name) ⇒ Object

Add child at rank with name.



80
81
82
83
84
85
86
87
88
89
# File 'lib/miga/tax_index.rb', line 80

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) ⇒ Object

Add dataset at the current taxon (not children).



93
# File 'lib/miga/tax_index.rb', line 93

def add_dataset(dataset) @datasets << dataset ; end

#datasets_countObject

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



97
98
99
# File 'lib/miga/tax_index.rb', line 97

def datasets_count
  datasets.size + children.map{ |it| it.datasets_count }.reduce(0, :+)
end

#tax_strObject

String representation of the taxon.



76
# File 'lib/miga/tax_index.rb', line 76

def tax_str ; "#{rank}:#{name.nil? ? "?" : name}" ; end

#to_hashObject

Hash representation of the taxon.



110
111
112
113
# File 'lib/miga/tax_index.rb', line 110

def to_hash
  { str:tax_str, datasets:datasets.map{|d| d.name},
    children:children.map{ |it| it.to_hash } }
end

#to_json(*a) ⇒ Object

JSON String of the taxon.



103
104
105
106
# File 'lib/miga/tax_index.rb', line 103

def to_json(*a)
  { str:tax_str, datasets:datasets.map{|d| d.name},
    children:children }.to_json(a)
end

#to_tab(unknown, indent = 0) ⇒ Object

Tabular String of the taxon.



117
118
119
120
121
122
123
124
125
# File 'lib/miga/tax_index.rb', line 117

def to_tab(unknown, indent=0)
  o = ""
  o = (" " * indent) + tax_str + ": " + datasets_count.to_s + "\n" if
    unknown or not datasets.empty? or not name.nil?
  indent += 2
  datasets.each{ |ds| o += (" " * indent) + "# " + ds.name + "\n" }
  children.each{ |it| o += it.to_tab(unknown, indent) }
  o
end