Sha256: 2eee24be31e7c983b4373e3d2e9d91a81c27f016af2638d9ae2aba989b91ddd5

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Anise
  require 'anise/annotation.rb'

  # = Annotator
  #
  # Annotator allows for the creation of dynamic <i>method
  # annotations</i> which attach to the next method defined.
  #
  #   require 'anise/annotator'
  #
  #   class X
  #     include Anise::Annotator
  #
  #     annotator :doc
  #
  #     doc "See what I mean?"
  #
  #     def see
  #       puts "Yes, I see!"
  #     end
  #   end
  #
  #   X.ann(:see, :doc) #=> "See what I mean?"
  #
  # This idiom of annotator before definition was popularized by
  # Rake's desc/task pair. Annotator makes it very easy to add
  # similar capabilites to any program.
  #
  # The library uses the #method_added callback, so be sure to
  # respect good practices of calling +super+ if you need to override
  # this method while using Annotator.
  #
  # TODO: Ensure thread safety of the internal <code>@pending_annotations</code> variable.
  #
  module Annotator

    def self.append_features(base)
      #if base == Object
      #  Module.send(:include, self)  # FIXME: Module ?
      #else
        base.extend Annotation #unless base.is_a?(Annotation)
        base.extend self
      #end
    end

    def annotator(name)
      (class << self; self; end).module_eval do
        define_method(name) do |*args|
          @pending_annotations ||= []
          @pending_annotations << [name, args]
        end
      end
    end

    def method_added(sym)
      @pending_annotations ||= []
      @pending_annotations.each do |name, args|
        ann sym, name => args
      end
      @pending_annotations = []
      super if defined?(super)
    end

  end

end

# Copyright (c) 2005, 2008 TigerOps

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
anise-0.2.2 lib/anise/annotator.rb