Class | Atom::Element |
In: |
lib/atom/element.rb
|
Parent: | Object |
The Class’ methods provide a DSL for describing Atom‘s structure
(and more generally for describing simple namespaced XML)
base | [RW] | this element‘s xml:base |
extensions | [R] | xml elements and attributes that have been parsed, but are unknown |
# File lib/atom/element.rb, line 396 def self.builders &block if ancestors[1].respond_to? :builders ancestors[1].builders &block end @on_build ||= [] @on_build.each &block end
defines a getter that calls ‘block‘
# File lib/atom/element.rb, line 492 def self.def_get(name, &block) define_method name.to_sym, &block end
defines a setter that calls ‘block‘
# File lib/atom/element.rb, line 497 def self.def_set(name, &block) define_method "#{name}=".to_sym, &block end
# File lib/atom/element.rb, line 387 def self.do_parsing e, root if ancestors[1].respond_to? :do_parsing ancestors[1].do_parsing e, root end @on_parse ||= [] @on_parse.each { |p| p.call e, root } end
wrapper for is_element
# File lib/atom/element.rb, line 345 def self.is_atom_element name self.is_element Atom::NS, name end
be sure to call super if you override this method!
# File lib/atom/element.rb, line 502 def initialize defaults = {} @extensions = [] @extensions.instance_variable_set('@attrs', {}) def @extensions.attributes @attrs end self.class.run_initters do |init| self.instance_eval &init end defaults.each do |k,v| set(k, v) end end
# File lib/atom/element.rb, line 382 def self.on_build &block @on_build ||= [] @on_build << block end
# File lib/atom/element.rb, line 519 def selfself..on_initon_init &block @on_init ||= [] @on_init << block end
turns a String, an IO-like, a REXML::Element, etc. into an Atom::Element
the ‘base’ base URL parameter should be supplied if you know where this XML was fetched from
if you want to parse into an existing Atom::Element, it can be passed in as ‘element‘
# File lib/atom/element.rb, line 412 def self.parse xml, base = '', element = nil if xml.respond_to? :elements root = xml.dup else xml = xml.read if xml.respond_to? :read begin root = REXML::Document.new(xml.to_s).root rescue REXML::ParseException => e raise Atom::ParseError, e.message end end unless root.local_name == self.self_name raise Atom::ParseError, "expected element named #{self.self_name}, not #{root.local_name}" end unless root.namespace == self.self_namespace raise Atom::ParseError, "expected element in namespace #{self.self_namespace}, not #{root.namespace}" end if root.attributes['xml:base'] base = (base.to_uri + root.attributes['xml:base']) end e = element ? element : self.new e.base = base # extension elements root.elements.each do |c| e.extensions << c end # extension attributes root.attributes.each do |k,v| e.extensions.attributes[k] = v end # as things are parsed, they're removed from e.extensions. whatever's # left over is stored so it can be round-tripped self.do_parsing e, root e end
# File lib/atom/element.rb, line 524 def self.run_initters &block @on_init.each(&block) if @on_init end
appends an element named ‘name’ in namespace ‘ns’ to ‘root’ ns is either [prefix, namespace] or just a String containing the namespace
# File lib/atom/element.rb, line 530 def append_elem(root, ns, name) if ns.is_a? Array prefix, uri = ns else prefix, uri = nil, ns end name = name.to_s existing_prefix = root.namespaces.find do |k,v| v == uri end root << if existing_prefix prefix = existing_prefix[0] if prefix != 'xmlns' name = prefix + ':' + name end REXML::Element.new(name) elsif prefix e = REXML::Element.new(prefix + ':' + name) e.add_namespace(prefix, uri) e else e = REXML::Element.new(name) e.add_namespace(uri) e end end
fill a REXML::Element with the data from this Atom::Element
# File lib/atom/element.rb, line 469 def build root if self.base and not self.base.empty? root.attributes['xml:base'] = self.base end self.class.builders do |builder| builder.call self, root end @extensions.each do |e| root << e.dup end @extensions.attributes.each do |k,v| root.attributes[k] = v end end
gets an attribute on xml
# File lib/atom/element.rb, line 370 def get_atom_attrb xml, name xml.attributes[name.to_s] end
gets a child element in the Atom namespace
# File lib/atom/element.rb, line 360 def get_atom_elem xml, name get_elem xml, Atom::NS, name end
gets multiple child elements in the Atom namespace
# File lib/atom/element.rb, line 365 def get_atom_elems xml, name get_elems Atom::NS, name end
gets a single namespaced child element
# File lib/atom/element.rb, line 350 def get_elem xml, ns, name REXML::XPath.first xml, "./ns:#{name}", { 'ns' => ns } end
gets multiple namespaced child elements
# File lib/atom/element.rb, line 355 def get_elems xml, ns, name REXML::XPath.match xml, "./ns:#{name}", { 'ns' => ns } end