kase 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb95aeef3f46aa01ab47195d790334819f152863
4
+ data.tar.gz: 4018d1351703b1507e7e18b353aeff417ecf9b30
5
+ SHA512:
6
+ metadata.gz: 830b147275360417dcffca01086ced94eef2f7f3de01ab673356ff43f50fbd217775614560a9835e2819fe1d34671ac54761876d736807437a8835b7952dc1b1
7
+ data.tar.gz: f9615f5128d39c6469fbff6c174c9b3cb395a53b507baf975a6a4487e86932fff24e2d9291ca9c5582e10b915f13436ff9edd94c4a0099a3b35a94810a770440
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -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 lasse@lasseebert.dk. 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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kase.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Lasse Skindstad Ebert
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,277 @@
1
+ # Kase
2
+
3
+ Kase gracefully handles `[status, result]`-like return values from methods.
4
+
5
+ It is a tool to avoid using exceptions as flow control and to write safer and
6
+ more readable code.
7
+
8
+ ## Introduction
9
+
10
+ The idea is inspired by Elixir in which many functions returns something like
11
+ `{:ok, result}` or `{:error, :not_found, "More specific error message"}`.
12
+
13
+ In Ruby we would usually handle those kind of return values like this:
14
+
15
+ ```ruby
16
+ class Orders
17
+ def process(cart)
18
+ status, result, message = complete_order(cart)
19
+
20
+ case status
21
+ when :ok
22
+ order = result
23
+ process_order(order)
24
+ when :error
25
+ error_kind = result
26
+ case error_kind
27
+ when :not_found
28
+ [404, {}, "Not found"]
29
+ when :invalid_state
30
+ [400, {}, "Invalid request: #{message}"]
31
+ else
32
+ raise "Unhandled error kind: #{error_kind}"
33
+ end
34
+ else
35
+ raise "Unhandles status: #{status}"
36
+ end
37
+ end
38
+ end
39
+ ```
40
+
41
+ This is hard to read.
42
+
43
+ Furthermore, the two lines that raises exception on unhandled status and
44
+ error_kind are probably getting zero code coverage (otherwise we would have
45
+ handled that specific status or error_kind).
46
+
47
+ With Kase we can do this instead, which is equivalent to the above:
48
+
49
+ ```ruby
50
+ require "kase"
51
+
52
+ class Orders
53
+ include Kase
54
+
55
+ def process(cart)
56
+ kase complete_order(cart) do
57
+ on :ok do |order|
58
+ process_order(order)
59
+ end
60
+
61
+ on :error, :not_found do
62
+ [404, {}, "Not found"]
63
+ end
64
+
65
+ on :error, :invalid_state do |message|
66
+ [400, {}, "Invalid request: #{message}"]
67
+ end
68
+ end
69
+ end
70
+ end
71
+ ```
72
+
73
+ This is much more easy to read and reason about.
74
+
75
+ See below for more a full list of what Kase does.
76
+
77
+ ## Installation
78
+
79
+ Add this line to your application's Gemfile:
80
+
81
+ ```ruby
82
+ gem "kase", "~> 0.1"
83
+ ```
84
+
85
+ ## Usage
86
+
87
+
88
+ ### kase
89
+
90
+ `kase` is the method used to match an array of values (typically a
91
+ `[status, result]`-like array) against a number of "patterns" using the
92
+ `on`-method. A pattern in this sense is just some values matching the array from
93
+ the beginning using `==`.
94
+
95
+ E.g. the pattern `:a, :b` matches `[:a, :b]` and `[:a, :b, :c]`, but not
96
+ `[:c, :a, :b]`.
97
+
98
+ The block in the first pattern that matches will be executed, and the return
99
+ value from that block is the return value of `kase`.
100
+
101
+ If no patterns match, a `Kase::NoMatchError` is raised.
102
+
103
+ An empty pattern will match everything, so that can be used as a catch-all.
104
+
105
+ The values yielded to the block are all the values that is not part of the
106
+ pattern. E.g. if `[:ok, "THE RESULT"]` is matched with `on(:ok, &block)`,
107
+ `"THE RESULT"` is yielded to block.
108
+
109
+ #### Simple examples:
110
+
111
+ ```ruby
112
+ kase process_order do
113
+ on :ok do
114
+ puts "Great success!"
115
+ end
116
+
117
+ on :error do
118
+ puts "BOOM"
119
+ end
120
+ end
121
+ ```
122
+
123
+ This will output "Great success" if process_order returns `:ok`,
124
+ `[:ok]` or `[:ok, some, more, values, here]`.
125
+
126
+ It will output "BOOM" if process_order returns `:error`, `[:error]` or
127
+ `[:error, some, more, values]`
128
+
129
+ If process_order returns something that is not matched, e.g. `:not_found`, this
130
+ will raise a `Kase::NoMatchError`.
131
+
132
+ #### Using the values
133
+
134
+ In the above example we don't use the values returned by process_order, if more
135
+ than one value is returned.
136
+
137
+ All values that are not part of the pattern will be yielded to the given block:
138
+
139
+ ```ruby
140
+ kase process_order do
141
+ on :ok do |order|
142
+ puts "Great success: #{order.inspect}"
143
+ end
144
+
145
+ on :error do |reason, message|
146
+ puts "BOOM! #{reason}: #{message}"
147
+ end
148
+ end
149
+ ```
150
+
151
+ Notice that we don't have to return the same number of values for each case to
152
+ be able to catch and use the values.
153
+
154
+ #### Matching on multiple values
155
+
156
+ We can match on multiple values, but only from the left:
157
+
158
+ ```ruby
159
+ kase process_order do
160
+ on :ok do |order|
161
+ puts "Great success: #{order.inspect}"
162
+ end
163
+
164
+ on :error, :not_found do
165
+ puts "Not found!"
166
+ end
167
+
168
+ on :error, :invalid_record do |message|
169
+ puts "Invalid record: #{message}"
170
+ end
171
+ end
172
+ ```
173
+
174
+ This `kase` will handle `[:ok, order]`, `[:error, :not_found]` and
175
+ `[:error, :invalid_record, "Message"]`, but will raise a `Kase::NoMatchError` on
176
+ e.g. `[:error, :not_authorized]`
177
+
178
+ ### ok!
179
+
180
+ Sometimes we only expect the `:ok` status to appear. In that case we can use
181
+ `ok!` as a shorthand.
182
+
183
+ It can rewrite this:
184
+
185
+ ```ruby
186
+ kase something do
187
+ on :ok do |result|
188
+ handle_result(result)
189
+ end
190
+ end
191
+ ```
192
+
193
+ To this:
194
+
195
+ ```ruby
196
+ ok! something do |result|
197
+ handle_result(result)
198
+ end
199
+ ```
200
+
201
+ ### Include or module_function
202
+
203
+ Kase is a module with helper methods. You can either include it in your own
204
+ class or use the methods as module functions. So both of these will work:
205
+
206
+ ```ruby
207
+ require "kase"
208
+
209
+ class MyFirstClass
210
+ include Kase
211
+
212
+ def call
213
+ kase some_result do
214
+ ...
215
+ end
216
+ end
217
+ end
218
+
219
+ class MySecondClass
220
+ def call
221
+ Kase.kase some_result do
222
+ ...
223
+ end
224
+ end
225
+ end
226
+ ```
227
+
228
+ All the logic resides in the Kase::Switcher class which you can use directly if
229
+ you need to:
230
+
231
+ ```ruby
232
+ switcher = Kase::Switcher.new(:ok, "RESULT")
233
+ switcher.on(:ok) { |result| puts result }
234
+ switcher.on(:error) { |message| warn message }
235
+ switcher.validate!
236
+ result = switcher.result
237
+ ```
238
+
239
+ The above is equivalent to:
240
+
241
+ ```ruby
242
+ result = Kase.kase :ok, "RESULT" do
243
+ on(:ok) { |result| puts result }
244
+ on(:error) { |message| warn message }
245
+ end
246
+ ```
247
+
248
+ Note that `#kase` is aliased to `#call` so you can use the shorthand
249
+ `Kase.(values)`.
250
+
251
+ ## Development
252
+
253
+ * Install dependencies with `bundle`
254
+ * Run specs with `bundle exec rspec`
255
+
256
+ ## Contributing
257
+
258
+ Bug reports and pull requests are welcome on GitHub at
259
+ https://github.com/lasseebert/kase.
260
+
261
+ This project is intended to be a safe, welcoming space for collaboration, and
262
+ contributors are expected to adhere to the
263
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
264
+
265
+ ### Pull requests
266
+
267
+ To make a pull request:
268
+
269
+ 1. Fork the project
270
+ 2. Make at least one failing test that proves the bug or describes the feature.
271
+ 3. Implement bugfix or feature
272
+ 4. Make pull request
273
+
274
+ ## License
275
+
276
+ The gem is available as open source under the terms of the
277
+ [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kase/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kase"
8
+ spec.version = Kase::VERSION
9
+ spec.authors = ["Lasse Skindstad Ebert"]
10
+ spec.email = ["lasse@lasseebert.dk"]
11
+
12
+ spec.summary = %q{A tool to parse [:ok, result]-like return values}
13
+ spec.homepage = "https://github.com/lasseebert/kase"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry-byebug", "~> 3.3"
25
+ end
@@ -0,0 +1,30 @@
1
+ require "kase/switcher"
2
+ require "kase/version"
3
+
4
+ module Kase
5
+ module_function
6
+
7
+ def kase(*values, &block)
8
+ Switcher.new(*values).switch(&block)
9
+ end
10
+ alias_method :call, :kase
11
+
12
+ def ok!(*values, &block)
13
+ Switcher.new(*values).switch do
14
+ on(:ok) do |*result|
15
+ if block_given?
16
+ yield(*result) if block_given?
17
+ else
18
+ case result.size
19
+ when 0
20
+ nil
21
+ when 1
22
+ result.first
23
+ else
24
+ result
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ module Kase
2
+ class KaseError < StandardError; end
3
+ class NoMatchError < KaseError; end
4
+ end
@@ -0,0 +1,60 @@
1
+ require "kase/errors"
2
+
3
+ module Kase
4
+ class Switcher
5
+ def initialize(*values)
6
+ if values.size == 1 && values.first.is_a?(Array)
7
+ values = values.first
8
+ end
9
+ @values = values
10
+ @matched = false
11
+ end
12
+
13
+ attr_reader :values
14
+ attr_reader :result
15
+
16
+ def matched?
17
+ !!@matched
18
+ end
19
+
20
+ def match?(*pattern)
21
+ values[0...pattern.size] == pattern
22
+ end
23
+
24
+ def on(*pattern)
25
+ return if matched?
26
+ return unless match?(*pattern)
27
+ @matched = true
28
+ @result = yield(*values[pattern.size..-1])
29
+ end
30
+
31
+ def validate!
32
+ raise NoMatchError unless matched?
33
+ true
34
+ end
35
+
36
+ def switch(&block)
37
+ context = eval("self", block.binding)
38
+ dsl = DSL.new(self, context)
39
+
40
+ dsl.instance_eval(&block)
41
+ validate!
42
+ result
43
+ end
44
+
45
+ class DSL
46
+ def initialize(switcher, context)
47
+ @switcher = switcher
48
+ @context = context
49
+ end
50
+
51
+ def on(*args, &block)
52
+ @switcher.on(*args, &block)
53
+ end
54
+
55
+ def method_missing(method, *args, &block)
56
+ @context.send(method, *args, &block)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Kase
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lasse Skindstad Ebert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-30 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ description:
70
+ email:
71
+ - lasse@lasseebert.dk
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - kase.gemspec
85
+ - lib/kase.rb
86
+ - lib/kase/errors.rb
87
+ - lib/kase/switcher.rb
88
+ - lib/kase/version.rb
89
+ homepage: https://github.com/lasseebert/kase
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A tool to parse [:ok, result]-like return values
113
+ test_files: []