[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] XMLable 0.1.1

Rolando Abarca

8/21/2007 8:33:00 PM

As we all need more free stuff that does the same*, here's another
XML serialization library:

XMLable 0.1.1

Some copy-paste from the docs:

Simple, and I mean really-simple, non-secure way to convert ruby
objects to/from XML data.
(ok, right now is really simple and maybe not really non-secure, but
it doesn‘t mean it can‘t be a bit more "secure" in the future :-P)
Just make sure that your class includes the XMLable module and the
constructor has only one parameter: a Hash with String keys for each
attribute you want to setup on initialization.
You can also use the utility function init_attributes_with_opts to
initialize the attributes (check the doc)
Oh, also, XMLable depends on REXML, so make sure you require
somewhere in your code ‘rexml/document’.

Now, for the fun, some irb tricks:

irb(main):001:0> require 'xmlable'
=> true
irb(main):002:0> require 'rexml/document'
=> true
irb(main):003:0> [1,2,3].to_xml
=> <rarray> ... </>
irb(main):004:0> [1,2,3].to_xml.to_s
=> "<rarray><ritem rclass='Fixnum'>1</ritem><ritem rclass='Fixnum'>2</
ritem><ritem rclass='Fixnum'>3</ritem></rarray>"
irb(main):005:0> [1,Time.now,nil,["wow",:nice]].to_xml.to_s
=> "<rarray><ritem rclass='Fixnum'>1</ritem><ritem
rclass='Time'>1187725687.81283</ritem><ritem rclass='NilClass'/
><ritem rclass='Array'><rarray><ritem rclass='String'>wow</
ritem><ritem rclass='Symbol'>nice</ritem></rarray></ritem></rarray>"
irb(main):006:0> data = [1,nil,"lalala"].to_xml
=> <rarray> ... </>
irb(main):007:0> Array.from_xml(data)
=> [1, nil, "lalala"]
irb(main):008:0> Array.from_xml(data.to_s)
=> [1, nil, "lalala"]
irb(main):009:0> data = {:hey => "thisisfun", ["right","?"] => nil}
=> {["right", "?"]=>nil, :hey=>"thisisfun"}
irb(main):010:0> Hash.from_xml(data.to_xml)
=> {["right", "?"]=>nil, :hey=>"thisisfun"}
irb(main):011:0> module A
irb(main):012:1> module B
irb(main):013:2> class C
irb(main):014:3> include XMLable
irb(main):015:3> def initialize(opts)
irb(main):016:4> init_attributes_with_opts({"lala" => 10}, opts)
irb(main):017:4> end
irb(main):018:3> end
irb(main):019:2> end
irb(main):020:1> end
=> nil
irb(main):021:0> c = A::B::C.new({"lala" => 5400})
=> #<A::B::C:0x22ffa8 @lala=5400>
irb(main):022:0> c.to_xml.to_s
=> "<robject rclass='A::B::C'><rattr name='lala'
rclass='Fixnum'>5400</rattr></robject>"
irb(main):023:0> another_c = A::B::C.from_xml(c.to_xml)
=> #<A::B::C:0x229298 @lala=5400>
irb(main):024:0> data = ["check",:this,c].to_xml.to_s
=> "<rarray><ritem rclass='String'>check</ritem><ritem
rclass='Symbol'>this</ritem><ritem rclass='A::B::C'><robject
rclass='A::B::C'><rattr name='lala' rclass='Fixnum'>5400</rattr></
robject></ritem></rarray>"
irb(main):025:0> Array.from_xml(data)
=> ["check", :this, #<A::B::C:0x21d4c0 @lala=5400>]

You can get it here (source and tests):

http://rolando.cl/ruby/xmlable/src/...
http://rolando.cl/ruby/xmlable/s...

and the doc:

http://rolando.cl/ruby/xmlable/doc/...

* I just was lazy searching for some library, and because I was
bored, I ended up writing my own library... :-P

Enjoy!
--
Rolando Abarca
Phone: +56-9 97851962