From: "Andrew S. Townley" Date: 2009-03-26T09:39:28+09:00 Subject: Re: [ANN] tagz-5.0.0 On Thu, 2009-03-26 at 09:30 +0900, ara.t.howard wrote: > On Mar 25, 2009, at 2:17 PM, Andrew S. Townley wrote: > > > Thanks. > > > > If you had to register the namespaces first in some kind of tagz > > registry, maybe you could do something by handling the NameError when > > you did: > > > > foo.bar_('bar:foo' => 42) > > > > If it had registered a namespace prefix of foo, then it'd just "do the > > right thing", otherwise, you'd get the normal name error. This would > > probably be the most logical syntax, but it's way beyond my Ruby > > magical > > abilities to figure out how to actually make it work. > > > > Still, I think it'd be the most readable of the proposed approaches... > > I don't want to have to find unicode characters on my keyboard! :) > > > > Cheers, > > how about this I like it, but would change it a bit to allow you to initialize the namespace URI. Not sure what you'd do with a default namespace declaration... > > cfp:~ > cat a.rb > require 'rubygems' > require 'tagz' > > class Namespace > def initialize(nsuri) &block @nsuri = nsuri > @ns = block.call.to_s > @context = block > @self = eval 'self', block > end > > instance_methods.each{|m| undef_method if m[%r/__/]} > > def method_missing m, *a, &b > if m.to_s[%r/^_|_$/] > @self.send("#{ @ns }:#{ m }", *a, &b) > else > "#{ @ns }:#{ m }" > end > end > end > > include Tagz.globally > > a = Namespace.new(".."){ :a } > b = Namespace.new(".."){ :b } > > xml = > a.root_(emit_nsuri => true, include_children => true) { > a.foo_(b.bar => 'bar'){ 'content' } > b.bar_(a.foo => 'foo'){ 'content' } > } > > puts xml > > > > cfp:~ > ruby a.rb > contentcontent b:bar> > What do you think? -- Andrew S. Townley http://atownley.org