matchcase 0.1.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.
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at geoff.lee@lendesk.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in matchcase.gemspec
5
+ gemspec
6
+
7
+ gem 'rubocop'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 matrinox
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,153 @@
1
+ # Matchcase
2
+
3
+ Ruby pattern matching done easy
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'matchcase'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install matchcase
20
+
21
+ ## Usage
22
+
23
+ Just include the gem and start pattern matching
24
+
25
+ You are used to this
26
+ ```ruby
27
+ case variable
28
+ when Numeric
29
+ 'is number'
30
+ when String
31
+ 'is string'
32
+ when Array
33
+ 'is array'
34
+ end
35
+ ```
36
+
37
+ But with more complex types...
38
+ ```ruby
39
+ case [1, 2]
40
+ when [Numeric, Numeric]
41
+ 'array with 2 numbers' # never gets called
42
+ when [1, 2]
43
+ 'array containing 1 and 2'
44
+ end
45
+ ```
46
+
47
+ What if you can do this
48
+ ```ruby
49
+ case [1, 2]
50
+ when [Numeric, Numeric]
51
+ 'array with 2 numbers' # matches!
52
+ end
53
+ ```
54
+
55
+ ## Plans
56
+
57
+ Draw inspirations from other functional languages with strong pattern matching like Haskell, Elixir, Scala, and Swift.
58
+ Ideas:
59
+ 1. Any (Object is not truly Any, BasicObject is closer - e.g. Delegator)
60
+ 2. nil (just a convenience for NilClass)
61
+ 3. Closure arguments
62
+ 4. Function arguments
63
+
64
+ ### Example Ideas
65
+
66
+ #### Any
67
+ ```ruby
68
+ case [1, 2]
69
+ when [Any, Integer]
70
+ 'any and integer'
71
+ end
72
+ ```
73
+
74
+ #### Nil
75
+ ```ruby
76
+ case [1, nil]
77
+ when [Integer, nil]
78
+ 'same as [Integer, NilClass]'
79
+ end
80
+ ```
81
+
82
+ #### Closure Arguments
83
+ ```ruby
84
+ play_with_string = match_proc(String, String) do |string, string|
85
+ string + string
86
+ end
87
+ # Add a + method to procs so more patterns can be stacked to the definition
88
+ play_with_string += match_proc(String, Integer) do |string, number = 2|
89
+ string * number
90
+ end
91
+ # This won't work on normal procs
92
+ proc {} + proc {} # raises error
93
+
94
+ # Call site
95
+ play_with_string.('Hello ', 3) # 'Hello Hello Hello '
96
+ play_with_string.('Hello ', 'World') # 'Hello World'
97
+
98
+ # With keyword arguments
99
+ run_with_options = match_proc(string: String, times: Integer) do |string:, times:|
100
+ string * times
101
+ end
102
+ run_with_options = match_proc(string1: String, string2: String) do |string1:, string2:|
103
+ string1 + string2
104
+ end
105
+
106
+ # Call site
107
+ run_with_options.(string: 'o', times: 5) # ooooo
108
+ run_with_options.(string1: 'he', string2: 'llo') # hello
109
+ run_with_options.(string1: 'Hi') # raise ArgumentError: missing keyword: string2
110
+ ```
111
+
112
+ #### Function Arguments
113
+ ```ruby
114
+ match_def(:play_with_string, String, String) do |string, string|
115
+ string + string
116
+ end
117
+ match_def(:play_with_string, String, Integer) do |string, number = 2|
118
+ string * number
119
+ end
120
+
121
+ # Call site
122
+ play_with_string('Hello ', 3) # 'Hello Hello Hello '
123
+ play_with_string('Hello ', 'World') # 'Hello World'
124
+
125
+ # With keyword arguments
126
+ match_def(:run_with_options, String, repeat: String, times: Integer, ending: [String]) do |start, repeat:, times: 1, ending:|
127
+ start + repeat * times + ending.join(', ')
128
+ end
129
+ match_def(:run_with_options, start: String, repeat: String, times: Integer, ending: String) do |start: '', repeat:, times: 2, ending: ''|
130
+ start + repeat * times + ending
131
+ end
132
+
133
+ # Call site
134
+ run_with_options(start: 'He', repeat: 'l', ending: 'o') # Hello
135
+ run_with_options(start: 'He', ending: 'o') # raise ArgumentError: missing keyword: repeat
136
+ run_with_options('He', repeat: 'l', times: 2, ending: ['o', ' ', 'world']) # Hello
137
+ run_with_options('Hick') # raise ArgumentError: missing keyword: ending
138
+ ```
139
+
140
+ ## Development
141
+
142
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
143
+
144
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
145
+
146
+ ## Contributing
147
+
148
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matrinox/matchcase. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
149
+
150
+
151
+ ## License
152
+
153
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'matchcase'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'matchcase/version'
3
+ require 'matchcase/array'
4
+ require 'matchcase/hash'
5
+
6
+ module Matchcase
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ class Array
3
+ # @param [Array] other
4
+ # @return [Boolean]
5
+ def ===(other)
6
+ return false unless other.is_a?(Array)
7
+ each_with_index.all? do |item, index|
8
+ item === other[index]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ class Hash
3
+ # @param [Hash] other
4
+ # @return [Boolean]
5
+ def ===(other)
6
+ return false unless other.is_a?(Hash)
7
+ all? do |key, item|
8
+ item === other[key]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Matchcase
3
+ VERSION = '0.1.0'
4
+ end
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'matchcase/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'matchcase'
9
+ spec.version = Matchcase::VERSION
10
+ spec.authors = ['matrinox']
11
+ spec.email = ['geoff.lee@lendesk.com']
12
+
13
+ spec.summary = 'Adds pattern matching to ruby'
14
+ spec.description = 'Pattern matching done easy'
15
+ spec.homepage = 'https://github.com/matrinox/matchcase'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.12'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+
33
+ # Fun pry tools
34
+ spec.add_development_dependency 'awesome_print', '~> 1.6.1'
35
+ spec.add_development_dependency 'pry', '~> 0.10.4'
36
+ spec.add_development_dependency 'pry-byebug', '~> 3.3.0'
37
+ spec.add_development_dependency 'pry-coolline', '~> 0.2.5'
38
+ spec.add_development_dependency 'pry-rails', '~> 0.3.4'
39
+ spec.add_development_dependency 'pry-toys', '~> 0.0.2'
40
+ spec.add_development_dependency 'pry-macro', '~> 1.0.1'
41
+ spec.add_development_dependency 'pry-state', '~> 0.1.7'
42
+ spec.add_development_dependency 'pry-inline', '~> 1.0.1'
43
+ spec.add_development_dependency 'pry-doc', '~> 0.9.0'
44
+ spec.add_development_dependency 'pry-highlight', '~> 0.1.0'
45
+ end
metadata ADDED
@@ -0,0 +1,242 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matchcase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - matrinox
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-06 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: awesome_print
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-coolline
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.2.5
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.2.5
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.3.4
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.3.4
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-toys
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.0.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.0.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-macro
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.0.1
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.0.1
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-state
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.1.7
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.1.7
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-inline
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.0.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 1.0.1
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry-doc
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.9.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.9.0
181
+ - !ruby/object:Gem::Dependency
182
+ name: pry-highlight
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 0.1.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 0.1.0
195
+ description: Pattern matching done easy
196
+ email:
197
+ - geoff.lee@lendesk.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".gitignore"
203
+ - ".rubocop.yml"
204
+ - ".ruby-style.yml"
205
+ - CODE_OF_CONDUCT.md
206
+ - Gemfile
207
+ - LICENSE
208
+ - LICENSE.txt
209
+ - README.md
210
+ - Rakefile
211
+ - bin/console
212
+ - bin/setup
213
+ - lib/matchcase.rb
214
+ - lib/matchcase/array.rb
215
+ - lib/matchcase/hash.rb
216
+ - lib/matchcase/version.rb
217
+ - matchcase.gemspec
218
+ homepage: https://github.com/matrinox/matchcase
219
+ licenses: []
220
+ metadata:
221
+ allowed_push_host: https://rubygems.org
222
+ post_install_message:
223
+ rdoc_options: []
224
+ require_paths:
225
+ - lib
226
+ required_ruby_version: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - ">="
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ required_rubygems_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ requirements: []
237
+ rubyforge_project:
238
+ rubygems_version: 2.5.1
239
+ signing_key:
240
+ specification_version: 4
241
+ summary: Adds pattern matching to ruby
242
+ test_files: []