Class: REXML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/QuickBaseClient.rb

Overview

This enables finding XML elements recursively using Ruby method syntax

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *method_args) ⇒ Object



5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
# File 'lib/QuickBaseClient.rb', line 5069

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_hashObject

Returns the value of attribute element_hash.



5067
5068
5069
# File 'lib/QuickBaseClient.rb', line 5067

def element_hash
  @element_hash
end

Instance Method Details

#processChildElements(element, leafElementsOnly, block) ⇒ Object



5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
# File 'lib/QuickBaseClient.rb', line 5143

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.



5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
# File 'lib/QuickBaseClient.rb', line 5110

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