Class Annotations
In: lib/facets/yore/annotation.rb
Parent: Object

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.

Synopsis

  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

Methods

==   []   []=   annotated_base   each   key?   method_missing   new   self   to_h  

Public Class methods

Public Instance methods

def inheritance

  anns = Annotations.new(@base)
  @base.ancestors.each do |anc|
    anc.annotations.each { |k,v|
      next if anns.key?(k)
      anns[k] = v #.dup
    }
  end
  #@ann.each_key do |name|
  #  anns[name] = self[name].heritage
  #end
  return anns

end

[Validate]