XMLable 0.1.1

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
=> … </>
irb(main):004:0> [1,2,3].to_xml.to_s
=> “12</
ritem>3”
irb(main):005:0> [1,Time.now,nil,[“wow”,:nice]].to_xml.to_s
=> "11187725687.81283<ritem rclass=‘NilClass’/

wow</
ritem>nice"
irb(main):006:0> data = [1,nil,“lalala”].to_xml
=> … </>
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
=> “5400”
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
=> “checkthis5400</
robject>”
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/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
    bored, I ended up writing my own library… :stuck_out_tongue:

Enjoy!