Module: Annotation
- Defined in:
- lib/annotation.rb
Overview
annotation.rb – annotation library for ruby
ex.
require 'annotation'
module ControllerAnnotations
extend Annotation
def GET(method_name, path)
(@__routes ||= []) << [path, :GET, method_name]
end
def POST(method_name, path)
(@__routes ||= []) << [path, :POST, method_name]
end
def login_required(method_name)
alias_method "__orig_#{method_name}", method_name
s = "def #{method_name}(*args)
raise '302 Found' unless @current_user
__orig_#{method_name}(*args)
end"
self.class_eval s # not 'eval(s)'
end
annotation :GET, :POST, :login_required # !!!!!!
end
class Controller
extend ControllerAnnotations
GET('/')
def index
"index() called."
end
GET('/:id')
def show(id)
"show(#{id}) called."
end
POST('/:id')
login_required
def update(id)
"update(#{id}) called."
end
p @__routes #=> [["/", :GET, :index],
# ["/:id", :GET, :show],
# ["/:id", :POST, :update]]
end
p Controller.new.update(123) #=> 302 Found (RuntimeError)
See README.txt for more examples.
Defined Under Namespace
Modules: MethodAdded
Constant Summary collapse
- VERSION =
"$Release: 0.1.2 $".split(' ')[1]
Instance Method Summary collapse
Instance Method Details
#annotation(*names) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/annotation.rb', line 76 def annotation(*names) self.class_eval do s = "" names.each do |name| aliased = alias_name(name) alias_method aliased, name private aliased s << "def #{name}(*args) (@__annotations ||= []) << [:#{aliased}, args] end protected :#{name}\n" # or private? end self.class_eval s end end |