overload 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2651b0fcd60f70a12139b23667621cd72f782be
4
+ data.tar.gz: 9c280a048cc6a343ed53255dea7b59cc6a693cea
5
+ SHA512:
6
+ metadata.gz: 1e75f5300859fe8cdacb727c5184a4f0aa5b5243f902c996de0ac009b0ab57ec5766104c573c02d90205c299b6c048f9a5fc40285cdff89b8aa4b19b632b51c6
7
+ data.tar.gz: ddeeb25c7a32f1c62de29ee25d08b424979c7fe11e48d2c809ababcceb1a5037f447d6c920fc986a620bb83e9caa6b9c423ccda607694fd60ffb4bd9f8b619f5
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in overload.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alex Sunderland
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,43 @@
1
+ # Overload
2
+
3
+ Allows instance methods to be overloaded.
4
+ Also functions with inheritance.
5
+ Methods defined twice with the same argument requirements after overload will
6
+ use the last defined method definition.
7
+
8
+ This gem is not intended for serious use.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'overload'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install overload
23
+
24
+ ## Usage
25
+
26
+ Extend the module
27
+
28
+ extend Overload
29
+
30
+ Then declare your overloaded methods
31
+
32
+ overload :foo
33
+ overload :bar, :baz, :bam
34
+
35
+ Methods will be selected according to which has the most appropriate arity.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,59 @@
1
+ require "overload/version"
2
+ require "overload/monkeypatching/hash.rb"
3
+
4
+ module Overload
5
+
6
+ attr_accessor :kept_methods
7
+
8
+ def overload(*methods)
9
+ @kept_methods ||= {}
10
+ methods.each do |method|
11
+ kept_methods[method] = {}
12
+ end
13
+ end
14
+
15
+ def method_added(new_method)
16
+ if @evil_things
17
+ # Maybe next time.
18
+ @evil_things = false
19
+ return
20
+ end
21
+
22
+ # To allow inheriting of overloaded methods
23
+ if kept_methods.nil?
24
+ self.ancestors.each_with_index do |ancestor, index|
25
+ if ancestor.respond_to?(:kept_methods) && ancestor.kept_methods
26
+ # To prevent changing overloaded methods on the parent, we must dup
27
+ self.kept_methods = self.ancestors[index].kept_methods.deep_dup
28
+ end
29
+ end
30
+ end
31
+
32
+ return unless kept_methods[new_method]
33
+
34
+ new_method_object = new.method(new_method)
35
+
36
+ if method_defined?(new_method)
37
+ kept_methods[new_method][new_method_object.arity] = new_method_object
38
+ instance_variable_set(:@kept_methods, kept_methods)
39
+
40
+ overload_method = Proc.new do |*args|
41
+ kept_methods = self.class.instance_variable_get(:@kept_methods)
42
+ if kept_methods[__method__][args.count]
43
+ kept_methods[__method__][args.count].call(*args)
44
+ else
45
+ kept_methods[__method__].keys.sort.each do |arity|
46
+ next unless arity < 0 && arity.abs - 1 <= args.count
47
+ return kept_methods[__method__][arity].call(*args)
48
+ end
49
+ end
50
+ end
51
+
52
+ # This was fun, but once was quite enough.
53
+ @evil_things = true
54
+
55
+ define_method new_method, overload_method
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,10 @@
1
+ class Hash
2
+ def deep_dup
3
+ duplicate = self.dup
4
+ duplicate.each_pair do |k,v|
5
+ tv = duplicate[k]
6
+ duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
7
+ end
8
+ duplicate
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Overload
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'overload/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "overload"
8
+ spec.version = Overload::VERSION
9
+ spec.authors = ["Alex Sunderland"]
10
+ spec.email = ["alex.sunderland@gmail.com"]
11
+ spec.description = %q{
12
+ Allows instance methods to be overloaded.
13
+ Also functions with inheritance.
14
+ Methods overloaded twice will use the last version.
15
+ }
16
+ spec.summary = %q{Allows instance methods to be overloaded.}
17
+ spec.homepage = ""
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ end
@@ -0,0 +1,102 @@
1
+ class TestClass
2
+ require "overload"
3
+ extend Overload
4
+
5
+ overload :foo, :bar
6
+
7
+ def foo
8
+ "overwritten"
9
+ end
10
+
11
+ def foo
12
+ 0
13
+ end
14
+
15
+ def foo(arg)
16
+ 1
17
+ end
18
+
19
+ def foo(arg1, arg2)
20
+ 2
21
+ end
22
+
23
+ def foo(*args)
24
+ "some"
25
+ end
26
+
27
+ def foo(arg1, arg2, *args)
28
+ "more"
29
+ end
30
+
31
+ def bar
32
+ "bar"
33
+ end
34
+
35
+ def bar(arg1)
36
+ "bar 1"
37
+ end
38
+
39
+ def baz(arg1)
40
+ "bax"
41
+ end
42
+
43
+ def baz
44
+ "baz"
45
+ end
46
+ end
47
+
48
+ class TestClassChild < TestClass
49
+ def foo(arg1, arg2, arg3, *args)
50
+ "TestClassChild foo"
51
+ end
52
+
53
+ overload :bam
54
+
55
+ def bam(*args)
56
+ "bam"
57
+ end
58
+
59
+ def bam(arg1, arg2, arg3, *args)
60
+ "bam with required args"
61
+ end
62
+ end
63
+
64
+ describe TestClass do
65
+ it "returns 0 when called with 0 arguments" do
66
+ expect(subject.foo).to eq(0)
67
+ end
68
+
69
+ it "returns 1 when called with 1 argument" do
70
+ expect(subject.foo(1)).to eq(1)
71
+ end
72
+
73
+ it "returns 2 when called with 2 arguments" do
74
+ expect(subject.foo(1, 2)).to eq(2)
75
+ end
76
+
77
+ it "calls the method with the greatest negative arity when ambiguous" do
78
+ # this also confirms that child methods don't overwrite parent methods.
79
+ expect(subject.foo(1, 2, 3, 4, 5)).to eq("more")
80
+ end
81
+
82
+ it "can overload multiple methods" do
83
+ expect(subject.bar).to eq("bar")
84
+ expect(subject.bar(1)).to eq("bar 1")
85
+ end
86
+
87
+ it "won't overload non-declared methods" do
88
+ expect(subject.baz).to eq("baz")
89
+ expect { subject.baz(1) }.to raise_error(ArgumentError)
90
+ end
91
+ end
92
+
93
+ describe TestClassChild do
94
+ it "functions in child classes" do
95
+ expect(subject.foo(1, 2)).to eq(2)
96
+ expect(subject.foo(1, 2, 3)).to eq("TestClassChild foo")
97
+ end
98
+
99
+ it "will not select methods with too large negative arity" do
100
+ expect(subject.bam(1,2)).to eq("bam")
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: overload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Sunderland
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "\n Allows instance methods to be overloaded.\n Also functions
56
+ with inheritance.\n Methods overloaded twice will use the last version.\n "
57
+ email:
58
+ - alex.sunderland@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/overload.rb
69
+ - lib/overload/monkeypatching/hash.rb
70
+ - lib/overload/version.rb
71
+ - overload.gemspec
72
+ - spec/lib/overload_spec.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Allows instance methods to be overloaded.
97
+ test_files:
98
+ - spec/lib/overload_spec.rb