Created
January 19, 2016 07:41
-
-
Save dux/af8fb6e50d412ea3e482 to your computer and use it in GitHub Desktop.
Java style annotations in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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