hosh-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: 5eb5a1416bdde14aa18de27b673fce5cc8a74cdd
4
+ data.tar.gz: d8426c0fbae96a5d0d3fc68c7afb3ec151f467e9
5
+ SHA512:
6
+ metadata.gz: 2689573acabb581501156e4aa2c2882c9003886e160353c1f75c96b2623ba0a41dee538fc40ca093abcea23ef3f3dbe5ee1811c3d658448484f129610f786181
7
+ data.tar.gz: 12f324cef935e82fd8210f79278361cf5eb3bea96857d6b0bdc9702f0409b8917abedddafc595c831cab1dc9e521561ecefc5213ebb7e45813b41c575c195e33
@@ -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,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
5
+ script: bundle exec rspec
@@ -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,289 @@
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
+ [![Gem Version](https://badge.fury.io/rb/kase.svg)](https://badge.fury.io/rb/kase)
9
+ [![Build Status](https://travis-ci.org/lasseebert/kase.svg?branch=master)](https://travis-ci.org/lasseebert/kase)
10
+
11
+ ## Introduction
12
+
13
+ The idea is inspired by Elixir in which many functions returns something like
14
+ `{:ok, result}` or `{:error, :not_found, "More specific error message"}`.
15
+
16
+ In Ruby we would usually handle those kind of return values like this:
17
+
18
+ ```ruby
19
+ class Orders
20
+ def process(cart)
21
+ status, result, message = complete_order(cart)
22
+
23
+ case status
24
+ when :ok
25
+ order = result
26
+ process_order(order)
27
+ when :error
28
+ error_kind = result
29
+ case error_kind
30
+ when :not_found
31
+ [404, {}, "Not found"]
32
+ when :invalid_state
33
+ [400, {}, "Invalid request: #{message}"]
34
+ else
35
+ raise "Unhandled error kind: #{error_kind}"
36
+ end
37
+ else
38
+ raise "Unhandles status: #{status}"
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ This is hard to read.
45
+
46
+ Furthermore, the two lines that raises exception on unhandled status and
47
+ error_kind are probably getting zero code coverage (otherwise we would have
48
+ handled that specific status or error_kind).
49
+
50
+ With Kase we can do this instead, which is equivalent to the above:
51
+
52
+ ```ruby
53
+ require "kase"
54
+
55
+ class Orders
56
+ include Kase
57
+
58
+ def process(cart)
59
+ kase complete_order(cart) do
60
+ on :ok do |order|
61
+ process_order(order)
62
+ end
63
+
64
+ on :error, :not_found do
65
+ [404, {}, "Not found"]
66
+ end
67
+
68
+ on :error, :invalid_state do |message|
69
+ [400, {}, "Invalid request: #{message}"]
70
+ end
71
+ end
72
+ end
73
+ end
74
+ ```
75
+
76
+ This is much more easy to read and reason about.
77
+
78
+ See below for more a full list of what Kase does.
79
+
80
+ ## Installation
81
+
82
+ Add this line to your application's Gemfile:
83
+
84
+ ```ruby
85
+ gem "kase", "~> 0.1"
86
+ ```
87
+
88
+ ## Usage
89
+
90
+
91
+ ### kase
92
+
93
+ `kase` is the method used to match an array of values (typically a
94
+ `[status, result]`-like array) against a number of "patterns" using the
95
+ `on`-method. A pattern in this sense is just some values matching the array from
96
+ the beginning using `==`.
97
+
98
+ E.g. the pattern `:a, :b` matches `[:a, :b]` and `[:a, :b, :c]`, but not
99
+ `[:c, :a, :b]`.
100
+
101
+ The block in the first pattern that matches will be executed, and the return
102
+ value from that block is the return value of `kase`.
103
+
104
+ If no patterns match, a `Kase::NoMatchError` is raised.
105
+
106
+ An empty pattern will match everything, so that can be used as a catch-all.
107
+
108
+ The values yielded to the block are all the values that is not part of the
109
+ pattern. E.g. if `[:ok, "THE RESULT"]` is matched with `on(:ok, &block)`,
110
+ `"THE RESULT"` is yielded to block.
111
+
112
+ #### Simple examples:
113
+
114
+ ```ruby
115
+ kase process_order do
116
+ on :ok do
117
+ puts "Great success!"
118
+ end
119
+
120
+ on :error do
121
+ puts "BOOM"
122
+ end
123
+ end
124
+ ```
125
+
126
+ This will output "Great success" if process_order returns `:ok`,
127
+ `[:ok]` or `[:ok, some, more, values, here]`.
128
+
129
+ It will output "BOOM" if process_order returns `:error`, `[:error]` or
130
+ `[:error, some, more, values]`
131
+
132
+ If process_order returns something that is not matched, e.g. `:not_found`, this
133
+ will raise a `Kase::NoMatchError`.
134
+
135
+ #### Using the values
136
+
137
+ In the above example we don't use the values returned by process_order, if more
138
+ than one value is returned.
139
+
140
+ All values that are not part of the pattern will be yielded to the given block:
141
+
142
+ ```ruby
143
+ kase process_order do
144
+ on :ok do |order|
145
+ puts "Great success: #{order.inspect}"
146
+ end
147
+
148
+ on :error do |reason, message|
149
+ puts "BOOM! #{reason}: #{message}"
150
+ end
151
+ end
152
+ ```
153
+
154
+ Notice that we don't have to return the same number of values for each case to
155
+ be able to catch and use the values.
156
+
157
+ #### Matching on multiple values
158
+
159
+ We can match on multiple values, but only from the left:
160
+
161
+ ```ruby
162
+ kase process_order do
163
+ on :ok do |order|
164
+ puts "Great success: #{order.inspect}"
165
+ end
166
+
167
+ on :error, :not_found do
168
+ puts "Not found!"
169
+ end
170
+
171
+ on :error, :invalid_record do |message|
172
+ puts "Invalid record: #{message}"
173
+ end
174
+ end
175
+ ```
176
+
177
+ This `kase` will handle `[:ok, order]`, `[:error, :not_found]` and
178
+ `[:error, :invalid_record, "Message"]`, but will raise a `Kase::NoMatchError` on
179
+ e.g. `[:error, :not_authorized]`
180
+
181
+ ### ok!
182
+
183
+ Sometimes we only expect the `:ok` status to appear. In that case we can use
184
+ `ok!` as a shorthand.
185
+
186
+ It can rewrite this:
187
+
188
+ ```ruby
189
+ kase something do
190
+ on :ok do |result|
191
+ handle_result(result)
192
+ end
193
+ end
194
+ ```
195
+
196
+ To this:
197
+
198
+ ```ruby
199
+ ok! something do |result|
200
+ handle_result(result)
201
+ end
202
+ ```
203
+
204
+ ### Include or module_function
205
+
206
+ Kase is a module with helper methods. You can either include it in your own
207
+ class or use the methods as module functions. So both of these will work:
208
+
209
+ ```ruby
210
+ require "kase"
211
+
212
+ class MyFirstClass
213
+ include Kase
214
+
215
+ def call
216
+ kase some_result do
217
+ ...
218
+ end
219
+ end
220
+ end
221
+
222
+ class MySecondClass
223
+ def call
224
+ Kase.kase some_result do
225
+ ...
226
+ end
227
+ end
228
+ end
229
+ ```
230
+
231
+ All the logic resides in the Kase::Switcher class which you can use directly if
232
+ you need to:
233
+
234
+ ```ruby
235
+ switcher = Kase::Switcher.new(:ok, "RESULT")
236
+ switcher.on(:ok) { |result| puts result }
237
+ switcher.on(:error) { |message| warn message }
238
+ switcher.validate!
239
+ result = switcher.result
240
+ ```
241
+
242
+ The above is equivalent to:
243
+
244
+ ```ruby
245
+ result = Kase.kase :ok, "RESULT" do
246
+ on(:ok) { |result| puts result }
247
+ on(:error) { |message| warn message }
248
+ end
249
+ ```
250
+
251
+ Note that `#kase` is aliased to `#call` so you can use the shorthand
252
+ `Kase.(values)`.
253
+
254
+ ## Development
255
+
256
+ * Install dependencies with `bundle`
257
+ * Run specs with `bundle exec rspec`
258
+
259
+ ## Contributing
260
+
261
+ Bug reports and pull requests are welcome on GitHub at
262
+ https://github.com/lasseebert/kase.
263
+
264
+ This project is intended to be a safe, welcoming space for collaboration, and
265
+ contributors are expected to adhere to the
266
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
267
+
268
+ ### Pull requests
269
+
270
+ To make a pull request:
271
+
272
+ 1. Fork the project
273
+ 2. Make at least one failing test that proves the bug or describes the feature.
274
+ 3. Implement bugfix or feature
275
+ 4. Make pull request
276
+
277
+ ## Contact
278
+
279
+ Find me on twitter: [@lasseebert](https://twitter.com/lasseebert)
280
+
281
+ ## Alternatives
282
+
283
+ * [Noadi](https://github.com/katafrakt/noaidi) mimics the functional pattern
284
+ matching of Elixir and might be used as an alternative of Kase.
285
+
286
+ ## License
287
+
288
+ The gem is available as open source under the terms of the
289
+ [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 = "hosh-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.new(@values) 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: hosh-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-31 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.4.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A tool to parse [:ok, result]-like return values
113
+ test_files: []