Class: REXML::Element
- Inherits:
-
Object
- Object
- REXML::Element
- Defined in:
- lib/broker/client/QuickBaseClient.rb
Overview
This enables finding XML elements recursively using Ruby method syntax
Instance Attribute Summary collapse
-
#element_hash ⇒ Object
Returns the value of attribute element_hash.
Instance Method Summary collapse
- #method_missing(method_name, *method_args) ⇒ Object
- #processChildElements(element, leafElementsOnly, block) ⇒ Object
-
#to_h(include_element = proc{|e|true}) ⇒ Object
Convert REXML Element tree into Hash.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *method_args) ⇒ Object
5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 |
# File 'lib/broker/client/QuickBaseClient.rb', line 5137 def method_missing(method_name,*method_args) ret = nil if elements if method_args and method_args.length > 0 if method_args[0].length > 1 searchProc = proc { |e| if e.is_a?(REXML::Element) and e.name == method_name.to_s and e.attributes if e.attributes[method_args[0][0].to_s] and e.attributes[method_args[0][0].to_s] == method_args[0][1].to_s ret = e end end } else searchProc = proc { |e| if e.is_a?(REXML::Element) and e.name == method_name.to_s and e.attributes if e.attributes["id"] and e.attributes["id"] == method_args[0][0].to_s ret = e end if ret.nil? and e.attributes["name"] and e.attributes["name"] == method_args[0][0].to_s ret = e end end } end else searchProc = proc { |e| if e.is_a?(REXML::Element) and e.name == method_name.to_s ret = e end } end end processChildElements( self, false, searchProc ) if ret and !ret.has_elements? and ret.has_text? ret = ret.text.dup end ret end |
Instance Attribute Details
#element_hash ⇒ Object
Returns the value of attribute element_hash.
5135 5136 5137 |
# File 'lib/broker/client/QuickBaseClient.rb', line 5135 def element_hash @element_hash end |
Instance Method Details
#processChildElements(element, leafElementsOnly, block) ⇒ Object
5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 |
# File 'lib/broker/client/QuickBaseClient.rb', line 5211 def processChildElements( element, leafElementsOnly, block ) if element if element.is_a?( Array ) element.each{ |e| processChildElements( e, leafElementsOnly, block ) } elsif element.is_a?( REXML::Element ) and element.has_elements? block.call( element ) if not leafElementsOnly element.each{ |e| if e.is_a?( REXML::Element ) and e.has_elements? processChildElements( e, leafElementsOnly, block ) else block.call( e ) end } end end end |
#to_h(include_element = proc{|e|true}) ⇒ Object
Convert REXML Element tree into Hash. Sibling elements with duplicate names become Arrays.
5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 |
# File 'lib/broker/client/QuickBaseClient.rb', line 5178 def to_h(include_element=proc{|e|true}) to_hash_proc = proc {|e| if e.is_a?(REXML::Element) and include_element.call(e) e.element_hash = {} e.element_hash["name"] = e.name if e.has_attributes? e.element_hash["attributes"] = {} e.attributes.each{|k,v|e.element_hash["attributes"][k]=v} end if e.has_text? text = e.text.strip e.element_hash["value"] = text if text.length > 0 end if e.parent and e.parent.is_a?(REXML::Element) if e.parent.element_hash and e.parent.element_hash[e.name] if e.parent.element_hash[e.name].is_a?(Array) e.parent.element_hash[e.name] << e.element_hash elsif e.parent.element_hash tmp = e.parent.element_hash[e.name].dup e.parent.element_hash[e.name] = [] e.parent.element_hash[e.name] << tmp e.parent.element_hash[e.name] << e.element_hash end elsif e.parent.element_hash e.parent.element_hash[e.name] = e.element_hash end end end } processChildElements( self, false, to_hash_proc ) element_hash end |