Class | Annotations |
In: |
lib/facets/yore/annotation.rb
|
Parent: | Object |
Annotations allows you to annontate objects, including methods with arbitrary "metadata". These annotations don‘t do anything in themselves. They are merely comments. But you can put them to use. For instance an attribute validator might check for an annotation called :valid and test against it.
Annotation is an OpenObject, and is used across the board for keeping annotations.
Annotation class serves for both simple and inherited cases depending on whether a base class is given.
class X attr :a ann :@a, :valid => lambda{ |x| x.is_a?(Integer) } def validate instance_variables.each { |iv| if validator = self.class.ann(iv)[:valid] value = instance_variable_get(iv) unless validator.call(vale) raise "Invalid value #{value} for #{iv}" end end } end end