deprecator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in deprecator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vasily Fedoseyev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Deprecator
2
+
3
+ Yet another library for dealing with code deprecation in ruby gracefully.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'deprecator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install deprecator
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+
23
+
24
+
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ t.libs.push 'spec'
8
+ end
9
+
10
+
11
+ # require 'rake/extensiontask'
12
+ # Rake::ExtensionTask.new('deprecator')
13
+ # task :test => :compile
14
+
15
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deprecator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "deprecator"
8
+ spec.version = Deprecator::VERSION
9
+ spec.authors = ["Vasily Fedoseyev"]
10
+ spec.email = ["vasilyfedoseyev@gmail.com"]
11
+ spec.description = %q{Yet another library for dealing with code deprecation in ruby}
12
+ spec.summary = %q{Adds some beauty and structure to code deprecation, see readme}
13
+ spec.homepage = "http://github.com/Vasfed/deprecator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.1'
22
+ spec.add_dependency "is_a", "~> 0.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,107 @@
1
+ require "deprecator/version"
2
+ require "deprecator/strategy"
3
+ require 'is_a'
4
+
5
+ module Deprecator
6
+
7
+ class DeprecatedClass
8
+ def self.inherited cls
9
+ cls.extend(Deprecated)
10
+ end
11
+ end
12
+
13
+ module Deprecated
14
+ def self.included cls
15
+ cls.extend self
16
+ end
17
+
18
+ def self.extended cls
19
+ ::Deprecator.strategy.class_found(cls, caller_line)
20
+ cls.send :include, InstanceMethods
21
+ end
22
+
23
+ module InstanceMethods
24
+ # SMELL: initialize in module is no good, but in this case may help for some cases
25
+ def initialize *args
26
+ ::Deprecator.strategy.object_found(self, caller_line)
27
+ end
28
+ end
29
+ Class = DeprecatedClass
30
+ end
31
+
32
+
33
+ module ClassClassMethods
34
+ def deprecated reason=nil, *args
35
+ ::Deprecator.strategy.class_found(self, caller_line, reason, args)
36
+ end
37
+
38
+ def __on_next_method &blk
39
+ @method_added_stack ||= []
40
+ if self.respond_to?(:method_added) || @method_added_stack.size > 0
41
+ m = self.method(:method_added)
42
+ @method_added_stack.push(m)
43
+ end
44
+
45
+ define_singleton_method(:method_added, ->(name){
46
+ return if name == :method_added
47
+ super(name)
48
+ old = @method_added_stack.pop
49
+ if old
50
+ define_singleton_method(:method_added, old)
51
+ else
52
+ class <<self; remove_method(:method_added); end
53
+ end
54
+ blk.call(name)
55
+ })
56
+ end
57
+
58
+ def deprecated_method name=nil, reason=nil, &blk
59
+ where = caller_line
60
+ unless reason
61
+ if !name || name.is_a?(String)
62
+ reason = name
63
+ name = nil
64
+ # => take next defined method
65
+ __on_next_method {|name|
66
+ Deprecator.strategy.method_found(self, name, reason, where)
67
+ }
68
+ return
69
+ end
70
+ end
71
+
72
+ name = [name] unless name.is_a?(Array)
73
+ name.each{|n|
74
+ Deprecator.strategy.method_found(self, n, reason, where)
75
+ }
76
+ end
77
+ end
78
+
79
+ DefaultStrategy = Strategy::Warning
80
+
81
+ class UnknownStrategy < ArgumentError; end
82
+ class NotImplemented < RuntimeError; end
83
+
84
+ @@strategy = nil
85
+ def self.strategy
86
+ @@strategy ||= DefaultStrategy.new
87
+ end
88
+
89
+ def self.strategy= s
90
+ case s
91
+ when Class then return(@@strategy = s.new)
92
+ when Symbol then
93
+ capitalized = s.capitalize
94
+ if Strategy.const_defined?(capitalized)
95
+ self.strategy = Strategy.const_get(capitalized)
96
+ else
97
+ raise UnknownStrategy, s
98
+ end
99
+ else
100
+ @@strategy = s
101
+ end
102
+ end
103
+ end
104
+
105
+
106
+
107
+ require "deprecator/core_ext"
@@ -0,0 +1,26 @@
1
+ # everything that goes outside of Deprecator namespace - goes here
2
+
3
+ class Module
4
+ include Deprecator::ClassClassMethods
5
+ end
6
+
7
+ class Class
8
+ include Deprecator::ClassClassMethods
9
+ end
10
+
11
+ module Kernel
12
+ def deprecated reason=nil, *args
13
+ Deprecator.strategy.deprecated(reason, caller_line, args)
14
+ end
15
+ alias DEPRECATED deprecated
16
+
17
+ [:fixme!, :todo!, :not_implemented].each{|method|
18
+ define_method(method){|msg=nil, *args| Deprecator.strategy.send(method, msg, caller_line, args) }
19
+ }
20
+ alias FIXME fixme!
21
+ alias TODO todo!
22
+ alias NOT_IMPLEMENTED not_implemented
23
+ end
24
+
25
+ DEPRECATED = Deprecator::Deprecated
26
+ Deprecated = DEPRECATED unless Object.const_defined?(:Deprecated)
@@ -0,0 +1,59 @@
1
+ module Deprecator
2
+ module Strategy
3
+
4
+
5
+ class Base
6
+ def class_found o, where=nil, reason=nil, args=nil
7
+ # msg(if reason
8
+ # reason.gsub(/%{cls}/, o)
9
+ # else
10
+ # "#{o} is deprecated!"
11
+ # end, where)
12
+ end
13
+ def method_found cls,name, reason, where=nil
14
+ this = self
15
+ unless cls.method_defined?(name) # also we may place stubs there for existing methods in other strategies
16
+ cls.send :define_method, name, ->(*args){
17
+ this.method_called cls,name,reason,caller_line,where
18
+ }
19
+ else
20
+ method = cls.instance_method(name)
21
+ cls.send :define_method, name, ->(*args){
22
+ this.method_called cls,name,reason,caller_line,where
23
+ method.bind(self).call(*args)
24
+ }
25
+ end
26
+ end
27
+ def fixme! msg, where, args; end
28
+ def todo! msg, where, args; end
29
+ def not_implemented msg, where, args
30
+ raise NotImplemented, "method at #{where} called from #{caller_line(2)}"
31
+ end
32
+ end
33
+
34
+ class Warning < Base
35
+ def msg msg, where=nil
36
+ warn "[DEPRECATED] #{msg}"
37
+ end
38
+
39
+ def object_found o, where=nil, reason=nil # deprecated class initialize
40
+ msg "deprecated class #{o.class} instantiated", where
41
+ end
42
+
43
+ # this is not entry point
44
+ def method_called cls,name,reason=nil,where,defined_at
45
+ msg "method #{name} is deprecated. Called from #{caller_line}"
46
+ end
47
+
48
+ def deprecated reason=nil, where=caller_line, args=nil
49
+ where =~ /in `(.+)'$/
50
+ method_name = $1 || '<unknown>'
51
+ reason ||= "%{method} is deprecated!"
52
+ reason.gsub!('%{method}', method_name)
53
+ msg reason, where
54
+ end
55
+ end
56
+
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Deprecator
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,96 @@
1
+ require 'rspec/autorun'
2
+ require 'deprecator'
3
+
4
+ describe Deprecator do
5
+ subject{ Deprecator }
6
+
7
+ context "deprecation marking of" do
8
+
9
+ context "classes" do
10
+ before{
11
+ class DummyStrategy; end
12
+ subject.strategy = DummyStrategy
13
+ }
14
+ it "via inheriting" do
15
+ subject.strategy.should_receive(:class_found).with(kind_of(Class), duck_type(:to_s))
16
+ Class.new(DEPRECATED::Class)
17
+ end
18
+ it "via extending" do
19
+ subject.strategy.should_receive(:class_found).with(kind_of(Class), duck_type(:to_s))
20
+ Class.new{ extend DEPRECATED }
21
+ end
22
+ it "via including" do
23
+ subject.strategy.should_receive(:class_found).with(kind_of(Class), duck_type(:to_s))
24
+ Class.new{ include DEPRECATED }
25
+ end
26
+ it "via call to deprecated" do
27
+ subject.strategy.should_receive(:class_found).with(kind_of(Class), duck_type(:to_s), "abc", [])
28
+ Class.new{ deprecated "abc" }
29
+ end
30
+ end
31
+
32
+ context "modules" do
33
+ it "via extending" do
34
+ subject.strategy.should_receive(:class_found).with(kind_of(Module), duck_type(:to_s))
35
+ Module.new{ extend DEPRECATED }
36
+ end
37
+ it "via including" do
38
+ subject.strategy.should_receive(:class_found).with(kind_of(Module), duck_type(:to_s))
39
+ Module.new{ include DEPRECATED }
40
+ end
41
+ it "via call to deprecated" do
42
+ subject.strategy.should_receive(:class_found).with(kind_of(Module), duck_type(:to_s), "abc", [])
43
+ Module.new{ deprecated "abc" }
44
+ end
45
+ end
46
+
47
+ context "methods" do
48
+ before{
49
+ subject.strategy = :warning
50
+ }
51
+ it "simple" do
52
+ subject.strategy.should_receive(:deprecated).with("reason", duck_type(:to_s), [])
53
+ deprecated "reason"
54
+ end
55
+
56
+ it "marking in class" do
57
+ cls = Class.new{
58
+ deprecated_method "reason"
59
+ def meth; end
60
+ }
61
+ subject.strategy.should_receive(:method_called).with(cls, :meth, "reason", /#{Regexp.escape __FILE__}:#{__LINE__+1}/, /#{Regexp.escape __FILE__}:#{__LINE__-3}/)
62
+ cls.new.meth
63
+ end
64
+
65
+ it "deprecation by name defines method" do
66
+ cls = Class.new{
67
+ deprecated_method :meth, "reason"
68
+ }
69
+ subject.strategy.should_receive(:method_called).with(cls, :meth, "reason", /#{Regexp.escape __FILE__}:#{__LINE__+1}/, /#{Regexp.escape __FILE__}:#{__LINE__-2}/)
70
+ cls.new.meth
71
+ end
72
+ end
73
+ end
74
+
75
+ context "strategy" do
76
+ context "set" do
77
+ it "via object" do
78
+ obj = Object.new
79
+ subject.strategy = obj
80
+ subject.strategy.should == obj
81
+ end
82
+ it "via class" do
83
+ cls = Class.new
84
+ subject.strategy = cls
85
+ subject.strategy.should be_a cls
86
+ end
87
+ it "by symbolic name" do
88
+ subject.strategy = :warning
89
+ subject.strategy.should be_a(Deprecator::Strategy::Warning)
90
+ end
91
+ it "raises on wrong strategy name" do
92
+ expect{ subject.strategy = :no_such_strategy }.to raise_error(Deprecator::UnknownStrategy)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,18 @@
1
+ require 'rspec/autorun'
2
+ require 'deprecator'
3
+
4
+ describe Deprecator::Strategy::Warning do
5
+ before{
6
+ Deprecator.strategy = subject
7
+ }
8
+ it "calls warn" do
9
+ subject.should_receive(:warn).with("[DEPRECATED] block (2 levels) in <top (required)> is deprecated!")
10
+ subject.deprecated
11
+ end
12
+
13
+ it "raises on not implemented" do
14
+ expect {
15
+ not_implemented
16
+ }.to raise_error(Deprecator::NotImplemented)
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deprecator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vasily Fedoseyev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: is_a
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.1'
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.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
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: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
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: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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: '0'
78
+ description: Yet another library for dealing with code deprecation in ruby
79
+ email:
80
+ - vasilyfedoseyev@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - deprecator.gemspec
92
+ - lib/deprecator.rb
93
+ - lib/deprecator/core_ext.rb
94
+ - lib/deprecator/strategy.rb
95
+ - lib/deprecator/version.rb
96
+ - spec/deprecator_spec.rb
97
+ - spec/strategy/warning_spec.rb
98
+ homepage: http://github.com/Vasfed/deprecator
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.9.1
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ segments:
118
+ - 0
119
+ hash: -2851817988265802731
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Adds some beauty and structure to code deprecation, see readme
126
+ test_files:
127
+ - spec/deprecator_spec.rb
128
+ - spec/strategy/warning_spec.rb