deprecations 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78ed3cd91fd99c383cfcf80cf73f9e5757f0f007
4
- data.tar.gz: bf79ec77bdff29dc0222a4b385efa37d7bc27fd5
3
+ metadata.gz: 8f3f137ca9a5beb63eb3bdd08488fff97528ca7b
4
+ data.tar.gz: d7ea7405ba490ee635a0a299acb96af174ac4f10
5
5
  SHA512:
6
- metadata.gz: 600ef0c82127f3df1111751cad2a13fdd0aa4342051fa629f122f498dac8ee469182f1417474df5ff1b60db178276858e707a8becfb9f62449d75e05126f41fb
7
- data.tar.gz: 17760c77380a0379c7e8cc523a1869bcf77164156fe4749d63ba3ed6afa674f1f0cf9bbed7f63f394e35b956e76dba51786f47d0e6d8fe84bdd4b5ac7aec51d0
6
+ metadata.gz: 80b8a7f300eac8799d2b813e0eee5481da2dc06716893a851585c876c21f32c68952f38f30e1233e440decc2249a48163a023b6a3cf74dd9056bbb60058ee25d
7
+ data.tar.gz: ca23236eda75c3e37649cd464bb9fe22f4944fbd260a318c9980ff6acf63656b1641342f1e0e60baa6fa4856b4f99cfe56f661e9e51348f7797a6e7a036a5b09
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### deprecations 1.0.4
2
+ - less code, better algorithm to overload deprecated methods
3
+ - no more private «shadow methods»
4
+
1
5
  ### deprecations 1.0.2
2
6
  - support for late bound class names (like in `Foo = Class.new`)
3
7
 
@@ -10,7 +10,7 @@ module Deprecations
10
10
  @@cfg
11
11
  end
12
12
 
13
- BEHAVIORS = [:warn, :raise, :silence].freeze
13
+ BEHAVIORS = [:warn, :raise, :silence]
14
14
 
15
15
  private
16
16
 
@@ -25,7 +25,7 @@ module Deprecations
25
25
  BEHAVIORS.include?(how) and return @behavior = how
26
26
  ::Kernel.raise(
27
27
  ::ArgumentError, "invalid parameter `#{how}` - have to be #{BEHAVIORS.map(&:inspect).join(' | ')}"
28
- )
28
+ )
29
29
  end
30
30
 
31
31
  end
@@ -1,7 +1,7 @@
1
-
2
1
  module Deprecations
3
2
  class << self
4
3
  private
4
+
5
5
  def infect(mod)
6
6
  mod.extend(ClassMethods)
7
7
  mod.send(:include, InstanceMethods)
@@ -9,95 +9,58 @@ module Deprecations
9
9
 
10
10
  module Helper
11
11
  private
12
-
13
- def __method_scope(method_name)
14
- private_method_defined?(method_name) and return :private
15
- protected_method_defined?(method_name) and return :protected
16
- public_method_defined?(method_name) and return :public
17
- nil
12
+ def __method(method_name)
13
+ instance_method(method_name) rescue nil
18
14
  end
19
15
 
20
- def __define_deprecated(name, decorated_name, scope, alternative, outdated)
21
- alias_name = __private_alias(name, decorated_name)
22
- send(scope, name)
23
- define_method(name) do |*a, &b|
24
- Deprecations.call(decorated_name, alternative, outdated)
25
- send(alias_name, *a, &b)
16
+ def __method_deprecated!(method, alternative, outdated)
17
+ define_method(method.name) do |*a, &b|
18
+ decorated = Class === self ? "#{self}." : "#{self.class}#"
19
+ Deprecations.call(
20
+ "#{decorated}#{__method__}",
21
+ alternative.nil? ? nil : "#{decorated}#{alternative.name}",
22
+ outdated
23
+ )
24
+ method.bind(self).call(*a, &b)
26
25
  end
27
- name
28
26
  end
29
27
 
30
- def __private_alias(name, decorated_name)
31
- alias_name = "deprecated_#{name}"
32
- private_method_defined?(alias_name) and raise(ScriptError, "method is already deprecated - #{decorated_name}")
33
- alias_method(alias_name, name)
34
- private(alias_name)
35
- alias_name
28
+ def __method_not_found!(method_name)
29
+ raise(NameError, "undefined method `#{method_name}` for class `#{self}`")
36
30
  end
31
+
37
32
  end
38
33
 
39
34
  module ClassMethods
40
- include Helper
41
35
  private
36
+ include Helper
42
37
 
43
- def deprecated(method_name, alternative_method_name = nil, outdated = nil)
44
- method_scope = __method_scope(method_name) or __not_found!(method_name)
45
- __define_deprecated(
46
- method_name,
47
- ->{"#{self.inspect[8..-2]}.#{method_name}"},
48
- method_scope,
49
- __decorated(alternative_method_name),
50
- outdated
51
- )
52
- ensure
53
- $@ and $@.delete_if{ |s| s.index(__FILE__) }
54
- end
55
-
56
- def __decorated(name)
57
- name or return nil
58
- __method_scope(name) and return ->{"#{self.inspect[8..-2]}.#{name}"}
59
- Symbol === name and __not_found!(name)
60
- name
61
- end
62
-
63
- def __not_found!(name)
64
- raise(NameError, "undefined method `#{name}` for class `#{self.inspect[8..-2]}`")
38
+ def deprecated(method_name, alternative = nil, outdated = nil)
39
+ m = __method(method_name) or __method_not_found!(method_name)
40
+ a = alternative.nil? ? nil : (__method(alternative) or __method_not_found!(alternative))
41
+ __method_deprecated!(m, a, outdated)
65
42
  end
66
43
  end
67
44
 
68
45
  module InstanceMethods
69
- include Helper
70
46
  private
47
+ include Helper
71
48
 
72
- def deprecated!(alternative = nil, outdated = nil)
73
- singleton_class.send(
74
- :__define_deprecated,
75
- :new,
76
- ->{self},
77
- :public,
78
- alternative ? "#{alternative}" : nil,
79
- outdated
80
- )
81
- end
82
-
83
- def deprecated(method_name, alternative_method_name = nil, outdated = nil)
84
- method_scope = __method_scope(method_name) and return __define_deprecated(
85
- method_name,
86
- ->{"#{self}##{method_name}"},
87
- method_scope,
88
- __decorated(alternative_method_name),
89
- outdated
90
- )
91
- singleton_class.send(:deprecated, method_name, alternative_method_name, outdated)
92
- ensure
93
- $@ and $@.delete_if{ |s| s.index(__FILE__) }
49
+ def deprecated(method_name, alternative = nil, outdated = nil)
50
+ m = __method(method_name) or return singleton_class.send(:deprecated, method_name, alternative, outdated)
51
+ a = alternative.nil? ? nil : (__method(alternative) or __method_not_found!(alternative))
52
+ __method_deprecated!(m, a, outdated)
94
53
  end
95
54
 
96
- def __decorated(name)
97
- name or return nil
98
- __method_scope(name) ? ->{"#{self}##{name}"} : singleton_class.send(:__decorated, name)
55
+ def deprecated!(alternative = nil, outdated = nil)
56
+ m = method(:new)
57
+ define_singleton_method(:new) do |*a, &b|
58
+ Deprecations.call(self, alternative, outdated)
59
+ m.call(*a, &b)
60
+ end
99
61
  end
100
62
  end
63
+
101
64
  end
102
65
 
103
66
  infect(Module)
@@ -1,3 +1,3 @@
1
1
  module Deprecations
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.4'
3
3
  end
data/lib/deprecations.rb CHANGED
@@ -21,8 +21,8 @@ module Deprecations
21
21
  private
22
22
 
23
23
  def throw!(subject, alternative)
24
- msg = "`#{value(subject)}` is deprecated"
25
- alternative and msg << " - use #{value(alternative)} instead"
24
+ msg = "`#{subject}` is deprecated"
25
+ alternative and msg << " - use #{alternative} instead"
26
26
  ex = DeprecationError.new(msg)
27
27
  ex.set_backtrace(caller(3))
28
28
  raise(ex)
@@ -30,15 +30,11 @@ module Deprecations
30
30
 
31
31
  def warn(subject, alternative, outdated)
32
32
  location = ::Kernel.caller_locations(3,1).last and location = "#{location.path}:#{location.lineno}: "
33
- msg = "#{location}[DEPRECATION] `#{value(subject)}` is deprecated"
34
- msg << ((outdated = value(outdated)) ? " and will be outdated #{outdated}." : '.')
35
- alternative = value(alternative) and msg << " Please use `#{alternative}` instead."
33
+ msg = "#{location}[DEPRECATION] `#{subject}` is deprecated"
34
+ msg << (outdated ? " and will be outdated #{outdated}." : '.')
35
+ alternative and msg << " Please use `#{alternative}` instead."
36
36
  ::Kernel.warn(msg)
37
37
  end
38
-
39
- def value(arg)
40
- defined?(arg.call) ? arg.call : arg
41
- end
42
38
 
43
39
  end
44
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deprecations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-09 00:00:00.000000000 Z
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -136,9 +136,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: 1.3.6
137
137
  requirements: []
138
138
  rubyforge_project: deprecations
139
- rubygems_version: 2.2.2
139
+ rubygems_version: 2.4.5
140
140
  signing_key:
141
141
  specification_version: 4
142
142
  summary: Deprecation support for your project.
143
143
  test_files: []
144
- has_rdoc: false