Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Unify the handling of XML records (See related posts)

This Ruby code creates, updates or deletes an XML record, using a hash, record handling objects, and XML to invoke the correct method.

#file: recordx_handler.rb
require 'recordx'

class RecordX_Update < RecordX
  def call(params)
    #todo: write the code to read the xml parameter string
    update_record(h)
    save_file 
  end
end

class RecordX_Create < RecordX
  def call(params)
    #todo: write the code to read the xml parameter string  
    create_record(h)
  end
end

class RecordX_Delete < RecordX
  def call(params)
    doc = Document.new(params)
    node = doc.root.elements["param[@var='id']"]
    puts node
    id = node.attributes.get_attribute('val').to_s
    delete_record(id)
    puts 'deleted record ' + id
    save_file
  end
end

class RecordX_handler
  def invoke(method, params)
    h = Hash.new
    h["create"] = RecordX_Create.new
    h["update"] = RecordX_Update.new
    h["delete"] = RecordX_Delete.new
    h[method].call(params)
  end
end

if __FILE__ == $0
  xml_method = "<method name='delete'><params><param var='id' val='17648' /></params></method>"
  doc = Document.new(xml_method)
  method = doc.root.attributes.get_attribute('name').to_s
  params = doc.root.elements['params'].to_s

  rh = RecordX_handler.new
  rh.invoke(method, params)
end


This code is intended to be called by a Ruby CGI script which can simply relay the cgi post argument to the recordx_handler object.

You need to create an account or log in to post comments to this site.


Click here to browse all 4875 code snippets

Related Posts