Class: Gravitext::XMLProd::Element
- Inherits:
-
Object
- Object
- Gravitext::XMLProd::Element
- Defined in:
- lib/gravitext-xmlprod/extensions.rb
Constant Summary collapse
- TRUTH =
lambda { |e| true }
Instance Method Summary collapse
-
#[](name) ⇒ Object
Shorthand for attribute accessed either by name (default Namespace) or Attribute object.
- #_find_r(tag, pred) ⇒ Object
- #_select_r(matches, tag, pred) ⇒ Object
- #characters ⇒ Object
-
#find(tag = nil) ⇒ Object
Return first child element matching tag and/or where yielding to block returns true.
-
#find_r(tag = nil, &block) ⇒ Object
Return first descendant element matching the specified tag and/or where yielding to block returns true.
-
#select(tag = nil) ⇒ Object
Return Array of child elements matching tag and/or further constrained where yielding to block returns true.
-
#select_r(tag = nil, &block) ⇒ Object
Return Array of descendant elements matching tag and/or where yielding to block returns true.
-
#to_xml(opts = {}) ⇒ Object
Serialize self to String of XML, with options.
Instance Method Details
#[](name) ⇒ Object
Shorthand for attribute accessed either by name (default Namespace) or Attribute object.
34 35 36 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 34 def []( name ) attribute( name ) end |
#_find_r(tag, pred) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 96 def _find_r( tag, pred ) children.each do |c| if c.element? found = c if ( tag.nil? || c.tag == tag ) && pred.call( c ) found ||= c._find_r( tag, pred ) return found if found end end nil end |
#_select_r(matches, tag, pred) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 107 def _select_r( matches, tag, pred ) children.each do |c| if c.element? if ( tag.nil? || c.tag == tag ) && pred.call( c ) matches << c else c._select_r( matches, tag, pred ) end end end matches end |
#characters ⇒ Object
38 39 40 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 38 def characters XMLHelper.element_characters( self ) end |
#find(tag = nil) ⇒ Object
Return first child element matching tag and/or where yielding to block returns true. Without a block is equivalent to first_element.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 68 def find( tag = nil ) tag = Tag.new( tag, Tag.WILDCARD_NS ) unless tag.nil? || tag.is_a?( Tag ) if block_given? children.find do |c| c.element? && ( tag.nil? || c.tag == tag ) && yield( c ) end else first_element( tag ) end end |
#find_r(tag = nil, &block) ⇒ Object
Return first descendant element matching the specified tag and/or where yielding to block returns true. Elements failing the tag/block test will be recursed into, in search of the first match.
83 84 85 86 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 83 def find_r( tag = nil, &block ) tag = Tag.new( tag, Tag.WILDCARD_NS ) unless tag.nil? || tag.is_a?( Tag ) _find_r( tag, block || TRUTH ) end |
#select(tag = nil) ⇒ Object
Return Array of child elements matching tag and/or further constrained where yielding to block returns true.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 44 def select( tag = nil ) tag = Tag.new( tag, Tag.WILDCARD_NS ) unless tag.nil? || tag.is_a?( Tag ) if block_given? children.select do |c| c.element? && ( tag.nil? || c.tag == tag ) && yield( c ) end else children.select do |c| c.element? && ( c.tag == tag ) end end end |
#select_r(tag = nil, &block) ⇒ Object
Return Array of descendant elements matching tag and/or where yielding to block returns true. Elements failing the tag/block test will be recursed into, in search of more matches.
60 61 62 63 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 60 def select_r( tag = nil, &block ) tag = Tag.new( tag, Tag.WILDCARD_NS ) unless tag.nil? || tag.is_a?( Tag ) _select_r( [], tag, block || TRUTH ) end |
#to_xml(opts = {}) ⇒ Object
Serialize self to String of XML, with options.
89 90 91 92 93 94 |
# File 'lib/gravitext-xmlprod/extensions.rb', line 89 def to_xml( opts = {} ) XMLHelper.write_element( self, opts[ :indentor ] || Indentor::COMPRESSED, opts[ :qmark ] || QuoteMark::DOUBLE, opts[ :implied_ns ] || [] ) end |