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.


87
88
89
90
91
92
# File 'lib/miga/tax_index.rb', line 87

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.


81
82
83
# File 'lib/miga/tax_index.rb', line 81

def children
  @children
end

#datasetsObject (readonly)

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


83
84
85
# File 'lib/miga/tax_index.rb', line 83

def datasets
  @datasets
end

#nameObject (readonly)

Name of the taxon.


79
80
81
# File 'lib/miga/tax_index.rb', line 79

def name
  @name
end

#rankObject (readonly)

Rank of the taxon.


77
78
79
# File 'lib/miga/tax_index.rb', line 77

def rank
  @rank
end

Instance Method Details

#add_child(rank, name) ⇒ Object

Add child at rank with name.


100
101
102
103
104
105
106
107
108
109
# File 'lib/miga/tax_index.rb', line 100

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).


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

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

#all_datasetsObject

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


123
124
125
# File 'lib/miga/tax_index.rb', line 123

def all_datasets
  datasets + children.map{ |it| it.datasets }.reduce([], :+)
end

#datasets_countObject

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


117
118
119
# File 'lib/miga/tax_index.rb', line 117

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

#tax_strObject

String representation of the taxon.


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

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

#to_hashObject

Hash representation of the taxon.


136
137
138
139
# File 'lib/miga/tax_index.rb', line 136

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.


129
130
131
132
# File 'lib/miga/tax_index.rb', line 129

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.


143
144
145
146
147
148
149
150
151
# File 'lib/miga/tax_index.rb', line 143

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