Path: | lib/more/facets/annotations.rb |
Last Update: | Tue Oct 28 06:39:06 -0400 2008 |
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
Copyright (c) 2005 Thomas Sawyer, George Moschovitis
Ruby License
This module is free software. You may use, modify, and/or redistribute this software under the same terms as Ruby.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.