The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Annotations in Ruby

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Aslak Hellesøy

Posts: 29
Nickname: aslak
Registered: Mar, 2005

Aslak Hellesoy is a senior developer for ThoughtWorks, Inc.
Annotations in Ruby Posted: Mar 9, 2005 3:13 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Aslak Hellesøy.
Original Post: Annotations in Ruby
Feed Title: Aslak Hellesøy's uncommon sense
Feed URL: http://aslakhellesoy.com/rss
Feed Description: Ruby-related blog posts from Aslak Hellesøy
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Aslak Hellesøy
Latest Posts From Aslak Hellesøy's uncommon sense

Advertisement

A couple of days ago I wished there was a way to use annotations in Ruby. I'm writing a Ruby on Rails application that renders a lot of objects in HTML. Many of these objects are "editable" via the web interface, and in order to make it easier for the user, I wanted the web interface to have both an explanatory text and a HTML tooltip for each object's field.

Because I like simple things I also like to keep things in one place (I don't want to maintain this metadata in a separate RHTML template).

Ruby doesn't support annotations out of the box, so I opened up the Class class and added support for annotations. Now I can do:

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 class' annotations can then be accessed like this:

EmailSender.server[:description] # => "IP address of the mail server"

It took the Java community forever to do stuff like this. First a couple of years of XDoclet, then JSR175. And tons of code too. In Ruby it's 1 hour's work and 25 lines of code. Go figure.

For more info see http://rscm.rubyforge.org/classes/Class.html

And oh, here's the code: class Class def ann(anns) @@anns ||= {} def self.method_missing(sym, *args) #:nodoc: @@anns[sym] end $attr_anns ||= {} $attr_anns.merge!(anns) end alias old_attr_reader attr_reader #:nodoc: def attr_reader(*syms) #:nodoc: syms.each do |sym| @@anns[sym] = $attr_anns end $attr_anns = nil old_attr_reader(*syms) end def attr_accessor(*syms) #:nodoc: attr_reader(*syms) attr_writer(*syms) end end UPDATE (March 3 2005) An updated version of the Ruby annotations library is available at rafb

Read: Annotations in Ruby

Topic: Public Todo list for Wee Previous Topic   Next Topic Topic: Get a life-time TextDrive for $399

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use