multidispatch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9cb0cc40fa1dbf67b7ac1719ff91f623e65e7fb7
4
+ data.tar.gz: b55d47ba43e2b8f1d6d98fd02e37f59ba4c5ab2e
5
+ SHA512:
6
+ metadata.gz: 0fbe1ab138090370cfda4f1c4be6e4b376d2066e81527a0ff04f98d4821f8bb07f18e6130f2db19d2d9c683e3e753c94bd55ca8217be8189fb349f4f69046928
7
+ data.tar.gz: bf9452366c911bfc1a1973874e6164a52e3e073b687e4d19f7ac595cb426b816c1a48fe3999f5c5adbbef336bf33b8d8484328b8bd0bb93b16c3389fd27d0652
data/.gitignore ADDED
@@ -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 d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-multidispatch.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Iaroslav
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yaroslav Sergeyev
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/README.md ADDED
@@ -0,0 +1,81 @@
1
+ ruby-multidispatch
2
+ ==================
3
+
4
+ Very basic multidispatch in Ruby using natural syntax.
5
+ It solves only one problem: converts
6
+
7
+ ```ruby
8
+ class BoilerplateClass
9
+ def foo(a = nil, b = nil)
10
+ unless a || b
11
+ # do action 1
12
+ # ...
13
+ end
14
+ if a && !b
15
+ # do action 2
16
+ # ...
17
+ end
18
+ if a && b
19
+ # do action 3
20
+ # ...
21
+ end
22
+ end
23
+ end
24
+ ```
25
+
26
+ to
27
+
28
+ ```ruby
29
+ class MultidispatchClass
30
+ include Multidispatch
31
+
32
+ def foo
33
+ # do action 1
34
+ # ...
35
+ end
36
+
37
+ def foo(a)
38
+ # do action 2
39
+ # ...
40
+ end
41
+
42
+ def foo(a,b)
43
+ # do action 3
44
+ # ...
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Installation
50
+
51
+ Add this line to your application's Gemfile:
52
+
53
+ gem 'ruby-multidispatch'
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install ruby-multidispatch
62
+
63
+ ## Usage
64
+
65
+ See samples:
66
+
67
+ * multidispatch.rb - the module itself
68
+ * morning.rb, night.rb - sample classes using multidispatch module
69
+ * main.rb - quick CLI test for **Morning** and **Night**
70
+
71
+ Currently supports overloads by count of mandatory parameters.
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
80
+
81
+ ... and Happy Hacking!
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Multidispatch
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,65 @@
1
+ require "multidispatch/version"
2
+
3
+ module Multidispatch
4
+
5
+ class Store
6
+ attr_reader :store
7
+
8
+ def initialize
9
+ @store = {}
10
+ @locked = nil
11
+ end
12
+
13
+ def get(klass, method_name, arity)
14
+ class_methods = @store[klass.name] || {}
15
+ methods = class_methods[method_name] || {}
16
+ method_body = methods[-1] || methods[arity]
17
+ raise NoMethodError unless method_body
18
+ method_body
19
+ end
20
+
21
+ def set(klass, method_name)
22
+ return nil if locked?
23
+
24
+ class_methods = @store[klass.name] ||= {}
25
+ methods = class_methods[method_name] ||= {}
26
+ method = klass.instance_method(method_name)
27
+ methods[method.arity] = method
28
+ end
29
+
30
+ def locked?
31
+ @locked
32
+ end
33
+
34
+ def lock!
35
+ @locked = true
36
+ self
37
+ end
38
+
39
+ def release!
40
+ @locked = nil
41
+ self
42
+ end
43
+ end
44
+
45
+ def self.included(base)
46
+ base.extend(ClassMethods)
47
+ end
48
+
49
+ module ClassMethods
50
+ puts "ClassMethods: #{self}"
51
+ @@store = Store.new
52
+
53
+ def method_added(name)
54
+ return if @@store.locked? # prevent recursion
55
+ @@store.set(self, name)
56
+
57
+ @@store.lock!
58
+ define_method(name) do |*args|
59
+ method = @@store.get(self.class, name, args.count)
60
+ method.bind(self).call(*args)
61
+ end
62
+ @@store.release!
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multidispatch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multidispatch"
8
+ spec.version = Multidispatch::VERSION
9
+ spec.authors = ["Iaroslav Sergieiev"]
10
+ spec.email = ["noomorph@gmail.com"]
11
+ spec.description = %q{
12
+ Very basic multidispatch in Ruby using natural def syntax.
13
+ }
14
+ spec.summary = %q{
15
+ Very basic multidispatch in Ruby using natural def syntax.
16
+ Currently supports only overloads by count of mandatory parameters.
17
+ }
18
+ spec.homepage = "https://github.com/noomorph/ruby-multidispatch"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files`.split($/)
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "rake"
29
+ end
@@ -0,0 +1,31 @@
1
+ describe "integration test" do
2
+
3
+ it "should not overwrite methods of previous classes" do
4
+ class Intersect1
5
+ include Multidispatch
6
+ def foo() "foo"; end
7
+ end
8
+
9
+ class Intersect2
10
+ include Multidispatch
11
+ def foo() "bar"; end
12
+ end
13
+
14
+ Intersect1.new.foo.should == "foo"
15
+ end
16
+
17
+ it "should work" do
18
+ class Foo
19
+ include Multidispatch
20
+
21
+ def foo() "foo"; end
22
+ def foo(a) "foo #{a}"; end
23
+ def foo(n, a) "#{n} foo #{a}"; end
24
+ end
25
+
26
+ f = Foo.new
27
+ f.foo().should == "foo"
28
+ f.foo(5).should == "foo 5"
29
+ f.foo(0,1).should == "0 foo 1"
30
+ end
31
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe Multidispatch do
4
+ it "should have self.included method" do
5
+ subject.method(:included).should_not be_nil
6
+ end
7
+ end
8
+
9
+ describe Multidispatch::Store do
10
+
11
+ context "when released" do
12
+ it "can be locked" do
13
+ expect { subject.lock! }.to change { subject.locked? }.from(nil).to(true)
14
+ end
15
+
16
+ it "has no effect to release" do
17
+ expect { subject.release! }.not_to change { subject.locked? }
18
+ end
19
+
20
+ context "when method added" do
21
+ before { @the_method = subject.set(Hash, :inspect) }
22
+
23
+ it { @the_method.should be_instance_of(UnboundMethod) }
24
+ its(:store) { should == {"Hash" => {inspect: {0 => @the_method}}} }
25
+
26
+ it "get() returns it" do
27
+ subject.get(Hash, :inspect, 0).should == @the_method
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ context "when locked" do
34
+ before { subject.lock! }
35
+
36
+ it "can be released" do
37
+ expect { subject.release! }.to change { subject.locked? }.from(true).to(nil)
38
+ end
39
+
40
+ context "when try to add method" do
41
+ before { @the_method = subject.set(Object, :some_method) }
42
+
43
+ it { @the_method.should be_nil }
44
+ its(:store) { should == {} }
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+ require 'multidispatch'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ # config.run_all_when_everything_filtered = true
7
+ # config.filter_run :focus
8
+ config.order = 'random'
9
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multidispatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Iaroslav Sergieiev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-01 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: rspec
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: rake
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 Very basic multidispatch in Ruby using natural def syntax.\n "
56
+ email:
57
+ - noomorph@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/multidispatch.rb
70
+ - lib/multidispatch/version.rb
71
+ - multidispatch.gemspec
72
+ - spec/integration_spec.rb
73
+ - spec/multidispatch_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: https://github.com/noomorph/ruby-multidispatch
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.1.5
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Very basic multidispatch in Ruby using natural def syntax. Currently supports
99
+ only overloads by count of mandatory parameters.
100
+ test_files:
101
+ - spec/integration_spec.rb
102
+ - spec/multidispatch_spec.rb
103
+ - spec/spec_helper.rb