Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Module
- def deprecate(sym, scope=nil)
- meth = instance_method(sym)
- unless scope
- pub = public_instance_methods
- pro = protected_instance_methods
- pri = private_instance_methods
- if pub.include?(sym) or pub.include?(sym.to_s)
- scope = :public
- elsif pro.include?(sym) or pro.include?(sym.to_s)
- scope = :protected
- elsif pri.include?(sym) or pri.include?(sym.to_s)
- scope = :private
- end
- end
- define_method(sym) do |*args|
- dep_meth = method(sym).unbind
- __deprecated_run_hook__(sym)
- retval = meth.bind(self).call(*args)
- dep_meth.bind(self)
- return retval
- end
- method(scope).call(sym) if scope
- return scope
- end
- end
- module Deprecated
- def __deprecated_run_hook__(sym)
- if @__deprecated_run_hook__
- @__deprecated_run_hook__.call(self.class, sym)
- else
- Deprecated.run_hook(self.class, sym)
- end
- end
- def deprecated_set_hook(&block)
- raise "blah" unless block_given?
- @__deprecated_hook__ = block.to_proc
- end
- def self.set_hook(type=nil, &block)
- @hook = case type
- when :warn
- proc { |klass, sym| warn "#{klass}##{sym} is deprecated." }
- when :die
- proc { |klass, sym| "stuff" }
- when :raise
- proc { |klass, sym| "stuff" }
- else
- raise "blah"
- end
- end
- def self.run_hook(klass, sym)
- raise "no hook" unless @hook
- @hook.call(klass, sym)
- end
- end
- class Foo
- include Deprecated
- def foo
- "bar"
- end
- deprecate :foo
- end
- Deprecated.set_hook(:warn)
- p Foo.new.foo
Add Comment
Please, Sign In to add comment