defp 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 +7 -0
- data/.gitignore +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +62 -0
- data/Rakefile +10 -0
- data/defp.gemspec +23 -0
- data/lib/defp.rb +47 -0
- data/lib/defp/version.rb +3 -0
- data/test.rb +6 -0
- data/test/test_defp.rb +70 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a7c467638e41b299b74540c9284c4741d3c9015
|
4
|
+
data.tar.gz: 72b4971ae4955ce977a89a9986e2434353861360
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9dfe3d70d7165f6dbc101aca43950115d65e2495025cb4f05bac62b3a9ed7fd42a3b33e30d649dff06d850ed70f2600d21865b341d98368f6c6987a968d077e8
|
7
|
+
data.tar.gz: cf23d6a536ac88547ee93faac954ab2ea8e0ba8adeeab18b7b8b61f3f8e3420fc9411ed095fa7e817660282f59b2000269b5b723b28f5f26c6e30ef3397f7de5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 doodzik
|
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,62 @@
|
|
1
|
+
# Defp
|
2
|
+
|
3
|
+
An pattern matching implementation in ruby inspired by haskell
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'CallBaecker'
|
12
|
+
gem 'defp'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install defp
|
22
|
+
$ gem install CallBaecker
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Example
|
28
|
+
# please extend/include in this order
|
29
|
+
extend Defp
|
30
|
+
include CallBaecker
|
31
|
+
|
32
|
+
# With normal params
|
33
|
+
def normal_params(value1, value2)
|
34
|
+
defp('hallo', _) == 'bubu1'
|
35
|
+
defp(_, 'hallo') == -> {
|
36
|
+
'bubu2'
|
37
|
+
}
|
38
|
+
'bubu3'
|
39
|
+
end
|
40
|
+
|
41
|
+
# With keyword arguments
|
42
|
+
def self.keyword_params(value1: ,value2:)
|
43
|
+
defp(value1: 'hallo', value2: _) == 'bubu1' # you can specify blank vars
|
44
|
+
defp(value2: 'hallo') == -> { 'bubu2' } # or not specifying them at all
|
45
|
+
'bubu3'
|
46
|
+
end
|
47
|
+
|
48
|
+
# With mixed params
|
49
|
+
def self.mixed_params(value1 = {}, value2, value3)
|
50
|
+
defp({value2: 'hallo'}, 'dud', _) == 'bubu1'
|
51
|
+
'bubu2'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it ( https://github.com/[my-github-username]/defp/fork )
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/defp.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 'defp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "defp"
|
8
|
+
spec.version = Defp::VERSION
|
9
|
+
spec.authors = ["doodzik"]
|
10
|
+
spec.email = ["4004blog@gmail.com"]
|
11
|
+
spec.summary = %q{Pattern matching for ruby}
|
12
|
+
spec.homepage = "https://github.com/doodzik/defp.git"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "CallBaecker"
|
23
|
+
end
|
data/lib/defp.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "defp/version"
|
2
|
+
|
3
|
+
module Defp
|
4
|
+
def self.extended(base)
|
5
|
+
base.send :include, DefpMethods
|
6
|
+
base.extend DefpMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module DefpMethods
|
10
|
+
def defp(*args)
|
11
|
+
matches_pattern?(*args) ? Defp::Between.new : Defp::NullBetween.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def matches_pattern?(*args)
|
15
|
+
# arg[1] == called_by_method_args
|
16
|
+
# arg[0] == pattern_args
|
17
|
+
args.zip(@__last_args)
|
18
|
+
.select { |arg| arg[0] }
|
19
|
+
.all? do |arg|
|
20
|
+
# TODO cleanup
|
21
|
+
if arg[0].is_a? Hash
|
22
|
+
pattern_args = arg[0].reject {|k,v| !v}
|
23
|
+
pattern_args.keys.all? { |key| arg[1][key] == arg[0][key] }
|
24
|
+
else
|
25
|
+
arg[0] == arg[1]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def _
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class NullBetween
|
36
|
+
def ==(valu)
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Between
|
42
|
+
def ==(param)
|
43
|
+
value = param.is_a?(Proc) ? param.call : param
|
44
|
+
throw :CallBaeckerDone, value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/defp/version.rb
ADDED
data/test.rb
ADDED
data/test/test_defp.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'CallBaecker'
|
3
|
+
|
4
|
+
require 'defp'
|
5
|
+
|
6
|
+
class Setup
|
7
|
+
extend Defp
|
8
|
+
include CallBaecker
|
9
|
+
|
10
|
+
def defp_normal_params(value1, value2)
|
11
|
+
defp('hallo', false) == 'bubu1'
|
12
|
+
defp(_, 'hallo') == -> {
|
13
|
+
'bubu2'
|
14
|
+
}
|
15
|
+
'bubu3'
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.defp_normal_params(value1, value2)
|
19
|
+
defp('hallo', false) == 'bubu1'
|
20
|
+
defp(_, 'hallo') == -> {
|
21
|
+
'bubu2'
|
22
|
+
}
|
23
|
+
'bubu3'
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.defp_hash_params(value1: ,value2:)
|
27
|
+
defp(value1: 'hallo', value2: _) == 'bubu1'
|
28
|
+
defp(value2: 'hallo') == -> { 'bubu2' }
|
29
|
+
'bubu3'
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.defp_multi_params(value1 = {}, value2, value3)
|
33
|
+
defp({value2: 'hallo'}, 'dud', _) == 'bubu1'
|
34
|
+
'bubu2'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class DefpTest < Minitest::Test
|
39
|
+
def test_instance_method
|
40
|
+
assert_equal Setup.new.defp_normal_params('value', 'value'), 'bubu3'
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_normal_param_not_matched
|
44
|
+
assert_equal Setup.defp_normal_params('value', 'value'), 'bubu3'
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_normal_param_matched
|
48
|
+
assert_equal Setup.defp_normal_params('hallo', 'value'), 'bubu1'
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_normal_param_matched_with_blank
|
52
|
+
assert_equal Setup.defp_normal_params('hallo', 'hallo'), 'bubu1'
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_normal_param_matched_given_block
|
56
|
+
assert_equal Setup.defp_normal_params('value', 'hallo'), 'bubu2'
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_hash_param
|
60
|
+
assert_equal Setup.defp_hash_params(value1: 'hallo', value2: 'dud'), 'bubu1'
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_hash_param_two
|
64
|
+
assert_equal Setup.defp_hash_params(value2: 'hallo', value1: 'dud'), 'bubu2'
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_mixed_params
|
68
|
+
assert_equal Setup.defp_multi_params({value2: 'hallo'}, 'dud', 'kuk'), 'bubu1'
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: defp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- doodzik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-25 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: CallBaecker
|
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:
|
56
|
+
email:
|
57
|
+
- 4004blog@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- defp.gemspec
|
68
|
+
- lib/defp.rb
|
69
|
+
- lib/defp/version.rb
|
70
|
+
- test.rb
|
71
|
+
- test/test_defp.rb
|
72
|
+
homepage: https://github.com/doodzik/defp.git
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Pattern matching for ruby
|
96
|
+
test_files:
|
97
|
+
- test/test_defp.rb
|
98
|
+
has_rdoc:
|