Class: VORuby::VOTable::V1_0::Td

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

Overview

A data element in a table row.

Constant Summary collapse

ELEMENT_NAME =
'TD'

Instance Attribute Summary

Attributes inherited from XML::Object::Base

#node

Instance Method Summary collapse

Methods included from Castable

#as_obj, #as_string, #cast, #convert_to_s

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

Create a new data element.

td = Td.new(:text => '123.2')


1739
1740
1741
# File 'lib/voruby/votable/1.0/votable.rb', line 1739

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

Instance Method Details

#fieldObject

Retrieve the Field associated with this column.



1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
# File 'lib/voruby/votable/1.0/votable.rb', line 1779

def field
  node_path = self.node.path
  
  # use the xpath to determine the position
  pos = (node_path =~ /TD\[\d+\]$/) ?
   node_path.split('/').last.match(/TD\[(\d+)\]$/)[1] :
   '1'
  
  field_node = self.node.find_first("../../../../*[local-name()='FIELD'][#{pos}]")
  field_node ? Field.new(field_node) : raise("Unable to associate a FIELD with this TD")
end

#refObject



1743
1744
1745
# File 'lib/voruby/votable/1.0/votable.rb', line 1743

def ref
  self.node['ref']
end

#ref=(r) ⇒ Object



1747
1748
1749
# File 'lib/voruby/votable/1.0/votable.rb', line 1747

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

#textObject

Retrieve the value of the data element as text.



1752
1753
1754
# File 'lib/voruby/votable/1.0/votable.rb', line 1752

def text
  self.node.content
end

#text=(txt) ⇒ Object

Set the value of the data element as text.



1757
1758
1759
# File 'lib/voruby/votable/1.0/votable.rb', line 1757

def text=(txt)
  @node.content = txt.to_s
end

#to_htmlObject

Convert a column to HTML.

td = Td.new(:text => 'hello')
puts td.to_html # => <td>hello</td>


1794
1795
1796
1797
1798
# File 'lib/voruby/votable/1.0/votable.rb', line 1794

def to_html
  builder = Builder::XmlMarkup.new(:indent => 2, :margin => 5)
  
  builder.td(self.text)
end

#valueObject

Retrieve the value of the data element as an appropriate object (or list of objects), according to the datatype and arraysize of the Field corresponding to this column. See Castable#as_obj for more details.



1765
1766
1767
1768
# File 'lib/voruby/votable/1.0/votable.rb', line 1765

def value
  cfield = self.field
  as_obj(self.text, cfield.datatype, cfield.arraysize)
end

#value=(v) ⇒ Object

Set the value of the data element. An attempt is made to convert native ruby types using the datatype and arraysize parameters of the associated Field.



1773
1774
1775
1776
# File 'lib/voruby/votable/1.0/votable.rb', line 1773

def value=(v)
  cfield = self.field
  self.text = as_string(v, cfield.datatype, cfield.arraysize)
end