Class: XML::Object::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/voruby/misc/libxml_ext.rb

Overview

A (essentially abstract) class that can be used in the creation of domain objects backed by XML documents.

Direct Known Subclasses

VORuby::VOTable::Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml = nil) ⇒ Base

Create a new domain object from a number of source formats. xml may be one of

  • a string

  • an IO object (i.e. a File object)

  • a LibXML Document (i.e. XML::Document)

  • a LibXML Node (i.e. XML::Node)

  • nothing (to create a blank object)

  • a hash where the keys of the hash represent the method to call

Assuming VOTable::V1_1::VOTable is a subclass of Baseā€¦

votable = VOTable::V1_1::VOTable.new('<VOTABLE version="1.1"/>')
votable = VOTable::V1_1::VOTable.new(File.new('myvotable.xml'))  # you could also use #from_file
votable = VOTable::V1_1::VOTable.new(XML::Document.file('myvotable.xml'))
votable = VOTable::V1_1::VOTable.new(XML::Node.new('VOTABLE'))
votable = VOTable::V1_1::VOTable.new()
votable = VOTable::V1_1::VOTable.new(:version => '1.1')


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/voruby/misc/libxml_ext.rb', line 48

def initialize(xml=nil)
  @node = case xml
    when String
      parser = XML::Parser.new
      parser.string = xml
      parser.parse.root
    when IO
      parser = XML::Parser.new
      parser.io = xml
      parser.parse.root
    when XML::Document
      xml.root
    when XML::Node
      xml
    when NilClass
      doc = XML::Document.new
      doc.root = XML::Node.new(self.class.element_name)
      doc.root
    when Hash
      doc = XML::Document.new
      doc.root = XML::Node.new(self.class.element_name)
      doc.root
    else
      raise "XML must be a XML::Document, XML::Node, String or IO object"
    end

    initialize_members(xml) if xml.is_a?(Hash)
end

Instance Attribute Details

#nodeObject (readonly)

The XML::Node used to store the state of the domain object.



14
15
16
# File 'lib/voruby/misc/libxml_ext.rb', line 14

def node
  @node
end

Class Method Details

.element_nameObject

Get the name of the element when serialized to XML that the object in question will take on. By default, the name of the class.



27
28
29
# File 'lib/voruby/misc/libxml_ext.rb', line 27

def self.element_name
  self.to_s
end

.from_file(filename) ⇒ Object

Create a domain object from an XML file on disk.

filename

the name of the file to parse

votable = VOTable::V1_1::VOTable.from_file('myvotable.xml')


20
21
22
# File 'lib/voruby/misc/libxml_ext.rb', line 20

def self.from_file(filename)
  self.new(XML::Document.file(filename))
end

Instance Method Details

#==(obj) ⇒ Object

Equality among domain objects. Always returns false, override to get less brain-dead result.



84
85
86
# File 'lib/voruby/misc/libxml_ext.rb', line 84

def ==(obj)
  false
end

#to_sObject

Serialize the domain object to XML.



78
79
80
# File 'lib/voruby/misc/libxml_ext.rb', line 78

def to_s
  self.node.to_s
end