Class: VORuby::VOTable::V1_0::Table

Inherits:
Base show all
Defined in:
lib/voruby/votable/1.0/votable.rb

Overview

The basic structure of a votable. Essentially, tablular data + a description of the columns of that data.

Constant Summary collapse

ELEMENT_NAME =
'TABLE'

Instance Attribute Summary

Attributes inherited from XML::Object::Base

#node

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, element_name, #get_element, #xpath_for

Methods inherited from XML::Object::Base

#==, element_name, from_file, #to_s

Constructor Details

#initialize(defn = nil) ⇒ Table

Create a new table.

table = Table.new(
  :name => 'results',
  :fields => [Field.new()],
  :data => Data.new(:format => TableData.new())
)


800
801
802
# File 'lib/voruby/votable/1.0/votable.rb', line 800

def initialize(defn=nil)
  super(defn)
end

Class Method Details

.serialization_orderObject



787
788
789
790
791
792
# File 'lib/voruby/votable/1.0/votable.rb', line 787

def self.serialization_order
  [
    :description, :fields, :links, :data,
    :id, :name, :ref
  ]
end

Instance Method Details

#dataObject

Retrieve the data (Data).



839
840
841
# File 'lib/voruby/votable/1.0/votable.rb', line 839

def data
  get_element(Data)
end

#data=(d) ⇒ Object

Set the data (Data).



844
845
846
# File 'lib/voruby/votable/1.0/votable.rb', line 844

def data=(d)
  set_element(Data, d)
end

#descriptionObject

Retrieve the description (Description).



829
830
831
# File 'lib/voruby/votable/1.0/votable.rb', line 829

def description
  get_element(Description)
end

#description=(d) ⇒ Object

Set the description (Description).



834
835
836
# File 'lib/voruby/votable/1.0/votable.rb', line 834

def description=(d)
  set_element(Description, d)
end

#fieldsObject

Retrieve the fields (Field), a.k.a. column descriptions. Returns a HomogeneousNodeList.



850
851
852
# File 'lib/voruby/votable/1.0/votable.rb', line 850

def fields
  HomogeneousNodeList.new(self.node, xpath_for(Field), Field)
end

#fields=(flds) ⇒ Object

Set the fields.

table.fields = [Field.new(), ...]


856
857
858
# File 'lib/voruby/votable/1.0/votable.rb', line 856

def fields=(flds)
  self.fields.replace(flds)
end

#idObject



804
805
806
# File 'lib/voruby/votable/1.0/votable.rb', line 804

def id
  self.node['ID']
end

#id=(i) ⇒ Object



808
809
810
# File 'lib/voruby/votable/1.0/votable.rb', line 808

def id=(i)
  @node['ID'] = i.to_s
end

Retrieve the links (Link). Returns a HomogeneousNodeList.



862
863
864
# File 'lib/voruby/votable/1.0/votable.rb', line 862

def links
  HomogeneousNodeList.new(self.node, xpath_for(Link), Link)
end

#links=(lnks) ⇒ Object

Set the links (Link).

table.links = [Link.new(), ...]


868
869
870
# File 'lib/voruby/votable/1.0/votable.rb', line 868

def links=(lnks)
  self.links.replace(lnks)
end

#nameObject



812
813
814
# File 'lib/voruby/votable/1.0/votable.rb', line 812

def name
  self.node['name']
end

#name=(n) ⇒ Object



816
817
818
# File 'lib/voruby/votable/1.0/votable.rb', line 816

def name=(n)
  @node['name'] = n.to_s
end

#refObject



820
821
822
# File 'lib/voruby/votable/1.0/votable.rb', line 820

def ref
  self.node['ref']
end

#ref=(r) ⇒ Object



824
825
826
# File 'lib/voruby/votable/1.0/votable.rb', line 824

def ref=(r)
  @node['ref'] = r.to_s
end

#to_htmlObject

Convert the table into HTML. Not all aspects of the votable are translated.

table = Table.new(
  :id => 'table1',
  :description => Description.new(:text => 'A test table'),
  :fields => [
    Field.new(:name => 'RA', :id => 'col1', :ucd => 'pos.eq.ra;meta.main',
              :ref => 'J2000', :datatype => 'float', :width => 6, :precision => '2', :unit => 'deg'),
    Field.new(:name => 'Dec', :id => 'col2', :ucd => 'pos.eq.dec;meta.main',
              :ref => 'J2000', :datatype => 'float', :width => 6, :precision => '2', :unit => 'deg'),
    Field.new(:name => 'Name', :id => 'col3', :ucd => 'meta.id;meta.main',
              :datatype => 'char', :arraysize => '8*')
    ],
    :data => Data.new(
      :format => TableData.new(
        :trs => [
          Tr.new(:tds => [Td.new(:text => '123'), Td.new(:text => '456'), Td.new(:text => 'my_obj1')]),
          Tr.new(:tds => [Td.new(:text => '789'), Td.new(:text => '112'), Td.new(:text => 'my_obj2')]),
          Tr.new(:tds => [Td.new(:text => '145'), Td.new(:text => '178'), Td.new(:text => 'my_obj3')])
        ]
      )
    )
)

puts table.to_html # => 
  <table id="table1">
    <caption>A test table</caption>
    <thead>
      <tr>
        <th>
          <div class="field" id="col1">
            <div class="name">RA</div>
            <div class="ucd">pos.eq.ra;meta.main</div>
            <div class="unit">deg</div>
            <span class="datatype">float</span>
          </div>
        </th>
        <th>
          <div class="field" id="col2">
            <div class="name">Dec</div>
            <div class="ucd">pos.eq.dec;meta.main</div>
            <div class="unit">deg</div>
            <span class="datatype">float</span>
          </div>
        </th>
        <th>
          <div class="field" id="col3">
            <div class="name">Name</div>
            <div class="ucd">meta.id;meta.main</div>
            <span class="datatype">char</span>
            <span class="arraysize">8*</span>
          </div>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>123</td>
        <td>456</td>
        <td>my_obj1</td>
      </tr>
      <tr>
        <td>789</td>
        <td>112</td>
        <td>my_obj2</td>
      </tr>
      <tr>
        <td>145</td>
        <td>178</td>
        <td>my_obj3</td>
      </tr>
   </tbody>
  </table


945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/voruby/votable/1.0/votable.rb', line 945

def to_html
  builder = Builder::XmlMarkup.new(:indent => 2, :margin => 2)
  
  tbl_options = {}
  tbl_options[:id] = self.id if self.id
  
  builder.table(tbl_options) { |table|
    table.caption(self.description.text) if self.description
    
    table.thead { |thead|
      if self.fields.size > 0
        thead.tr { |tr|
          self.fields.each do |field|
            tr.th { |th|
              th << field.to_html
            }
          end
        }
      end
    }
    
    if self.data and self.data.format
      table.tbody { |tbody|
        self.data.format.trs.each do |tr|
          tbody << tr.to_html
        end
      }
    end
  }
end