dont 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/README.md +15 -14
- data/dont.gemspec +1 -0
- data/lib/dont.rb +67 -50
- data/lib/dont/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24d5eabf7ad968cb3d575076d120bf5ccc4808ae
|
4
|
+
data.tar.gz: f7dc01339e3629f8cad5ed1cfc3297afd4c0f0b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c2a8cf3e08e5aed86eff61fb3af1d1b612c9a56c9ca46c8655abe7ceb16904734b2f8c0e9a97cf77e6a090a752a6b303f2af693a359e6a42a8f15a6ecfd67b6
|
7
|
+
data.tar.gz: ff10f1eb7399ee41889c758e8d47c3ee87b410fafabd9a894e16c090fef129c7eadd37c04a4e83620c38174084fc064e2155093b879991a72dee28c644a7b9ed
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Dont
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/datacamp/dont.svg?branch=master)](https://travis-ci.org/datacamp/dont) [![Gem Version](https://badge.fury.io/rb/dont.svg)](https://badge.fury.io/rb/dont)
|
4
|
+
|
3
5
|
Easily deprecate methods.
|
4
6
|
|
5
7
|
## Installation
|
@@ -21,12 +23,10 @@ Or install it yourself as:
|
|
21
23
|
## Usage
|
22
24
|
|
23
25
|
```ruby
|
24
|
-
# Register
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
logger.warn("Don't use `#{class_name}##{method}`. It's deprecated.")
|
29
|
-
})
|
26
|
+
# Register a deprecation handler.
|
27
|
+
# Anything that responds to `.call(deprecation)` will work.
|
28
|
+
LOGGER = Logger.new
|
29
|
+
Dont.register_handler(:logger, ->(deprecation) { LOGGER.warn(deprecation.message) })
|
30
30
|
class Shouter
|
31
31
|
include Dont.new(:logger)
|
32
32
|
|
@@ -37,25 +37,26 @@ class Shouter
|
|
37
37
|
def scream(msg)
|
38
38
|
shout(msg)
|
39
39
|
end
|
40
|
-
dont_use :scream
|
40
|
+
dont_use :scream, use: :shout
|
41
41
|
end
|
42
42
|
|
43
|
-
#
|
44
|
-
#
|
43
|
+
# Logs "DEPRECATED: Don't use Shouter#scream. It's deprecated in favor of
|
44
|
+
# shout.", before executing the method.
|
45
45
|
Shouter.new.scream("hello")
|
46
|
-
# => HELLO
|
47
46
|
|
48
|
-
|
47
|
+
|
48
|
+
# The :exception deprecation handler is provided by default.
|
49
|
+
# It raises an exception whenever the method is called, which is handy in
|
50
|
+
# test or development mode.
|
49
51
|
class Person
|
50
52
|
include Dont.new(:exception)
|
51
53
|
|
52
54
|
attr_accessor :firstname
|
53
55
|
attr_accessor :first_name
|
54
56
|
|
55
|
-
dont_use :firstname
|
57
|
+
dont_use :firstname, use: :first_name
|
56
58
|
end
|
57
|
-
|
58
|
-
Person.new.firstname # => Dont::DeprecationError
|
59
|
+
Person.new.firstname # => fails with Dont::DeprecationError
|
59
60
|
```
|
60
61
|
|
61
62
|
## Development
|
data/dont.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
24
|
spec.add_dependency "dry-container", ">= 0.6"
|
25
|
+
spec.add_dependency "dry-initializer", ">= 0.10.0"
|
25
26
|
|
26
27
|
spec.add_development_dependency "bundler", "~> 1.13"
|
27
28
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/dont.rb
CHANGED
@@ -1,27 +1,16 @@
|
|
1
1
|
require "dont/version"
|
2
2
|
require "dry-container"
|
3
|
+
require "dry-initializer"
|
3
4
|
|
4
5
|
# Defines a `dont_use` method which can be used to deprecate methods. Whenever
|
5
6
|
# the deprecated method is used the specified handler will get triggered.
|
6
7
|
#
|
7
8
|
# @example
|
8
9
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# attr_accessor :first_name
|
14
|
-
#
|
15
|
-
# dont_use :firstname
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# Person.new.firstname # => Dont::DeprecationError
|
19
|
-
#
|
20
|
-
# # Register custom handlers
|
21
|
-
# Dont.register_handler(:logger, ->(object, method) {
|
22
|
-
# class_name = object.class.name
|
23
|
-
# logger.warn("Don't use `#{class_name}##{method}`. It's deprecated.")
|
24
|
-
# })
|
10
|
+
# # Register a deprecation handler.
|
11
|
+
# # Anything that responds to `.call(deprecation)` will work.
|
12
|
+
# LOGGER = Logger.new
|
13
|
+
# Dont.register_handler(:logger, ->(deprecation) { LOGGER.warn(deprecation.message) })
|
25
14
|
# class Shouter
|
26
15
|
# include Dont.new(:logger)
|
27
16
|
#
|
@@ -32,36 +21,35 @@ require "dry-container"
|
|
32
21
|
# def scream(msg)
|
33
22
|
# shout(msg)
|
34
23
|
# end
|
35
|
-
# dont_use :scream
|
24
|
+
# dont_use :scream, use: :shout
|
36
25
|
# end
|
37
26
|
#
|
38
|
-
# #
|
39
|
-
# #
|
27
|
+
# # Logs "DEPRECATED: Don't use Shouter#scream. It's deprecated in favor of
|
28
|
+
# # shout.", before executing the method.
|
40
29
|
# Shouter.new.scream("hello")
|
41
|
-
# # => HELLO
|
42
30
|
#
|
43
31
|
#
|
32
|
+
# # The :exception deprecation handler is provided by default.
|
33
|
+
# # It raises an exception whenever the method is called, which is handy in
|
34
|
+
# # test or development mode.
|
35
|
+
# class Person
|
36
|
+
# include Dont.new(:exception)
|
37
|
+
#
|
38
|
+
# attr_accessor :firstname
|
39
|
+
# attr_accessor :first_name
|
40
|
+
#
|
41
|
+
# dont_use :firstname, use: :first_name
|
42
|
+
# end
|
43
|
+
# Person.new.firstname # => fails with Dont::DeprecationError
|
44
|
+
#
|
44
45
|
class Dont < Module
|
45
46
|
Error = Class.new(StandardError)
|
46
47
|
DeprecationError = Class.new(Error)
|
47
48
|
MissingHandlerError = Class.new(Error)
|
48
|
-
WrongArityError = Class.new(Error)
|
49
|
-
|
50
|
-
HANDLERS = {
|
51
|
-
exception: -> (object, method) {
|
52
|
-
class_name = object.class.name
|
53
|
-
fail DeprecationError, "Don't use `#{class_name}##{method}`. It's deprecated."
|
54
|
-
},
|
55
|
-
airbrake: -> (object, method) {
|
56
|
-
class_name = object.class.name
|
57
|
-
err = DeprecationError.new("Don't use `#{class_name}##{method}`. It's deprecated.")
|
58
|
-
Airbrake.notify(err)
|
59
|
-
},
|
60
|
-
}
|
61
49
|
|
62
50
|
def initialize(key)
|
63
51
|
handler = Dont.fetch_handler(key)
|
64
|
-
@implementation = ->(
|
52
|
+
@implementation = ->(old_method, use: nil) {
|
65
53
|
# The moment `dont_use` is called in ActiveRecord is before AR defines
|
66
54
|
# the attributes in the model. So you get an error when calling
|
67
55
|
# instance_method with the regular implementation.
|
@@ -70,19 +58,25 @@ class Dont < Module
|
|
70
58
|
# adapts the code.
|
71
59
|
is_ar_attribute = defined?(ActiveRecord::Base) &&
|
72
60
|
ancestors.include?(ActiveRecord::Base) &&
|
73
|
-
!method_defined?(
|
61
|
+
!method_defined?(old_method)
|
74
62
|
|
75
|
-
|
76
|
-
define_method(
|
77
|
-
|
63
|
+
original = instance_method(old_method) unless is_ar_attribute
|
64
|
+
define_method(old_method) do |*args|
|
65
|
+
deprecation = Deprecation.new(
|
66
|
+
subject: self,
|
67
|
+
new_method: use,
|
68
|
+
old_method: old_method,
|
69
|
+
)
|
70
|
+
handler.call(deprecation)
|
78
71
|
if is_ar_attribute
|
79
|
-
if
|
80
|
-
|
72
|
+
if old_method =~ /=\z/
|
73
|
+
attr = old_method.to_s.sub(/=\z/, '')
|
74
|
+
public_send(:[]=, attr, *args)
|
81
75
|
else
|
82
|
-
self[
|
76
|
+
self[old_method.to_s.sub(/\?\z/, '')]
|
83
77
|
end
|
84
78
|
else
|
85
|
-
|
79
|
+
original.bind(self).call(*args)
|
86
80
|
end
|
87
81
|
end
|
88
82
|
}
|
@@ -95,23 +89,46 @@ class Dont < Module
|
|
95
89
|
end
|
96
90
|
|
97
91
|
class << self
|
98
|
-
def handlers
|
99
|
-
@handlers ||= Dry::Container.new
|
100
|
-
end
|
101
|
-
|
102
92
|
def register_handler(key, callable)
|
103
93
|
handlers.register(key, callable)
|
104
94
|
end
|
105
95
|
|
106
96
|
def fetch_handler(key)
|
107
|
-
|
97
|
+
handlers.resolve(key)
|
108
98
|
rescue Dry::Container::Error => e
|
109
99
|
fail MissingHandlerError, e.message
|
110
100
|
end
|
101
|
+
|
102
|
+
protected
|
103
|
+
|
104
|
+
def handlers
|
105
|
+
@handlers ||= Dry::Container.new
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Contains info about the deprecated method being called
|
110
|
+
class Deprecation
|
111
|
+
extend Dry::Initializer::Mixin
|
112
|
+
|
113
|
+
option :subject
|
114
|
+
option :new_method
|
115
|
+
option :old_method, optional: true
|
116
|
+
|
117
|
+
# A message saying that the old_method is deprecated. It also mentions the
|
118
|
+
# new_method if provided.
|
119
|
+
#
|
120
|
+
# @return [String]
|
121
|
+
def message
|
122
|
+
klass = subject.class.name
|
123
|
+
if new_method && !new_method.empty?
|
124
|
+
"DEPRECATED: Don't use #{klass}##{old_method}. It's deprecated in favor of #{new_method}."
|
125
|
+
else
|
126
|
+
"DEPRECATED: Don't use #{klass}##{old_method}. It's deprecated."
|
127
|
+
end
|
128
|
+
end
|
111
129
|
end
|
112
130
|
|
113
|
-
register_handler(:exception, -> (
|
114
|
-
|
115
|
-
fail Dont::DeprecationError, "Don't use `#{class_name}##{method}`. It's deprecated."
|
131
|
+
register_handler(:exception, -> (deprecation) {
|
132
|
+
fail Dont::DeprecationError, deprecation.message
|
116
133
|
})
|
117
134
|
end
|
data/lib/dont/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dont
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maarten Claes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-container
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-initializer
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.10.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.10.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|