pax_global_header00006660000000000000000000000064122015231560014507gustar00rootroot0000000000000052 comment=28ecaa0dc239c868c25d6a760bbfab6bfecccd42 ruby-deprecated-3.0.1/000077500000000000000000000000001220152315600145675ustar00rootroot00000000000000ruby-deprecated-3.0.1/.gitignore000066400000000000000000000000261220152315600165550ustar00rootroot00000000000000.test-result pkg rdoc ruby-deprecated-3.0.1/MIT-LICENSE.txt000066400000000000000000000020341220152315600170400ustar00rootroot00000000000000Copyright (c) Erik Hollensbe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.ruby-deprecated-3.0.1/README000066400000000000000000000026551220152315600154570ustar00rootroot00000000000000Deprecated is a module to help you deprecate code BEFORE you remove it. Don't surprise your users, deprecate them! Usage is simple: class Foo include Deprecated def moo "cow" end deprecated :moo def sheep "baaa" end deprecated :sheep, "Sounds#baa" protected def bar true end deprecated :bar end Foo.new.moo # warns that the call is deprecated Deprecated.set_action(:raise) Foo.new.moo # raises with the same message Deprecated.set_action do |klass, sym, replacement| email_boss( "Someone tried to use #{klass}##{sym}! " + "They should be using #{replacement} instead!" ) end Foo.new.sheep # do I really need to explain? Foo.new.bar # still protected! Let's do it live! class Bar include Deprecated # sets it just for this class deprecated_set_action do |klass, sym, replacement| email_boss( "Someone tried to use #{klass}##{sym}! " + "They should be using #{replacement} instead!" ) end def cow "moo" end deprecate :cow # emails your boss when called! end Please see Deprecated::Module#deprecated, Deprecated.set_action, and Deprecated::Module#deprecated_set_action for more information. This library is released under the MIT license: * http://www.opensource.org/licenses/MITruby-deprecated-3.0.1/Rakefile000066400000000000000000000025031220152315600162340ustar00rootroot00000000000000# # Please see the COPYING file in the source distribution for copyright information. # require 'rubygems' require 'rubygems/package_task' require 'rake/testtask' require 'rdoc/task' $:.unshift 'lib' require 'deprecated' $:.shift task :default => [ :dist ] # # Tests # Rake::TestTask.new do |t| t.libs << 'lib' t.test_files = FileList['test/test*.rb'] t.verbose = true end # # Distribution # task :dist => [:test, :repackage, :gem, :rdoc] task :distclean => [:clobber_package, :clobber_rdoc] task :clean => [:distclean] # # Documentation # RDoc::Task.new do |rd| rd.rdoc_dir = "rdoc" rd.main = "README" rd.rdoc_files.include("README") rd.rdoc_files.include("./lib/**/*.rb") rd.options = %w(-a) end # # Packaging # spec = Gem::Specification.new do |s| s.name = "deprecated" s.version = Deprecated::VERSION s.author = "Erik Hollensbe" s.email = "erik@hollensbe.org" s.summary = "An easy way to handle deprecating and conditionally running deprecated code" s.has_rdoc = true s.files = Dir['Rakefile'] + Dir['lib/deprecated.rb'] + Dir['test/test_deprecated.rb'] s.test_file = "test/test_deprecated.rb" s.rubyforge_project = 'deprecated' s.license = "MIT" s.add_development_dependency "test-unit", ">= 0" end Gem::PackageTask.new(spec) do |p| p.need_tar_gz = true p.need_zip = true end ruby-deprecated-3.0.1/lib/000077500000000000000000000000001220152315600153355ustar00rootroot00000000000000ruby-deprecated-3.0.1/lib/deprecated.rb000066400000000000000000000123071220152315600177650ustar00rootroot00000000000000class DeprecatedError < StandardError; end # # Deprecated is a module to help you deprecate code BEFORE you remove it. Don't # surprise your users, deprecate them! # # Usage is simple: # # class Foo # include Deprecated # # def moo # "cow" # end # # deprecated :moo # # def sheep # "baaa" # end # # deprecated :sheep, "Sounds#baa" # # protected # # def bar # true # end # # deprecated :bar # end # # Foo.new.moo # warns that the call is deprecated # # Deprecated.set_action(:raise) # Foo.new.moo # raises with the same message # # Deprecated.set_action do |klass, sym, replacement| # email_boss( # "Someone tried to use #{klass}##{sym}! " + # "They should be using #{replacement} instead!" # ) # end # # Foo.new.sheep # do I really need to explain? # # Foo.new.bar # still protected! # # Let's do it live! # # class Bar # include Deprecated # # # sets it just for this class # deprecated_set_action do |klass, sym, replacement| # email_boss( # "Someone tried to use #{klass}##{sym}! " + # "They should be using #{replacement} instead!" # ) # end # # def cow # "moo" # end # # deprecate :cow # emails your boss when called! # end # # Please see Deprecated::Module#deprecated, Deprecated.set_action, and # Deprecated::Module#deprecated_set_action for more information. # module Deprecated VERSION = "3.0.1" def __deprecated_run_action__(sym, replacement) if self.class.instance_eval { @__deprecated_run_action__ } self.class.instance_eval { @__deprecated_run_action__ }.call(self.class, sym, replacement) else Deprecated.run_action(self.class, sym, replacement) end end def self.build_message(klass, sym, replacement) message = "#{klass}##{sym} is deprecated." if replacement message += " Please use #{replacement}." end return message end # # set_action takes 3 "canned" arguments or an arbitrary block. If you # provide the block, any canned argument is ignored. # # The canned arguments are: # # :warn:: display a warning # :raise:: raise a DeprecatedError (a kind of StandardError) with the warning. # :fail:: fail. die. kaput. it's over. # # Procs take three arguments: # # - The class of the method # - The method name itself, a symbol # - A replacement string which may be nil # def self.set_action(type=nil, &block) @action = if block block else case type when :warn proc { |*args| warn build_message(*args) } when :fail proc { |*args| fail build_message(*args) } when :raise proc { |*args| raise DeprecatedError, build_message(*args) } else raise ArgumentError, "you must provide a symbol or a block to set_action()." end end end # # Is called when an action needs to be run. Proably not in your best # interest to run this directly. # def self.run_action(klass, sym, replacement) raise "run_action has no associated hook" unless @action @action.call(klass, sym, replacement) end # # Returns the current action; this may be block or Proc. # def self.action @action end end module Deprecated module Module # # deprecated takes up to three arguments: # # - A symbol which is the name of the method you wish to deprecate # (required) # - A string or symbol which is the replacement method. If you provide # this, your users will be instructed to use that method instead. # - A symbol of :public, :private, or :protected which determines the # new scope of the method. If you do not provide one, it will be # searched for in the various collections, and scope will be chosen # that way. # def deprecated(sym, replacement=nil, scope=nil) unless sym.kind_of?(Symbol) raise ArgumentError, "deprecated() requires symbols for its first argument." end 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_action__(sym, replacement) retval = meth.bind(self).call(*args) dep_meth.bind(self) return retval end method(scope).call(sym) if scope return scope end # # Deprecated.set_action for class scope. See Deprecated.set_action. # def deprecated_set_action(&block) raise "You must provide a block" unless block @__deprecated_run_action__ = block end end def self.included(base) base.extend(Module) end end Deprecated.set_action(:warn) ruby-deprecated-3.0.1/test/000077500000000000000000000000001220152315600155465ustar00rootroot00000000000000ruby-deprecated-3.0.1/test/test_deprecated.rb000066400000000000000000000045551220152315600212430ustar00rootroot00000000000000#!/usr/bin/env ruby require 'rubygems' gem 'test-unit' require 'test/unit' $:.unshift 'lib' require 'deprecated.rb' $:.shift # this class is used to test the deprecate functionality class DummyClass include Deprecated def monkey return true end def monkey_bars return true end deprecated :monkey deprecated :monkey_bars, "FooClass#fart" protected def my_protected return true end deprecated :my_protected private def my_private return true end deprecated :my_private end class DummyClass2 include Deprecated deprecated_set_action do |klass, sym, replacement| raise DeprecatedError, "foo!" end def monkey return true end deprecated :monkey end # we want exceptions for testing here. Deprecated.set_action(:raise) class DeprecateTest < Test::Unit::TestCase def test_set_action assert_raises(DeprecatedError) { DummyClass.new.monkey } Deprecated.set_action { |klass, sym| raise DeprecatedError.new("#{klass}##{sym} is deprecated.") } assert_raises(DeprecatedError.new("DummyClass#monkey is deprecated.")) do DummyClass.new.monkey end Deprecated.set_action(:raise) assert_raises(DeprecatedError.new("DummyClass#monkey is deprecated.")) do DummyClass.new.monkey end # set to warn and make sure our return values are getting through. Deprecated.set_action(:warn) assert(DummyClass.new.monkey) Kernel.module_eval { def self.fail raise "failed" end } Deprecated.set_action(:fail) assert_raises("failed") { DummyClass.new.monkey } end def test_scope assert( DummyClass.public_instance_methods.include?(:monkey) || DummyClass.public_instance_methods.include?("monkey") ) assert( DummyClass.protected_instance_methods.include?(:my_protected) || DummyClass.protected_instance_methods.include?("my_protected") ) assert( DummyClass.private_instance_methods.include?(:my_private) || DummyClass.private_instance_methods.include?("my_private") ) end def test_scoped_actions assert_raises(DeprecatedError.new("foo!")) { DummyClass2.new.monkey } end def test_replacement Deprecated.set_action(:raise) assert_raises(DeprecatedError.new("DummyClass#monkey_bars is deprecated. Please use FooClass#fart.")) do DummyClass.new.monkey_bars end end end