overrides 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ *.gem
2
+ .bundle
3
+ .config
4
+ .yardoc
5
+ Gemfile.lock
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ test/tmp
14
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Karol Bucek
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.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Overrides
2
+ VERSION = '0.1.0'
3
+ end
data/lib/overrides.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'overrides/version'
2
+
3
+ # Allows for an #overrides annotation for your methods.
4
+ #
5
+ # Thus might help you e.g. during refactoring (method renaming) to assure methods
6
+ # expected to call a super will fail early during def time instead of when called.
7
+ #
8
+ # Sample :
9
+ #
10
+ # class Message
11
+ # extend Overrides
12
+ #
13
+ # def do_send; end
14
+ # def sent_at; end
15
+ # def reply?; end
16
+ # end
17
+ #
18
+ # class Notice < Message
19
+ # overrides
20
+ # def do_send; super; end
21
+ #
22
+ # def reply?; nil; end
23
+ # def sent_at; nil; end
24
+ #
25
+ # overrides :reply?, :sent_at
26
+ # end
27
+ #
28
+ #
29
+ # Without arguments works with singleton methods as well :
30
+ #
31
+ # class Message
32
+ # extend Overrides
33
+ #
34
+ # def self.deliver(msg); end
35
+ # end
36
+ #
37
+ # class Notice < Message
38
+ # overrides
39
+ # def self.deliver(msg); end
40
+ # end
41
+ #
42
+ # To hook it up for all classes/modules, simply do a :
43
+ #
44
+ # Object.extend Overrides
45
+ #
46
+ # @author kares
47
+ #
48
+ module Overrides
49
+
50
+ Error = NoMethodError
51
+
52
+ def overrides(*names)
53
+ if names.empty? # next method
54
+ @_override = true
55
+ else
56
+ names.each do |name|
57
+ unless Overrides.overriden?(self, name)
58
+ raise Error, "previous (instance) method #{name.inspect} not found"
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def method_added(name)
65
+ return super unless (@_override ||= nil); @_override = false
66
+ unless Overrides.overriden?(self, name, true)
67
+ raise Error, "previous (instance) method #{name.inspect} not found"
68
+ end
69
+ super
70
+ end
71
+
72
+ def singleton_method_added(name)
73
+ return super unless (@_override ||= nil); @_override = false
74
+ unless Overrides.overriden?(self, name, false)
75
+ raise Error, "previous singleton method #{name.inspect} not found"
76
+ end
77
+ super
78
+ end
79
+
80
+ def self.overriden?(klass, method, instance = true)
81
+ if instance
82
+ klass.included_modules.each do |mod|
83
+ return true if mod.instance_methods(true).include? method
84
+ end
85
+ return false unless klass.is_a?(Class) # "only" a Module ...
86
+ return klass.superclass.instance_methods(true).include? method
87
+ else
88
+ (class << klass; self; end).included_modules.each do |mod|
89
+ return true if mod.instance_methods(true).include? method
90
+ end
91
+ return false unless klass.is_a?(Class) # "only" a Module ...
92
+ return klass.superclass.methods(true).include? method
93
+ end
94
+ end
95
+
96
+ end
data/overrides.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'overrides/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "overrides"
8
+ gem.version = Overrides::VERSION
9
+ gem.authors = ["Karol Bucek"]
10
+ gem.email = ["self@kares.org"]
11
+ gem.summary = %q{an #overrides annotation for your methods}
12
+ gem.description = %q{An #overrides annotation for your (Ruby) methods.}
13
+ gem.homepage = "http://github.com/kares/overrides"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "bundler", "~> 1.3"
22
+ gem.add_development_dependency "rake", "~> 10.1"
23
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: overrides
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Karol Bucek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
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: '10.1'
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: '10.1'
46
+ description: ! 'An #overrides annotation for your (Ruby) methods.'
47
+ email:
48
+ - self@kares.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - Rakefile
57
+ - lib/overrides.rb
58
+ - lib/overrides/version.rb
59
+ - overrides.gemspec
60
+ homepage: http://github.com/kares/overrides
61
+ licenses:
62
+ - MIT
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ segments:
74
+ - 0
75
+ hash: -1605842409600528455
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ segments:
83
+ - 0
84
+ hash: -1605842409600528455
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.25
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: ! 'an #overrides annotation for your methods'
91
+ test_files: []