The following is an implementation I just whipped up of annotation.
- annotations are cascaded up through superclasses.
- no way to remove an annotation (could override with a nil value
though...)
- allows you to use this syntax:
class Dude
annotate :a => :b
def get_jiggy
end
end
to annotate the method, which I think is much nicer than "annotate
:get_jiggy, :a => :b", though that
method is also available. This, however introduces a dependency on
method_added not being overridden which
I don't know how to avoid.
-----
class Module
def annotate(*args)
hash =
case args.length
when 2:
annotation_hash[args[0]].update(args[1])
when 1:
x = class_eval("@@__meta_next_update ||= {}")
x.update(args[0])
else
raise ArgumentError, "wrong number of arguments
(#{args.length})"
end
end
def annotation_hash
class_eval("@@__meta ||= Hash.new {|h,k| h[k]={}}")
end
private :annotation_hash
def annotations(symbol)
symbol = symbol.intern unless Symbol === symbol
my_hash = annotation_hash[symbol]
my_hash = superclass.annotations(symbol).merge(my_hash) if
superclass
my_hash
end
#it's nice to be able to annotate without naming the method (just
apply it to the next method added)
def method_added(symbol)
meta_hash = annotation_hash[symbol]
new_hash = class_eval("x = @@__meta_next_update||=nil;
@@__meta_next_update = nil;x")
meta_hash.update(new_hash) if new_hash
end
end
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################