Skip to content

Instantly share code, notes, and snippets.

@dux
Created January 19, 2016 07:41
Show Gist options
  • Save dux/af8fb6e50d412ea3e482 to your computer and use it in GitHub Desktop.
Save dux/af8fb6e50d412ea3e482 to your computer and use it in GitHub Desktop.
Java style annotations in Ruby
module Annotations
def annotations(meth=nil)
return @__annotations__[meth] if meth
@__annotations__
end
private
def method_added(m)
(@__annotations__ ||= {})[m] = @__last_annotation__ if @__last_annotation__
@__last_annotation__ = nil
super
end
def method_missing(meth, *args)
return super unless /\A_/ =~ meth
@__last_annotation__ ||= {}
@__last_annotation__[meth[1..-1].to_sym] = args.size == 1 ? args.first : args
end
end
class A
extend Annotations
_hello color: 'red', ancho: 23
_goodbye color: 'green', alto: -123
_foobar color: 'blew'
def m1; end
def m2; end
_foobar color: 'cyan'
def m3; end
def self.a
@__annotations__
end
end
p A.a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment