Newsgroups: comp.lang.ruby
From: Phlip <phlip2...@gmail.com>
Date: Sun, 8 Mar 2009 10:53:21 +0900
Local: Sat, Mar 7 2009 8:53 pm
Subject: [ANN] ActiveRecord .from_xml upgrade
Rubies:
The gist of this tiny code snip... ...is a light but flexible DSL that converts XML - typically output by to_xml() ==create or update records== Here's the simplest example: xml ='<photos> doc = Nokogiri::XML(xml) (Note that from_xml{} is a member of a Node, not of your Model.) That code created new Photo records with matching IDs. If any record were ==rename fields and pass in data== Here's the next more complicated example: authors = node.from_xml(Author, [:id, :remote_id], :name) The code reads an <id> tag, then finds or creates an author with a ==associations== from_xml takes an optional &block, and yields into this the record under doc.from_xml Post, :id, :title, :body do |post, node, *| from_xml{} will call that block each time it finds a (top-level) <post> record, (Note, also, that <tag> records, for example, should be shared between many The splat operator * threw away three more arguments - they were the string ==raw XML== To scan your XML with very similar abilities, but without using a Model with the node.convert 'tags/tag', :id, :name do |n, id, name| That block shows form_tag{} "unrolled" into its low-level behavior. convert{} Your block could have done something more complex, but this one merely simulated One more detail - the renamed fields, and their string values, are also ==what about to_xml?== One ActiveRecord FAQ goes, "Why can't from_xml take the same arguments as -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||