Class: RSCM::Accurev::ElementBackedClass
- Inherits:
-
Object
- Object
- RSCM::Accurev::ElementBackedClass
- 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" ="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
Direct Known Subclasses
AcResponseData, CommentData, ElementData, MessageData, TransactionData, VersionData
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Class Method Summary collapse
-
.children(*kidtypes) ⇒ Object
currently advisory only.
- .element_name(name) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
ebc is a synonym for ebc.children.
-
#initialize(element = nil) ⇒ ElementBackedClass
constructor
A new instance of ElementBackedClass.
- #to_s ⇒ Object
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
#children ⇒ Object
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_s ⇒ Object
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 |