Ara's double_x : generate xml?


#1

Does this library support generating of xml (not xhtml)? what’s the top
level call? I tried

xml_ { …} and it dies with
irb(main):021:0> doc = xml_ { p_ “a” }
NoMethodError: undefined method create' for nil:NilClass from c:/ruby/lib/ruby/gems/1.8/gems/xx-0.1.0/lib/xx.rb:197:inmethod_missing’
from (eval):10:in xml_' from c:/ruby/lib/ruby/gems/1.8/gems/xx-0.1.0/lib/xx.rb:208:insend’
from c:/ruby/lib/ruby/gems/1.8/gems/xx-0.1.0/lib/xx.rb:208:in
`method_missing’
from (irb):21
from :0
irb(main):022:0>

Thanks.


#2

On Mon, 27 Mar 2006, itsme213 wrote:

   from c:/ruby/lib/ruby/gems/1.8/gems/xx-0.1.0/lib/xx.rb:208:in

`method_missing’
from (irb):21
from :0
irb(main):022:0>

Thanks.

you just need to mixin the method you want, in this case the xml
methods:

harp:~ > cat a.rb
require ‘xx’
include XX::XML

puts xml_{ p_{ “a” } }
harp:~ > ruby a.rb

<?xml version='1.0'?>

a

regards.

-a


#3

thank you sir. love the library. is the ‘_’ convention something that is
(or
should be) configurable?

i’m also looking forward to experimenting with traits. it sounds like
something that could be used for recording meta-data in lots of places,
including things like nitro/og.

removed_email_address@domain.invalid wrote in message
news:removed_email_address@domain.invalid…


#4

On Tue, 28 Mar 2006, itsme213 wrote:

thank you sir. love the library. is the ‘_’ convention something that is (or
should be) configurable?

it is actually configurable. you do something like

xx_configure “method_missing”, XX::STRICT # tag methods
require ‘
xx_configure “method_missing”, XX::CRAZY_LIKE_A_HELL # tag methods no
not require '

in your class.

you example is a great example of why not to do that however,
without the
‘_’ this

xml{ p ‘a’ }

would call ruby’s built-in ‘p’ method. this would fail, but silently.
one of
the big differences with xx and other html/xml generation libs is that
it’s
designed to be mixed into other classes to give them html generation
capbilities (a la widgets). the thing is, when you mixin to a class
like,
say, Array, you’ve already lost a huge part of you namespace like
‘length’,
‘find’, ‘each’, etc. none of these methods will trigger method missing
and so
cannot be tag methods. this is less a big deal for html than for xml,
which
can contain pretty much anything as tag names. if one uses a blanket
method
missing approach, or even a receiver as the tag generator, one simply
cannot
generate xml using

o.type{ } # oops, all object have ‘type’
o.id{ } # oops, all object have ‘id’

you can use a BlankSlate here… but then you’ve also lost the ability
to
mixin rather than agregate.

i’m also looking forward to experimenting with traits. it sounds like
something that could be used for recording meta-data in lots of places,
including things like nitro/og.

for sure. it’s one of the things it was designed for. being able to
get
back list of traits is very valuable.

for instance, this line, taken from an ‘initialize’ method, iterates
over all
of this object’s class traits and creates an instance trait for the
current
object that gets a deep copy of the class trait as the default value:

klass = self.class
mcp = lambda{|obj| Marshal::load(Marshal::dump(obj))}
klass::class_traits.each{|r,w| klass::trait®{ mcp[klass.send®]}}

so basically it says : “i have all the same traits as my class and i’ll
bootstrap myself from the values of those traits which have been
parameterized
in my class”

this reason this is done is that this class is part of a hierarchy where
each
subclass adds more traits to parameterize the class. with this generic
line i
can make any instance of that subclass bootstrap properly with no new
code.
so

class Parent
class_trait ‘p’ => 42
def intialize

end
end
class Child < Parent
class_trait ‘c’ => 42.0
end

child = Child::new

and here ‘child’ would have the instance traits ‘a’ and ‘b’ with
default
values gotten from the class method.

regards.

-a


#5

removed_email_address@domain.invalid wrote in message

it is actually configurable. you do something like

I did not mean a blanket method_missing. My real question was:

  • if I have a conflict with the ‘*_’ convention, &
  • I have another convention that I’d like to use e.g. ‘*_x’
    would I be able to configure to use that convention?

this reason this is done is that this class is part of a hierarchy where
each
subclass adds more traits to parameterize the class.

Is it possible to have different ways of combining up the class/module
chains? e.g. override? append? other?

Thanks!


#6

On Tue, 28 Mar 2006, itsme213 wrote:

removed_email_address@domain.invalid wrote in message

it is actually configurable. you do something like

I did not mean a blanket method_missing. My real question was:

  • if I have a conflict with the ‘*_’ convention, &
  • I have another convention that I’d like to use e.g. ‘*_x’
    would I be able to configure to use that convention?

ah. no. however, it would be a 3 minute exercise to add this
capability.
would you like me too?

this reason this is done is that this class is part of a hierarchy where
each subclass adds more traits to parameterize the class.

Is it possible to have different ways of combining up the class/module
chains? e.g. override? append? other?

hmm. override is the default. let me think about append.

regards.

-a


#7

removed_email_address@domain.invalid wrote in message

ah. no. however, it would be a 3 minute exercise to add this capability.
would you like me too?

If you think it would be generally useful. For right now I’m fine with
‘_’.

Is it possible to have different ways of combining up the class/module
chains? e.g. override? append? other?

hmm. override is the default. let me think about append.

A trait of a trait, perhaps :wink: