matchete 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3717143d8e222ee9da803cd7647f328c36f56a7
4
+ data.tar.gz: bf3af40853ad0382c1776698fb8997da63a7254b
5
+ SHA512:
6
+ metadata.gz: 4684675527ccb701546d644fb324d189befd55d0f4dfcb77fa09e58dab98b67c40ccd629494e3cdc495b54a3c188d1acd67a38fb2b02ea19443ecca87596e8f8
7
+ data.tar.gz: 30b6af0a2dbf30a61cf307cdad0d9e691aa9039b34aa4c6569c90a8876bc937a5975d93d4261286dbbfe4243510f4e1d11883eddec29bf62ea93649e7b3dd583
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'rspec'
5
+ gem 'bundler'
6
+ gem 'jeweler'
7
+ end
8
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Alexander Ivanov
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/README.md ADDED
@@ -0,0 +1 @@
1
+ #Matchete
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'rspec/core'
15
+ require 'rspec/core/rake_task'
16
+ RSpec::Core::RakeTask.new(:spec) do |spec|
17
+ spec.pattern = FileList['spec/**/*_spec.rb']
18
+ end
19
+
20
+ task :default => :spec
data/lib/matchete.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'set'
2
+
3
+ module Matchete
4
+ class NotResolvedError < StandardError
5
+ end
6
+
7
+ def self.included(klass)
8
+ klass.extend ClassMethods
9
+ klass.instance_variable_set "@functions", {}
10
+ klass.instance_variable_set "@default_functions", {}
11
+ end
12
+
13
+ Any = -> (x) { true }
14
+ None = -> (x) { false }
15
+
16
+ module ClassMethods
17
+ def on(*guards, function)
18
+ @functions[function] ||= []
19
+ @functions[function] << [guards, instance_method(function)]
20
+ convert_to_matcher function
21
+ end
22
+
23
+ def default(method_name)
24
+ @default_functions[method_name] = instance_method(method_name)
25
+ convert_to_matcher method_name
26
+ end
27
+
28
+ def duck(*method_names)
29
+ -> object do
30
+ method_names.all? do |method_name|
31
+ object.respond_to? method_name
32
+ end
33
+ end
34
+ end
35
+
36
+ def convert_to_matcher(function)
37
+ define_method(function) do |*args|
38
+ guards = self.class.instance_variable_get('@functions')[function].find do |guards, _|
39
+ self.class.match_guards guards, args
40
+ end
41
+
42
+ handler = if guards.nil?
43
+ default_method = self.class.instance_variable_get('@default_functions')[function]
44
+ if default_method
45
+ default_method
46
+ else
47
+ raise NotResolvedError.new("not resolved #{function} with #{args}")
48
+ end
49
+ else
50
+ guards[1]
51
+ end
52
+
53
+ handler.bind(self).call *args
54
+ end
55
+ end
56
+
57
+ def match_guards(guards, args)
58
+ guards.zip(args).all? do |guard, arg|
59
+ match_guard guard, arg
60
+ end
61
+ end
62
+
63
+ def match_guard(guard, arg)
64
+ case guard
65
+ when Module
66
+ arg.is_a? guard
67
+ when Symbol
68
+ send guard, arg
69
+ when Proc
70
+ guard.call arg
71
+ when String
72
+ guard == arg
73
+ when Regexp
74
+ arg.is_a? String and guard.match arg
75
+ when Array
76
+ arg.is_a?(Array) and
77
+ guard.zip(arg).all? { |child_guard, child| match_guard child_guard, child }
78
+ else
79
+ guard == arg
80
+ end
81
+ end
82
+ end
83
+ end
84
+
data/matchete.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'matchete'
6
+ s.version = '0.0.1'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Alexander Ivanov"]
9
+ s.email = ["alehander42@gmail.com"]
10
+ s.homepage = 'https://github.com/alehander42/matchete'
11
+ s.summary = %q{Method overloading for Ruby based on pattern matching}
12
+ s.description = %q{A DSL for method overloading for Ruby based on pattern matching}
13
+
14
+ s.add_development_dependency 'rspec', '~> 0'
15
+
16
+ s.license = 'MIT'
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,72 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'matchete'
5
+
6
+ describe Matchete do
7
+ it 'can be used to overload a method in a class' do
8
+ class A
9
+ include Matchete
10
+
11
+ on Integer,
12
+ def play(value)
13
+ :integer
14
+ end
15
+
16
+ on Float, Any,
17
+ def play(value, object)
18
+ :float
19
+ end
20
+ end
21
+
22
+ a = A.new
23
+ a.play(2).should eq :integer
24
+ a.play(2.2, 4).should eq :float
25
+ end
26
+
27
+ it 'can use a pattern based on classes and modules' do
28
+ class A
29
+ include Matchete
30
+
31
+ on Integer,
32
+ def play(value)
33
+ :integer
34
+ end
35
+
36
+ on Float,
37
+ def play(value)
38
+ :float
39
+ end
40
+
41
+ on Enumerable,
42
+ def play(value)
43
+ :enumerable
44
+ end
45
+ end
46
+
47
+ a = A.new
48
+ a.play(2).should eq :integer
49
+ a.play(2.2).should eq :float
50
+ a.play([2]).should eq :enumerable
51
+ end
52
+
53
+ it 'can use a pattern based on nested arrays with classes/modules' do
54
+ class A
55
+ include Matchete
56
+
57
+ on [Integer, Float],
58
+ def play(values)
59
+ [:integer, :float]
60
+ end
61
+
62
+ on [[Integer], Any],
63
+ def play(values)
64
+ :s
65
+ end
66
+ end
67
+
68
+ a = A.new
69
+ a.play([2, 2.2]).should eq [:integer, :float]
70
+ a.play([[2], Matchete]).should eq :s
71
+ end
72
+ end
File without changes
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matchete
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Ivanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A DSL for method overloading for Ruby based on pattern matching
28
+ email:
29
+ - alehander42@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - lib/matchete.rb
39
+ - matchete.gemspec
40
+ - spec/matchete_spec.rb
41
+ - spec/spec_helper.rb
42
+ homepage: https://github.com/alehander42/matchete
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Method overloading for Ruby based on pattern matching
66
+ test_files: []