From: Rolando Abarca Date: 2007-08-22T05:32:56+09:00 Subject: [ANN] XMLable 0.1.1 --Apple-Mail-7--1032220225 Content-Type: multipart/alternative; boundary=Apple-Mail-6--1032220265 --Apple-Mail-6--1032220265 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=WINDOWS-1252; delsp=yes; format=flowed As we all need more free stuff that does the same*, here's another =20 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 =20 objects to/from XML data. (ok, right now is really simple and maybe not really non-secure, but =20 it doesn=91t mean it can=91t be a bit more "secure" in the future :-P) Just make sure that your class includes the XMLable module and the =20 constructor has only one parameter: a Hash with String keys for each =20 attribute you want to setup on initialization. You can also use the utility function init_attributes_with_opts to =20 initialize the attributes (check the doc) Oh, also, XMLable depends on REXML, so make sure you require =20 somewhere in your code =91rexml/document=92. Now, for the fun, some irb tricks: irb(main):001:0> require 'xmlable' =3D> true irb(main):002:0> require 'rexml/document' =3D> true irb(main):003:0> [1,2,3].to_xml =3D> ... irb(main):004:0> [1,2,3].to_xml.to_s =3D> "123" irb(main):005:0> [1,Time.now,nil,["wow",:nice]].to_xml.to_s =3D> "11187725687.81283wownice" irb(main):006:0> data =3D [1,nil,"lalala"].to_xml =3D> ... irb(main):007:0> Array.from_xml(data) =3D> [1, nil, "lalala"] irb(main):008:0> Array.from_xml(data.to_s) =3D> [1, nil, "lalala"] irb(main):009:0> data =3D {:hey =3D> "thisisfun", ["right","?"] =3D> = nil} =3D> {["right", "?"]=3D>nil, :hey=3D>"thisisfun"} irb(main):010:0> Hash.from_xml(data.to_xml) =3D> {["right", "?"]=3D>nil, :hey=3D>"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" =3D> 10}, opts) irb(main):017:4> end irb(main):018:3> end irb(main):019:2> end irb(main):020:1> end =3D> nil irb(main):021:0> c =3D A::B::C.new({"lala" =3D> 5400}) =3D> # irb(main):022:0> c.to_xml.to_s =3D> "5400" irb(main):023:0> another_c =3D A::B::C.from_xml(c.to_xml) =3D> # irb(main):024:0> data =3D ["check",:this,c].to_xml.to_s =3D> "checkthis5400" irb(main):025:0> Array.from_xml(data) =3D> ["check", :this, #] You can get it here (source and tests): http://rolando.cl/ruby/xmlable/src/xmlable.rb http://rolando.cl/ruby/xmlable/src/test.rb and the doc: http://rolando.cl/ruby/xmlable/doc/index.html * I just was lazy searching for some library, and because I was =20 bored, I ended up writing my own library... :-P Enjoy! --=20 Rolando Abarca Phone: +56-9 97851962 --Apple-Mail-6--1032220265 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=WINDOWS-1252 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=91t mean it can=91t 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 =91rexml/document=92.

Now, for the fun, some irb tricks:

irb(main):001:0> require = 'xmlable'
=3D> true
irb(main):002:0> require = 'rexml/document'
=3D> true
irb(main):003:0> = [1,2,3].to_xml
=3D> <rarray> ... = </>
irb(main):004:0> = [1,2,3].to_xml.to_s
=3D> "<rarray><ritem = rclass=3D'Fixnum'>1</ritem><ritem = rclass=3D'Fixnum'>2</ritem><ritem = rclass=3D'Fixnum'>3</ritem></rarray>"
irb(main):0= 05:0> [1,Time.now,nil,["wow",:nice]].to_xml.to_s
=3D> = "<rarray><ritem rclass=3D'Fixnum'>1</ritem><ritem = rclass=3D'Time'>1187725687.81283</ritem><ritem = rclass=3D'NilClass'/><ritem = rclass=3D'Array'><rarray><ritem = rclass=3D'String'>wow</ritem><ritem = rclass=3D'Symbol'>nice</ritem></rarray></ritem></r= array>"
irb(main):006:0> data =3D = [1,nil,"lalala"].to_xml
=3D> <rarray> ... = </>
irb(main):007:0> = Array.from_xml(data)
=3D> [1, nil, = "lalala"]
irb(main):008:0> = Array.from_xml(data.to_s)
=3D> [1, nil, = "lalala"]
irb(main):009:0> data =3D {:hey =3D> = "thisisfun", ["right","?"] =3D> nil}
=3D> {["right", = "?"]=3D>nil, :hey=3D>"thisisfun"}
irb(main):010:0> = Hash.from_xml(data.to_xml)
=3D> {["right", "?"]=3D>nil, = :hey=3D>"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" =3D> 10}, = opts)
irb(main):017:4> end
irb(main):018:3> = end
irb(main):019:2> end
irb(main):020:1> = end
=3D> nil
irb(main):021:0> c =3D = A::B::C.new({"lala" =3D> 5400})
=3D> = #<A::B::C:0x22ffa8 @lala=3D5400>
irb(main):022:0> = c.to_xml.to_s
=3D> "<robject = rclass=3D'A::B::C'><rattr name=3D'lala' = rclass=3D'Fixnum'>5400</rattr></robject>"
irb(mai= n):023:0> another_c =3D A::B::C.from_xml(c.to_xml)
=3D> = #<A::B::C:0x229298 @lala=3D5400>
irb(main):024:0> = data =3D ["check",:this,c].to_xml.to_s
=3D> = "<rarray><ritem rclass=3D'String'>check</ritem><ritem= rclass=3D'Symbol'>this</ritem><ritem = rclass=3D'A::B::C'><robject rclass=3D'A::B::C'><rattr = name=3D'lala' = rclass=3D'Fixnum'>5400</rattr></robject></ritem></= rarray>"
irb(main):025:0> = Array.from_xml(data)
=3D> ["check", :this, = #<A::B::C:0x21d4c0 @lala=3D5400>]

You can get it here (source = and tests):


and the doc:


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

Enjoy!
=

= --Apple-Mail-6--1032220265-- --Apple-Mail-7--1032220225 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (Darwin) iD8DBQFGy0v59fOunnYODS4RAtYHAJ9XsL2GQ32k3uWxjYpS01OHHmW5UQCgrIaZ XdbiyPPlFxDmpQEBJC5gUOM= =sRAp -----END PGP SIGNATURE----- --Apple-Mail-7--1032220225--