Skip to content
rubyworksPublic

About

Dynamic Annotations System for Ruby

Resources

Stars

4 stars

Watchers

0 watching

Forks

Latest commit

 

History

142 Commits

Folders and files

Repository files navigation

Anise

Source Code / Documentation / Report Issue     CI

Dynamic Annotations for Ruby

Anise is an Annotation System for the Ruby programming language. Unlike most other annotations systems it is not a comment-based or macro-based system that sits over-and-above the rest of the code. Rather, Anise is a dynamic annotations system operating at runtime.

The following example briefly demonstrates all three major features. To use any of them first require the anise library.

 require 'anise'

General annotations are provided by the Anise::Annotations module.

class X
  extend Anise::Annotations

  ann :grape, :class=>String
end

X.ann(:grape, :class)  #=> String

Annotated attributes can be easily added to a class via the Annotative::Attributes module.

class X
  extend Anise::Annotative::Attributes

  attr :baz, Integer, :max => 10
end

X.ann(:baz)  #=> {:class=>Integer, :max=>10}

Method annotations can be had via the Annotative::Methods module.

class X
  extend Anise::Annotative::Methods

  def self.doc(string)
    method_annotation(:doc=>string)
  end

  doc "This is an entry."

  def bar
    # ...
  end
end

X.ann(:bar)  #=> {:doc=>"This is an entry."}

An attribute's :default annotation is its value until it is set. Each object gets its own copy, and a Proc is called in the object for the value.

class X
  extend Anise::Annotative::Attributes

  attr_accessor :list, :default => []
  attr_accessor :size, :default => proc{ list.size }
end

X.new.list  #=> []

One thing to know: in a class that extends Annotative::Attributes, a private or protected line on its own does not apply to the attributes after it. Ruby keeps that setting for the class body alone, where a method that wraps attr cannot see it. Name the attributes instead:

class X
  extend Anise::Annotative::Attributes

  attr :a, :doc => "one way"
  private :a

  private attr(:b, :doc => "another")
end

Methods defined with def are not affected.

Any of these modules can be used in conjunction. Since both Annotative::Methods and Annotative::Attributes preclude Annotations all three can be used by simply using the later two.

 class X
   extend Anise::Annotative::Attributes
   extend Anise::Annotative::Methods

   ...
 end

Note also that the Anise module is clean and contains only modules and classes with detailed names starting with "Annotat-", so it can be included in your own application's namespace, and its modules reached from there.

module MyApp
  include Anise

  class Foo
    extend MyApp::Annotative::Attributes

    ...
  end
end

More

An annotation's reference can be anything, not only a method's name, and annotations pass from a class or module to those that inherit from it.

class X
  extend Anise::Annotations

  ann :@count, :valid => lambda{ |x| x.is_a?(Integer) }
end

class Y < X
end

Y.ann(:@count, :valid)  #=> the lambda

Annotations are kept in namespaces. The standard one is ann. Another is named after a slash, which is how to write an annotator of your own:

class X
  def self.doc(ref, *keys)
    ann "#{ref}/doc", *keys
  end

  doc :grape, :text => "A fruit."
end

X.doc(:grape, :text)  #=> "A fruit."

With require 'anise/core_ext', the reference and namespace can also be written :grape/:doc. That adds a / method to Symbol and String, so it is left for you to ask for.

To have ann in every class and module without extending each one, use require 'anise/universal'.

A class is told of each annotation by its annotation_added(ref, namespace) class method, if it defines one.

Installation

It needs Ruby 3.1 or newer.

$ gem install anise

Documentation

The demonstrations in demo/ walk through each feature. They are QED documents, so every example in them is run and checked.

The API documentation is on rubydoc.info.

Development

Anise is on GitHub. Problems, questions and requests go in the issue tracker; to contribute, fork the repository and send a pull request.

Anise has demonstrations, run with QED, and unit tests, written with Spectrum on Rubytest:

$ bundle install
$ bundle exec rake demo
$ bundle exec rake test

Copyrights

Copyright (c) 2008 Rubyworks. All rights reserved.

This program is distributed under the terms of the BSD-2-Clause license.

See LICENSE.txt for details.

This project was created on 2008-02-21.

About

Dynamic Annotations System for Ruby

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages