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



5105
5106
5107
5108
5109
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
5142
# File 'lib/QuickBaseClient.rb', line 5105

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.



5103
5104
5105
# File 'lib/QuickBaseClient.rb', line 5103

def element_hash
  @element_hash
end

Instance Method Details

#processChildElements(element, leafElementsOnly, block) ⇒ Object



5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
# File 'lib/QuickBaseClient.rb', line 5179

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.



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
5175
5176
5177
# File 'lib/QuickBaseClient.rb', line 5146

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