deprecation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ #gemspec
4
+ #
5
+ gem 'activesupport'
6
+ gem 'rspec'
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'bundler/gem_tasks'
4
+
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+
15
+ task :console do
16
+ sh "irb -rubygems -I lib -r deprecation.rb"
17
+ end
@@ -0,0 +1,25 @@
1
+ # Provide a simple gemspec so you can easily use your enginex
2
+ # project in your rails apps through git.
3
+ require File.join(File.dirname(__FILE__), "lib/deprecation/version")
4
+ Gem::Specification.new do |s|
5
+ s.name = "deprecation"
6
+ s.version = Deprecation::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Chris Beer"]
9
+ s.email = ["chris@cbeer.info"]
10
+ s.summary = %q{Stand-alone deprecation library borrowed from ActiveSupport::Deprecation}
11
+ s.description = %q{Stand-alone deprecation library borrowed from ActiveSupport::Deprecation}
12
+ s.homepage = "http://github.com/cbeer/deprecation"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency "activesupport"
20
+
21
+ s.add_development_dependency("rake")
22
+ s.add_development_dependency("rspec")
23
+ s.add_development_dependency("bundler", ">= 1.0.14")
24
+ end
25
+
@@ -0,0 +1,34 @@
1
+ require 'deprecation/behaviors'
2
+ require 'deprecation/reporting'
3
+ require 'deprecation/method_wrappers'
4
+ require 'active_support/concern'
5
+
6
+ module Deprecation
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class << self
11
+ attr_accessor :deprecation_horizon
12
+
13
+ # Whether to print a backtrace along with the warning.
14
+ attr_accessor :debug
15
+
16
+ attr_accessor :silenced
17
+ end
18
+
19
+ # By default, warnings are not silenced and debugging is off.
20
+ self.silenced = false
21
+ self.debug = false
22
+ end
23
+
24
+ module ClassMethods
25
+ def deprecate *method_names
26
+ # Declare that a method has been deprecated.
27
+ # deprecate :foo
28
+ # deprecate :bar => 'message'
29
+ # deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!'
30
+ deprecate_methods(self, *method_names)
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,68 @@
1
+ require "active_support/notifications"
2
+ require "active_support/concern"
3
+
4
+ module Deprecation
5
+ module ClassMethods
6
+
7
+ # Returns the current behavior or if one isn't set, defaults to +:stderr+
8
+ def deprecation_behavior
9
+ @deprecation_behavior ||= [Deprecation.behaviors(self)[:stderr]]
10
+ end
11
+
12
+ # Sets the behavior to the specified value. Can be a single value, array, or
13
+ # an object that responds to +call+.
14
+ #
15
+ # Available behaviors:
16
+ #
17
+ # [+stderr+] Log all deprecation warnings to +$stderr+.
18
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
19
+ # [+notify] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
20
+ # [+silence+] Do nothing.
21
+ #
22
+ # Setting behaviors only affects deprecations that happen after boot time.
23
+ # Deprecation warnings raised by gems are not affected by this setting because
24
+ # they happen before Rails boots up.
25
+ #
26
+ # Deprecation.deprecation_behavior = :stderr
27
+ # Deprecation.deprecation_behavior = [:stderr, :log]
28
+ # Deprecation.deprecation_behavior = MyCustomHandler
29
+ # Deprecation.deprecation_behavior = proc { |message, callstack|
30
+ # # custom stuff
31
+ # }
32
+ def deprecation_behavior=(deprecation_behavior)
33
+ @deprecation_behavior = Array(deprecation_behavior).map { |b| Deprecation.behaviors(self)[b] || b }
34
+ end
35
+ end
36
+
37
+ def self.deprecations
38
+ @deprecations ||= []
39
+ end
40
+
41
+ def self.behaviors klass
42
+ # Default warning behaviors per Rails.env.
43
+ {
44
+ :stderr => Proc.new { |message, callstack|
45
+ $stderr.puts(message)
46
+ $stderr.puts callstack.join("\n ") if klass.debug
47
+ },
48
+ :log => Proc.new { |message, callstack|
49
+ logger =
50
+ if defined?(Rails) && Rails.logger
51
+ Rails.logger
52
+ else
53
+ require 'active_support/logger'
54
+ ActiveSupport::Logger.new($stderr)
55
+ end
56
+ logger.warn message
57
+ logger.debug callstack.join("\n ") if klass.debug
58
+ },
59
+ :notify => Proc.new { |message, callstack|
60
+ ActiveSupport::Notifications.instrument("deprecation.#{klass.to_s}",
61
+ :message => message, :callstack => callstack)
62
+ },
63
+ :raise => Proc.new { |message, callstack| raise message },
64
+ :silence => Proc.new { |message, callstack| },
65
+ :test => Proc.new { |message, callstack| self.deprecations << message }
66
+ }
67
+ end
68
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_support/core_ext/module/aliasing'
2
+ require 'active_support/core_ext/array/extract_options'
3
+
4
+ module Deprecation
5
+ module ClassMethods
6
+ # Declare that a method has been deprecated.
7
+ def deprecate_methods(target_module, *method_names)
8
+ options = method_names.extract_options!
9
+ method_names += options.keys
10
+
11
+ method_names.each do |method_name|
12
+ target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
13
+ target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
14
+ def #{target}_with_deprecation#{punctuation}(*args, &block)
15
+ self.class.warn(
16
+ self.class.deprecated_method_warning(
17
+ :#{method_name},
18
+ #{options[method_name].inspect}),
19
+ caller
20
+ )
21
+ send(:#{target}_without_deprecation#{punctuation}, *args, &block)
22
+ end
23
+ end_eval
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,81 @@
1
+ module Deprecation
2
+ module ClassMethods
3
+ # Outputs a deprecation warning to the output configured by <tt>ActiveSupport::Deprecation.behavior</tt>
4
+ #
5
+ # Deprecation.warn("something broke!")
6
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
7
+ def warn(message = nil, callstack = caller)
8
+ return if silenced
9
+ deprecation_message(callstack, message).tap do |m|
10
+ deprecation_behavior.each { |b| b.call(m, callstack) }
11
+ end
12
+ end
13
+
14
+ # Silence deprecation warnings within the block.
15
+ def silence
16
+ old_silenced, @silenced = @silenced, true
17
+ yield
18
+ ensure
19
+ @silenced = old_silenced
20
+ end
21
+
22
+ def collect
23
+ old_behavior = self.class.deprecation_behavior
24
+ deprecations = []
25
+ self.deprecation_behavior = Proc.new do |message, callstack|
26
+ deprecations << message
27
+ end
28
+ result = yield
29
+ [result, deprecations]
30
+ ensure
31
+ self.class.deprecation_behavior = old_behavior
32
+ end
33
+
34
+ def deprecated_method_warning(method_name, options = nil)
35
+
36
+ options ||= {}
37
+
38
+ if options.is_a? String or options.is_a? Symbol
39
+ message = options
40
+ options = {}
41
+ end
42
+
43
+ warning = "#{method_name} is deprecated and will be removed from #{options[:deprecation_horizon] || deprecation_horizon}"
44
+ case message
45
+ when Symbol then "#{warning} (use #{message} instead)"
46
+ when String then "#{warning} (#{message})"
47
+ else warning
48
+ end
49
+ end
50
+
51
+ private
52
+ def deprecation_message(callstack, message = nil)
53
+ message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
54
+ message += '.' unless message =~ /\.$/
55
+ "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}"
56
+ end
57
+
58
+ def deprecation_caller_message(callstack)
59
+ file, line, method = extract_callstack(callstack)
60
+ if file
61
+ if line && method
62
+ "(called from #{method} at #{file}:#{line})"
63
+ else
64
+ "(called from #{file}:#{line})"
65
+ end
66
+ end
67
+ end
68
+
69
+ def extract_callstack(callstack)
70
+ deprecation_gem_root = File.expand_path("../..", __FILE__) + "/"
71
+ offending_line = callstack.find { |line| !line.start_with?(deprecation_gem_root) } || callstack.first
72
+ if offending_line
73
+ if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
74
+ md.captures
75
+ else
76
+ offending_line
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module Deprecation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Deprecation do
4
+ class DeprecationTest
5
+ include Deprecation
6
+ self.deprecation_behavior = :raise
7
+
8
+ self.deprecation_horizon = 'release 0.1'
9
+
10
+
11
+ def a
12
+ 1
13
+ end
14
+
15
+ deprecate :a
16
+
17
+ def b
18
+
19
+ end
20
+
21
+ def c
22
+
23
+ end
24
+
25
+ def d
26
+
27
+ end
28
+
29
+ deprecate :c, :d
30
+
31
+ def e
32
+
33
+ end
34
+ deprecate :e => { :deprecation_horizon => 'asdf 1.4' }
35
+ end
36
+ subject { DeprecationTest.new}
37
+
38
+ describe "a" do
39
+ it "should be deprecated" do
40
+ expect { subject.a }.to raise_error /a is deprecated/
41
+ end
42
+ end
43
+
44
+ describe "b" do
45
+ it "should not be deprecated" do
46
+ expect { subject.b }.not_to raise_error /b is deprecated/
47
+ end
48
+ end
49
+
50
+ describe "c,d" do
51
+ it "should be deprecated" do
52
+ expect { subject.c }.to raise_error /c is deprecated/
53
+ expect { subject.d }.to raise_error /d is deprecated/
54
+ end
55
+ end
56
+
57
+ describe "e" do
58
+ it "should be deprecated in asdf 1.4" do
59
+ expect { subject.e }.to raise_error /e is deprecated and will be removed from asdf 1.4/
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ require 'deprecation'
2
+
3
+ require 'rspec/expectations'
4
+
5
+ RSpec::Matchers.define :be_deprecated do |expected|
6
+ match do |actual|
7
+ actual.call
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deprecation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Beer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.14
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.14
78
+ description: Stand-alone deprecation library borrowed from ActiveSupport::Deprecation
79
+ email:
80
+ - chris@cbeer.info
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - Gemfile
86
+ - Rakefile
87
+ - deprecation.gemspec
88
+ - lib/deprecation.rb
89
+ - lib/deprecation/behaviors.rb
90
+ - lib/deprecation/method_wrappers.rb
91
+ - lib/deprecation/reporting.rb
92
+ - lib/deprecation/version.rb
93
+ - spec/deprecation_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://github.com/cbeer/deprecation
96
+ licenses: []
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
108
+ - 0
109
+ hash: 2510818728159110120
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Stand-alone deprecation library borrowed from ActiveSupport::Deprecation
122
+ test_files:
123
+ - spec/deprecation_spec.rb
124
+ - spec/spec_helper.rb