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(r){ mcp[klass.send(r)]}}
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
···
On Tue, 28 Mar 2006, itsme213 wrote:
--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama