Sha256: ba8cfb3e37c733ac413a8cc9925d7a862196d3ba0014b9ee94df0b0d1eb266fa

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

class Class
  @@anns = {}

  # Defines annotation(s) for the next defined +attr_reader+ or
  # +attr_accessor+. The +anns+ argument should be a Hash defining annotations
  # for the associated attr. Example:
  #
  #   require 'rscm/annotations'
  #
  #   class EmailSender
  #     ann :description => "IP address of the mail server", :tip => "Use 'localhost' if you have a good box, sister!"
  #     attr_accessor :server
  #   end
  #
  # The EmailSender class' annotations can then be accessed like this:
  #
  #   EmailSender.server[:description] # => "IP address of the mail server"
  #
  # Yeah right, cool, whatever. What's this good for? It's useful for example if you want to
  # build some sort of user interface (for example in on Ruby on Rails) that allows editing of
  # fields, and you want to provide an explanatory text and a tooltip in the UI.
  #
  # You may also use annotations to specify more programmatically meaningful metadata. More power to you.
  # 
  def ann(anns)
    $attr_anns ||= {}
    $attr_anns.merge!(anns)
  end

  def method_missing(sym, *args) #:nodoc:
    anns = @@anns[self]
    return superclass.method_missing(sym, *args) if(anns.nil?)
    anns[sym]
  end

  alias old_attr_reader attr_reader #:nodoc:
  def attr_reader(*syms) #:nodoc:
    @@anns[self] ||= {}
    syms.each do |sym|
      @@anns[self][sym] = $attr_anns.dup if $attr_anns
    end
    $attr_anns = nil
    old_attr_reader(*syms)
  end

  def attr_accessor(*syms) #:nodoc:
    attr_reader(*syms)
    attr_writer(*syms)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rscm-0.2.0 lib/rscm/annotations.rb
rscm-0.2.1.1404 lib/rscm/annotations.rb