Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DeprecatedError < Exception; end
- module Deprecated
- def __deprecated_run_hook__(sym)
- if self.class.instance_eval { @__deprecated_run_hook__ }
- self.class.instance_eval { @__deprecated_run_hook__ }.call(self.class, sym)
- else
- Deprecated.run_hook(self.class, sym)
- end
- end
- def self.set_action(type=nil, &block)
- @action = if block
- block
- else
- case type
- when :warn
- proc { |klass, sym| warn "#{klass}##{sym} is deprecated." }
- when :die
- proc { |klass, sym| fail "#{klass}##{sym} is deprecated." }
- when :raise
- proc { |klass, sym| raise DeprecatedError, "#{klass}##{sym} is deprecated." }
- else
- raise ArgumentError, "you must provide a symbol or a block to set_hook()."
- end
- end
- end
- def self.run_action(klass, sym)
- raise "no hook" unless @action
- @hook.call(klass, sym)
- end
- def self.action
- @action
- end
- end
- module Deprecated
- module Module
- def deprecate(sym, scope=nil)
- raise ArgumentError, "deprecate() requires symbols for its arguments" unless sym.kind_of?(Symbol)
- 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
- def deprecated_set_hook(&block)
- raise "blah" unless block
- @__deprecated_run_hook__ = block
- end
- end
- def self.included(base)
- base.extend(Module)
- end
- end
Add Comment
Please, Sign In to add comment