Class: Class

Inherits:
Object show all
Defined in:
lib/rscm/annotations.rb

Constant Summary collapse

@@anns =
{}
@@attr_anns =
{}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

:nodoc:



32
33
34
35
36
37
# File 'lib/rscm/annotations.rb', line 32

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

Instance Method Details

#ann(anns) ⇒ Object

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.



27
28
29
30
# File 'lib/rscm/annotations.rb', line 27

def ann(anns)
  @@attr_anns ||= {}
  @@attr_anns.merge!(anns)
end

#attr_accessor(*syms) ⇒ Object

:nodoc:



49
50
51
52
# File 'lib/rscm/annotations.rb', line 49

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

#attr_reader(*syms) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
# File 'lib/rscm/annotations.rb', line 40

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

#old_attr_readerObject

:nodoc:



39
# File 'lib/rscm/annotations.rb', line 39

alias old_attr_reader attr_reader