Class: RSCM::Accurev::ElementBackedClass

Inherits:
Object
  • Object
show all
Defined in:
lib/rscm/scm/accurev/xml.rb

Overview

Abstract base class for defining typed mirror classes for XML elements.

A subclass of ElementBackedClass has:

  • A class static element_name method, identifying what type of XML element it shadows

  • A set of attributes shadowing the XML attributes of its target element

  • A map (children) of child elements, keyed by element name

  • A (optional) text_content attribute, populated when the target element has text subnodes.


Example

For the XML element:

<book title="The Monkey Wrench Gang" author="Edward Albee">
  <note>Lots of beer and dynamite</note>
</book>

You can define:

class Book < ElementBackedClass
  element_name 'book'
  attr_accessor :title, :author
end

And then use XMLMapper to create instances:

e = ...; # <book> node: REXML::Element
book = XMLMapper.map(e)
puts "Title: #{book.title}"
puts "Author: #{book.author}"
puts "Notes:"
book['note'].each do |n|
  puts n.text_content
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element = nil) ⇒ ElementBackedClass

Returns a new instance of ElementBackedClass.



117
118
119
120
121
122
# File 'lib/rscm/scm/accurev/xml.rb', line 117

def initialize( element=nil )
  @children = {}
  unless element.nil?
    self.set_from_element(element)
  end
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



104
105
106
# File 'lib/rscm/scm/accurev/xml.rb', line 104

def children
  @children
end

Class Method Details

.children(*kidtypes) ⇒ Object

currently advisory only



113
114
115
# File 'lib/rscm/scm/accurev/xml.rb', line 113

def self.children( *kidtypes )
  # nothing XXX
end

.element_name(name) ⇒ Object



106
107
108
109
110
# File 'lib/rscm/scm/accurev/xml.rb', line 106

def self.element_name( name )
  class << self
    self
  end.send( :define_method, "element_name" ) {name}
end

Instance Method Details

#[](key) ⇒ Object

ebc is a synonym for ebc.children



125
126
127
# File 'lib/rscm/scm/accurev/xml.rb', line 125

def []( key )
  return self.children[key]
end

#to_sObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rscm/scm/accurev/xml.rb', line 129

def to_s
  s = "<#{self.class.element_name} "
  self.instance_variables.each do |a|
    unless a == "@children"
      attr = a.sub( /^@/, "" )
      s += "#{attr}='#{self.send(attr)}' "
    end
  end
  s += "/>"
  return s
end