Annotation
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
Authors
- Thomas Sawyer
- George Moschovitis
History
- 2006-11-07 trans Created this ultra-concise version of annotations.
Todo
- Might be nice to have a default object of annotation, eg. the next method defined, like how desc annotates a rake task.
- The ann(x).name notation is kind of nice. Would like to add that back-in if reasonable. Basically this require heritage to be an OpenObject rather than just a hash.
Copying
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.
- facets/hash/to_h
- facets/hash/rekey
- facets/hash/op
- facets/annotations.rb
- facets/inheritor.rb