matchrb 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: fb61077f3da5e5af7fd2ef01dbdd248b0c4ad5d1
4
+ data.tar.gz: bf16bb6c22c929cf20e1a2c1595b143fe3664951
5
+ SHA512:
6
+ metadata.gz: e3fd2a0e2667e685a329abb316db4514ae39f6de64fd6d708f9c52573d0d0d876770dcb6b2e46a8842b2362d701fc8bdb0a0646ce63c7fb80213ed3a3a2095b3
7
+ data.tar.gz: 1298b29f54502424a1e77fb12d880f89ce99bc7af0c91861d378ddab9014848721961f4304090a7917775d1848dfe91a2c1a47fde95d97fa6c74aad62d471ccc
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in matchrb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2014 Diogo Lisboa
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification,
6
+ are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation and/or
13
+ other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
19
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # matchrb
2
+
3
+ **matchrb** (pronounced 'matcherby') provides a simple but powerful way to do
4
+ pattern matching in Ruby.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'matchrb'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install matchrb
19
+
20
+ ## Usage
21
+
22
+ `matchrb` implements two methods, `#match` and `#otherwise`. You can use them
23
+ globaly like so:
24
+
25
+ ```ruby
26
+ require 'matchrb/global'
27
+
28
+ object = 100
29
+ match object,
30
+ String => "object is a string",
31
+ Integer => "object is an integer",
32
+ otherwise => "object is...just an object"
33
+ ```
34
+
35
+ or, to avoid name clashes, use them individually:
36
+
37
+ ```ruby
38
+ require 'matchrb'
39
+
40
+ object = 100
41
+ Matchrb.match object,
42
+ String => "object is a string",
43
+ Integer => "object is an integer",
44
+ Matchrb.otherwise => "object is...just an object"
45
+ ```
46
+
47
+ That's it!
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/dlisboa/matchrb/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Run tests"
4
+ task :test do
5
+ puts %x[ruby spec/*]
6
+ end
@@ -0,0 +1,2 @@
1
+ require 'matchrb'
2
+ Object.include(Matchrb)
@@ -0,0 +1,3 @@
1
+ module Matchrb
2
+ VERSION = "0.0.1"
3
+ end
data/lib/matchrb.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "matchrb/version"
2
+
3
+ module Matchrb
4
+ extend self
5
+
6
+ def match(something, patterns)
7
+ apply = lambda do |action, value|
8
+ return action unless action.respond_to?(:to_proc)
9
+
10
+ action = action.to_proc
11
+ action.arity.zero? ? action[] : action[value]
12
+ end
13
+
14
+ patterns.each do |pattern, action|
15
+ if (value = pattern === something)
16
+ return apply[action, value]
17
+ end
18
+ end
19
+
20
+ nil
21
+ end
22
+
23
+ def otherwise
24
+ Object
25
+ end
26
+ end
data/matchrb.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'matchrb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "matchrb"
8
+ spec.version = Matchrb::VERSION
9
+ spec.authors = ["Diogo Lisboa"]
10
+ spec.email = ["diogoslisboa@gmail.com"]
11
+ spec.summary = %q{A tiny play on pattern matching in Ruby.}
12
+ spec.description = <<-EOS
13
+ matchrb (pronounced 'matcherby') provides a simple but powerful way to do
14
+ pattern matching in Ruby.
15
+ EOS
16
+ spec.homepage = "https://github.com/dlisboa/matchrb"
17
+ spec.license = "BSD"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.test_files = spec.files.grep(%r{spec})
21
+ spec.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,144 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require "minitest/autorun"
4
+ require "minitest/spec"
5
+
6
+ require "matchrb/global"
7
+
8
+ describe "match" do
9
+ describe "when the pattern is a class" do
10
+ it "matches the kind of the object" do
11
+ Foo = Class.new
12
+ foo = Foo.new
13
+
14
+ result = match foo,
15
+ Foo => "foo is Foo"
16
+
17
+ result.must_equal "foo is Foo"
18
+ end
19
+ end
20
+
21
+ describe "when the pattern is a Regexp" do
22
+ it "matches the regex against the object" do
23
+ result = match "this is a string",
24
+ /this is not a string/ => false,
25
+ /this is a.*/ => true
26
+
27
+ result.must_equal true
28
+ end
29
+ end
30
+
31
+ describe "when no pattern is matched" do
32
+ it "returns nil" do
33
+ result = match "this is a string",
34
+ Integer => false,
35
+ Class => true
36
+
37
+ result.must_equal nil
38
+ end
39
+ end
40
+
41
+ describe "when the action is an object" do
42
+ it "returns that object" do
43
+ someobject = Object.new
44
+
45
+ result = match 1,
46
+ Integer => someobject
47
+
48
+ result.object_id.must_equal someobject.object_id
49
+ end
50
+ end
51
+
52
+ describe "when the action is a :to_proc'able object" do
53
+ it "returns the result of calling that object" do
54
+ Identity = Class.new do
55
+ def initialize(number)
56
+ @number = number
57
+ end
58
+
59
+ def to_proc
60
+ -> { @number }
61
+ end
62
+ end
63
+
64
+ result = match 1,
65
+ Integer => Identity.new(10)
66
+
67
+ result.must_equal 10
68
+ end
69
+ end
70
+
71
+ describe "when an `otherwise' pattern is given" do
72
+ it "matches that pattern if no other is matched before" do
73
+ result = match "this is a string",
74
+ 666 => true,
75
+ /the quick brown fox/ => false,
76
+ otherwise => "none matched"
77
+
78
+ result.must_equal "none matched"
79
+ end
80
+
81
+ it "matches more specific patterns before that" do
82
+ result = match "this is a string",
83
+ String => true,
84
+ Class => false,
85
+ otherwise => "none matched"
86
+
87
+ result.must_equal true
88
+ end
89
+ end
90
+
91
+ it "matches any predicate if it returns true for that object" do
92
+ Twice = Class.new do
93
+ def self.===(object)
94
+ object % 2 == 0
95
+ end
96
+ end
97
+
98
+ number = 21 * 2
99
+
100
+ result = match number,
101
+ Twice => "it's twice something"
102
+
103
+ result.must_equal "it's twice something"
104
+ end
105
+
106
+ describe "when passed an 'extractor'" do
107
+ it "it calls the action with the extracted value" do
108
+ Multiple = Class.new do
109
+ def self.of(number)
110
+ new(number)
111
+ end
112
+
113
+ def initialize(number)
114
+ @number = number
115
+ end
116
+
117
+ def ===(object)
118
+ object % @number == 0 ? object/@number : false
119
+ end
120
+ end
121
+
122
+ number = 99
123
+
124
+ result = match number,
125
+ Multiple.of(3) => ->(value) { value }
126
+
127
+ result.must_equal 33
128
+ end
129
+ end
130
+
131
+ describe "when used as control flow" do
132
+ it "returns from outer context when using `proc'" do
133
+ def foobar
134
+ match 1,
135
+ Integer => proc { return 666 }
136
+
137
+ 10
138
+ end
139
+
140
+ foobar.must_equal 666
141
+ end
142
+ end
143
+ end
144
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matchrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diogo Lisboa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ matchrb (pronounced 'matcherby') provides a simple but powerful way to do
15
+ pattern matching in Ruby.
16
+ email:
17
+ - diogoslisboa@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/matchrb.rb
28
+ - lib/matchrb/global.rb
29
+ - lib/matchrb/version.rb
30
+ - matchrb.gemspec
31
+ - spec/match_spec.rb
32
+ homepage: https://github.com/dlisboa/matchrb
33
+ licenses:
34
+ - BSD
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.2.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: A tiny play on pattern matching in Ruby.
56
+ test_files:
57
+ - matchrb.gemspec
58
+ - spec/match_spec.rb
59
+ has_rdoc: