Simple Annotations 4 Ruby

Simple method annotations like in java or Python methods decorators

GitHub

Documentation GitHub issues GitHub tag (latest by date) GitHub top language GitHub milestones

Gem Gem Version Twitter Follow GitHub Org's stars GitHub watchers

Installation

Install it yourself as:

$ gem install carioca

Usage

Adding annotations to a class

require 'rubygems'
require 'simple-annotations'

class A
  using AnnotationRefinement

  annotate!

  §test 'string'
  def method
    return annotations(__callee__)
  end

end

anA = A.new
pp anA.method

Display

{:test=>"string"}

Getting fields

require 'rubygems'
require 'simple-annotations'

class A
  using AnnotationRefinement
  annotate!

  §foobar {:color => 'cyan' }   # Hash
  §test 1234                    # Numeric
  §foobar color: 'cyan'         # Hash by double splat, like keyword
  §testbar 10, {}, [], 'string' # Hybrid by splat
  §barfoo                       # Boolean set true
  §footest 'string'             # String                    
  def method
      [...]
  end

end

pp A.annotations[:method]

Display

{:test=>1234, :foobar=>{:color=>"cyan"}, :testbar=>[10, {}, [], "string"], :barfoo=>true, :footest=>"string"}

Defining Hooks

Supporting 2 Hooks :

  • §after
  • §before

Like :

require 'rubygems'
require 'simple-annotations'

class A
  using AnnotationRefinement

  annotate!


  §after -> { puts 'after' }
  §before -> { puts 'before' }
  def m1
    puts 'test'
  end


end



anA = A.new
anA.m1

Display

before
test
after