matchete 0.2.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c15b81dead014a1340a37b9172d0ae776022403e
4
- data.tar.gz: 41f24df6592f9844db61c761244adc461a7f188d
3
+ metadata.gz: 5fa9bd76ac56c8cef81d38dc29c909785266d343
4
+ data.tar.gz: 229e287190233cdf977ba70a82a38843ffe7f27b
5
5
  SHA512:
6
- metadata.gz: 374025ed07b2683c768cb4129e72ec3b0d6293282ea6356122f630271bf6d53ff198175344d98e27d8673e11e280ba3443f644d6f71b68e607e82188358b65e8
7
- data.tar.gz: ef2763cf51026c47d1e9fd04b8087ee0ea2183c8647c68414159af74b79e6bf771d216373130249bcc0a9a37bca162968733400316b8d647aedba0cc39618b18
6
+ metadata.gz: db82efc590b09b6475c2e110760d7bc3e4d83051c335127df92b2ceabe71c0710f1b0327da5c86712e5cc24458fc020449ab9ae17ada1bd6a2aea779dbd99b43
7
+ data.tar.gz: 49cee2493d6e2b4e738a6205e412d6f446b33c5440554a38e685c3669d2a37ad87b6f522860d636015cdcedff2271d56fccb71ce9c692281fa2411fcf4f9095a
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ script:
5
+ - bundle exec rake
6
+ addons:
7
+ code_climate:
8
+ repo_token: wxJo5SHv2RawNqcV2JjT
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- group :development do
3
+ group :development, :test do
4
4
  gem 'rspec'
5
5
  gem 'bundler'
6
6
  gem 'jeweler'
7
7
  end
8
8
 
9
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.3.8)
5
+ builder (3.2.2)
6
+ codeclimate-test-reporter (0.4.7)
7
+ simplecov (>= 0.7.1, < 1.0.0)
8
+ descendants_tracker (0.0.4)
9
+ thread_safe (~> 0.3, >= 0.3.1)
10
+ diff-lcs (1.2.5)
11
+ docile (1.1.5)
12
+ faraday (0.9.1)
13
+ multipart-post (>= 1.2, < 3)
14
+ git (1.2.9.1)
15
+ github_api (0.12.3)
16
+ addressable (~> 2.3)
17
+ descendants_tracker (~> 0.0.4)
18
+ faraday (~> 0.8, < 0.10)
19
+ hashie (>= 3.3)
20
+ multi_json (>= 1.7.5, < 2.0)
21
+ nokogiri (~> 1.6.3)
22
+ oauth2
23
+ hashie (3.4.2)
24
+ highline (1.7.2)
25
+ jeweler (2.0.1)
26
+ builder
27
+ bundler (>= 1.0)
28
+ git (>= 1.2.5)
29
+ github_api
30
+ highline (>= 1.6.15)
31
+ nokogiri (>= 1.5.10)
32
+ rake
33
+ rdoc
34
+ json (1.8.3)
35
+ jwt (1.5.0)
36
+ mini_portile (0.6.2)
37
+ multi_json (1.11.0)
38
+ multi_xml (0.5.5)
39
+ multipart-post (2.0.0)
40
+ nokogiri (1.6.6.2)
41
+ mini_portile (~> 0.6.0)
42
+ oauth2 (1.0.0)
43
+ faraday (>= 0.8, < 0.10)
44
+ jwt (~> 1.0)
45
+ multi_json (~> 1.3)
46
+ multi_xml (~> 0.5)
47
+ rack (~> 1.2)
48
+ rack (1.6.1)
49
+ rake (10.4.2)
50
+ rdoc (4.2.0)
51
+ rspec (3.2.0)
52
+ rspec-core (~> 3.2.0)
53
+ rspec-expectations (~> 3.2.0)
54
+ rspec-mocks (~> 3.2.0)
55
+ rspec-core (3.2.3)
56
+ rspec-support (~> 3.2.0)
57
+ rspec-expectations (3.2.1)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.2.0)
60
+ rspec-mocks (3.2.1)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.2.0)
63
+ rspec-support (3.2.2)
64
+ simplecov (0.10.0)
65
+ docile (~> 1.1.0)
66
+ json (~> 1.8)
67
+ simplecov-html (~> 0.10.0)
68
+ simplecov-html (0.10.0)
69
+ thread_safe (0.3.5)
70
+
71
+ PLATFORMS
72
+ ruby
73
+
74
+ DEPENDENCIES
75
+ bundler
76
+ codeclimate-test-reporter
77
+ jeweler
78
+ rspec
data/README.md CHANGED
@@ -3,6 +3,8 @@ Matchete
3
3
 
4
4
  Matchete provides a DSL for method overloading based on pattern matching for Ruby.
5
5
 
6
+ [![Build Status](https://travis-ci.org/alehander42/matchete.svg)](https://travis-ci.org/alehander42/matchete)
7
+
6
8
  It's just a quick hack inspired by weissbier and the use-return-values-of-method-definitions DSL technique used in [harmonic](https://github.com/s2gatev/harmonic)
7
9
 
8
10
  **It supports only ruby 2.1+**
@@ -17,6 +19,9 @@ Features
17
19
  * `on :test?` matches with user-defined predicate methods
18
20
  * `on either('#count', Array)` matches if any of the tests returns true for an arg
19
21
  * `on full_match('#count', '#combine')` matches if all of the tests return true for an arg
22
+ * `on exact(Integer)` matches special values, used as shortcuts in other cases:
23
+ classes, strings starting with '#', etc
24
+ # `default` matches when no match has been found in `on` branches
20
25
 
21
26
 
22
27
 
@@ -28,6 +33,35 @@ Install
28
33
  Usage
29
34
  -----
30
35
 
36
+ ```ruby
37
+ class Translator
38
+ include Matchete
39
+
40
+ on Any, :string,
41
+ def translate(value, to)
42
+ value.to_s
43
+ end
44
+
45
+ on '#-@', :negative,
46
+ def translate(value, to)
47
+ - value
48
+ end
49
+
50
+ on String, :integer,
51
+ def translate(value, to)
52
+ value.to_i
53
+ end
54
+
55
+ default def translate(value, to)
56
+ 0
57
+ end
58
+ end
59
+
60
+ t = Translator.new
61
+ p t.translate 72, :negative # -72
62
+ p t.translate nil, :integer # 0
63
+ ```
64
+
31
65
  ```ruby
32
66
  require 'matchete'
33
67
 
data/lib/matchete.rb CHANGED
@@ -32,10 +32,23 @@ module Matchete
32
32
  convert_to_matcher method_name
33
33
  end
34
34
 
35
+ # Matches something like sum types:
36
+ # either(Integer, Array)
37
+ # matches both [2] and 2
35
38
  def either(*guards)
36
39
  -> arg { guards.any? { |g| match_guard(g, arg) } }
37
40
  end
38
41
 
42
+ # Matches an exact value
43
+ # useful if you want to match a string starting with '#' or the value of a class
44
+ # exact(Integer) matches Integer, not 2
45
+ def exact(value)
46
+ -> arg { arg == value }
47
+ end
48
+
49
+ # Matches each guard
50
+ # full_match(Integer, '#value')
51
+ # matches only instances of Integer which respond to '#value'
39
52
  def full_match(*guards)
40
53
  -> arg { guards.all? { |g| match_guard(g, arg) } }
41
54
  end
@@ -101,12 +114,15 @@ module Matchete
101
114
  end
102
115
 
103
116
  def match_guard(guard, arg)
104
- p
105
117
  case guard
106
118
  when Module
107
119
  arg.is_a? guard
108
120
  when Symbol
109
- send guard, arg
121
+ if guard.to_s[-1] == '?'
122
+ send guard, arg
123
+ else
124
+ guard == arg
125
+ end
110
126
  when Proc
111
127
  instance_exec arg, &guard
112
128
  when Regexp
data/matchete.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'matchete'
6
- s.version = '0.2.0'
6
+ s.version = '0.4.0'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Alexander Ivanov"]
9
9
  s.email = ["alehander42@gmail.com"]
@@ -4,6 +4,25 @@ require 'rspec'
4
4
  require 'matchete'
5
5
 
6
6
  describe Matchete do
7
+ it 'can use a pattern based on `either` helpers' do
8
+ class A
9
+ include Matchete
10
+
11
+ on either(FalseClass, Proc), FalseClass,
12
+ def work(g, h)
13
+ g
14
+ end
15
+
16
+ on FalseClass, NilClass,
17
+ def work(g, h)
18
+ :love
19
+ end
20
+ end
21
+
22
+ expect(A.new.work(false, false)).to eq false
23
+ expect(A.new.work(false, nil)).to eq :love
24
+ end
25
+
7
26
  it 'can be used to overload a method in a class' do
8
27
  class A
9
28
  include Matchete
@@ -132,6 +151,25 @@ describe Matchete do
132
151
  expect(A.new.play(2.2)).to eq :else
133
152
  end
134
153
 
154
+ it 'can use a pattern based on `exact` helpers' do
155
+ class A
156
+ include Matchete
157
+
158
+ on Integer,
159
+ def lala(a)
160
+ a - 2
161
+ end
162
+
163
+ on exact(Integer),
164
+ def lala(a)
165
+ a
166
+ end
167
+ end
168
+
169
+ expect(A.new.lala(4)).to eq 2
170
+ expect(A.new.lala(Integer)).to eq Integer
171
+ end
172
+
135
173
  it 'can use a pattern based on existing predicate methods given as symbols' do
136
174
  class A
137
175
  include Matchete
data/spec/spec_helper.rb CHANGED
@@ -0,0 +1,2 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matchete
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Ivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-03 00:00:00.000000000 Z
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -31,7 +31,9 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - ".travis.yml"
34
35
  - Gemfile
36
+ - Gemfile.lock
35
37
  - LICENSE
36
38
  - README.md
37
39
  - Rakefile
@@ -64,6 +66,4 @@ rubygems_version: 2.4.6
64
66
  signing_key:
65
67
  specification_version: 4
66
68
  summary: Method overloading for Ruby based on pattern matching
67
- test_files:
68
- - spec/matchete_spec.rb
69
- - spec/spec_helper.rb
69
+ test_files: []