deprecated 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/deprecated.rb +35 -64
  2. data/test/deprecated.rb +11 -20
  3. metadata +2 -2
data/lib/deprecated.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # Deprecate - handle deprecating and executing deprecated code
3
3
  #
4
- # Version:: 1.0.0
4
+ # Version:: 2.0.0
5
5
  # Author:: Erik Hollensbe
6
6
  # License:: BSD
7
7
  # Copyright:: Copyright (c) 2006 Erik Hollensbe
@@ -17,47 +17,26 @@
17
17
  #
18
18
  # class Foo
19
19
  # private
20
- # # rename the original function and make it private
21
- # def _monkey
22
- # do stuff...
23
- # end
24
- # public
20
+ # # rename the original function and make it private
21
+ # def monkey
22
+ # do stuff...
23
+ # end
25
24
  # # deprecate the function, this will create a 'monkey' method
26
25
  # # that will call the deprecate warnings
27
- # deprecate :monkey, :_monkey
26
+ # deprecate :monkey, :private
28
27
  # end
29
28
  #
30
29
  # The 'deprecate' call is injected into the 'Module' class at
31
30
  # require-time. This allows all classes that are newly-created to
32
- # access the 'deprecate' functionality.
33
- #
34
- # The call itself takes a list of symbols. Each pair is first the name
35
- # of the original function, the second the name of the current,
36
- # renamed function. Both are represented as symbols.
37
- #
38
- # Ex:
39
- #
40
- # # foo subroutine is generated to call _foo, and notify the user
41
- # # when called.
42
- # deprecate :foo, :_foo
43
- #
44
- # You can define as many symbols as you like on one line as
45
- # deprecated, but for obvious reasons the length of the list must be
46
- # even. In the case that it isn't, the 'deprecate' call will throw an
47
- # exception of type 'DeprecatedError'.
48
- #
49
- # If you prefer to have more sugar in your syntax, the call
50
- # understands arrays:
51
- #
52
- # deprecate [:foo, :_foo], [:bar, :_bar]
53
- #
54
- # And of course, you can make the call multiple times.
31
+ # access the 'deprecate' functionality. The deprecate definition must
32
+ # follow the method definition. You may only define one deprecated
33
+ # function per call.
55
34
  #
56
35
  # Methods deprecated default to 'public'. This is due to a limitation
57
36
  # in how Ruby handles permission definition. If you're aware of a
58
37
  # workaround to this problem, please let me know.
59
38
  #
60
- # You can however change this by providing an optional leading
39
+ # You can however change this by providing an optional trailing
61
40
  # parameter to the 'deprecate' call:
62
41
  #
63
42
  # * :public - set the created method to be public
@@ -102,8 +81,6 @@
102
81
 
103
82
  module Deprecate
104
83
 
105
- CALLER_REGEX = /\`([^\']+)/
106
-
107
84
  #
108
85
  # set_action defines the action that will be taken when code marked
109
86
  # deprecated is encountered. There are several stock options:
@@ -189,38 +166,32 @@ end
189
166
 
190
167
  Module.send(:define_method, :deprecate,
191
168
  proc do |*args|
192
- args.flatten!
193
- if args.length
194
-
195
- permission = :public
196
-
197
- if args.length % 2 != 0
198
- permission = args.shift
199
- end
200
-
201
- while args.length > 0
202
- syms = args.slice! 0, 2
203
-
204
- if syms.include? nil
205
- raise DeprecatedError.new("Invalid number of arguments passed to 'deprecated' function")
206
- end
207
-
208
- define_method(syms[0]) do |*sendparams|
209
- Deprecate.action.call(self.class.to_s + '#' + syms[0].to_s)
210
- self.send(syms[1], *sendparams)
211
- end
212
-
213
- case permission
214
- when :public
215
- public(syms[0])
216
- when :protected
217
- protected(syms[0])
218
- when :private
219
- private(syms[0])
220
- end
221
-
222
- end
169
+ sym = args.shift
170
+ protection = args.shift || :public
171
+
172
+ unless sym
173
+ raise DeprecatedError.new("Invalid number of arguments passed to 'deprecated' function")
174
+ end
175
+
176
+ old_method = self.instance_method(sym)
177
+
178
+ define_method(sym) do |*sendparams|
179
+ Deprecate.action.call(self.class.to_s + '#' + sym.to_s)
180
+ new_method = self.class.instance_method(sym)
181
+ retval = old_method.bind(self).call(*sendparams)
182
+ new_method.bind(self)
183
+ return retval
184
+ end
185
+
186
+ case protection
187
+ when :public
188
+ public(sym)
189
+ when :private
190
+ private(sym)
191
+ when :protected
192
+ protected(sym)
223
193
  end
194
+
224
195
  end)
225
196
 
226
197
  Deprecate.set_action(:warn)
data/test/deprecated.rb CHANGED
@@ -1,42 +1,33 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'lib/deprecate.rb'
3
+ require 'lib/deprecated.rb'
4
4
  require 'test/unit'
5
5
 
6
6
  # this class is used to test the deprecate functionality
7
7
  class DummyClass
8
- deprecate :monkey, :_monkey
9
-
10
- def _monkey
8
+ def monkey
11
9
  return true
12
10
  end
13
11
 
12
+ deprecate :monkey
14
13
  end
15
14
 
16
15
  # we want exceptions for testing here.
17
16
  Deprecate.set_action(:throw)
18
17
 
19
18
  class DeprecateTest < Test::Unit::TestCase
20
- def set_action_builder
21
- d = DummyClass.new
22
- test = true
23
- begin
24
- d.monkey
25
- test = false
26
- rescue DeprecatedError => e
27
- test = e.message == "DummyClass#monkey is deprecated."
28
- end
29
-
30
- return test
31
- end
32
-
33
- # this inadvertently tests the deprecate functionality as well...
34
19
  def test_set_action
35
20
 
36
- assert(set_action_builder, "Deprecate.set_action message/:throw test")
21
+ assert_raise(DeprecatedError) { raise StandardError.new unless DummyClass.new.monkey }
37
22
 
38
23
  Deprecate.set_action(proc { |msg| raise DeprecatedError.new("#{msg} is deprecated.") })
39
24
 
40
- assert(set_action_builder, "Deprecate.set_action custom proc test")
25
+ assert_raise(DeprecatedError) { raise StandardError.new unless DummyClass.new.monkey }
26
+
27
+
28
+ # set to warn and make sure our return values are getting through.
29
+ Deprecate.set_action(:warn)
30
+
31
+ assert_nothing_raised(DeprecatedError) { raise StandardError.new unless DummyClass.new.monkey }
41
32
  end
42
33
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: deprecated
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2006-02-15 00:00:00 -08:00
6
+ version: 2.0.0
7
+ date: 2006-05-17 00:00:00 -07:00
8
8
  summary: An easy way to handle deprecating and conditionally running deprecated code
9
9
  require_paths:
10
10
  - lib