keka 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4644a49e6ff908024e1ec91cdeec0d7a6886c9d
4
+ data.tar.gz: 6a89ab5dab98c71ab7f77a1ddfed84419e956eed
5
+ SHA512:
6
+ metadata.gz: b2ba159cfc8a028f3f834cd2b48af5184da28f8b2d85a36ad62711b3771754f812b8f01c60e426f7f39c441eb30c4dc8b7b1eabda0632a970dc94767c13c41c7
7
+ data.tar.gz: 62b1c97c626ff58d1e51b033259b9d18bf0c0f71594a73549150f6b6b37aeff5d58944c5d591564fc78ac43975b1ef16bb50725cd3f67d354e2ddb3e17917c9e
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in keka.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ keka (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.2)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.8.0)
17
+ rspec-core (~> 3.8.0)
18
+ rspec-expectations (~> 3.8.0)
19
+ rspec-mocks (~> 3.8.0)
20
+ rspec-core (3.8.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-expectations (3.8.2)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-mocks (3.8.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-support (3.8.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.16)
35
+ keka!
36
+ pry
37
+ rake (~> 10.0)
38
+ rspec (~> 3.7)
39
+
40
+ BUNDLED WITH
41
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 zino
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.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Keka
2
+
3
+ Keka (Japanese for 'result') is a wrapper that represents the result of a particular execution, along with any message returned.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'keka'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install keka
20
+
21
+ ## Usage
22
+
23
+ Below is an example of how the various methods can come together.
24
+
25
+ ```ruby
26
+ class Order
27
+ def refund
28
+ Keka.run do
29
+ # returns an err keka with provided msg if !refundable?
30
+ Keka.err_unless!(refundable?, 'Payment is no longer refundable.')
31
+ # returns an err keka with provided msg if !refund!
32
+ Keka.err_unless!(refund!, 'Refund failed. Please try again')
33
+ # execute statements if nothing 'return' from above
34
+ do_something_else
35
+ # if cancel_delivery?
36
+ # => returns an err keka with provided msg if !remove_delivery
37
+ Keka.err_unless!(remove_delivery, 'Refunded but failed to remove delivery.') if cancel_delivery?
38
+ # returns an ok keka if nothing 'return' from above
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def remove_delivery
45
+ Keka.run do
46
+ # returns an ok keka if already_removed?
47
+ Keka.ok_if! already_removed?
48
+ # returns an err keka with no msg if !remove!
49
+ Keka.err_unless! remove!
50
+ # returns an ok keka if nothing 'return' from above
51
+ end
52
+ end
53
+ end
54
+
55
+ class SomeController
56
+ def some_action
57
+ keka = @order.refund
58
+ if keka.ok?
59
+ head :ok
60
+ else
61
+ render json: { error: keka.msg }, status: 422
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ Of course, you can also use `.err_unless!`, `err_if!`, and `ok_if!` outside
68
+ of the `Keka.run` block.
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ 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).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zinosama/keka.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "keka"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/keka.gemspec ADDED
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "keka/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "keka"
8
+ spec.version = Keka::VERSION
9
+ spec.authors = ["zino"]
10
+ spec.email = ["zinocodes@gmail.com"]
11
+
12
+ spec.summary = %q{Better handle short-circuit logic, result state, and return message}
13
+ spec.description = %q{No more ad hoc passage of boolean results and error messages.}
14
+ spec.homepage = "https://github.com/zinosama/keka"
15
+ spec.license = "MIT"
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
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.7"
38
+ spec.add_development_dependency 'pry'
39
+
40
+ end
data/lib/keka.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'keka/version'
2
+
3
+ module Keka
4
+
5
+ class Halt < StandardError
6
+ attr_reader :keka
7
+ def initialize(keka)
8
+ @keka = keka
9
+ super
10
+ end
11
+ end
12
+
13
+ class Base
14
+ attr_accessor :msg
15
+
16
+ def initialize(is_success, msg)
17
+ @is_success = is_success
18
+ @msg = msg
19
+ end
20
+
21
+ def ok?
22
+ is_success
23
+ end
24
+
25
+ private
26
+ attr_reader :is_success
27
+ end
28
+
29
+ def self.err_if!(evaluator, msg = nil)
30
+ raise Halt.new(err(msg)) if (evaluator.respond_to?(:ok?) ? evaluator.ok? : evaluator)
31
+ end
32
+
33
+ def self.err_unless!(evaluator, msg = nil)
34
+ if evaluator.is_a? self::Base
35
+ return if evaluator.ok?
36
+ evaluator.msg = msg if msg
37
+ raise Halt.new(evaluator)
38
+ else
39
+ raise Halt.new(err(msg)) unless evaluator
40
+ end
41
+ end
42
+
43
+ def self.ok_if!(evaluator, msg = nil)
44
+ if evaluator.is_a? self::Base
45
+ return unless evaluator.ok?
46
+ evaluator.msg = msg if msg
47
+ raise Halt.new(evaluator)
48
+ else
49
+ raise Halt.new(ok(msg)) if evaluator
50
+ end
51
+ end
52
+
53
+ def self.run
54
+ raise 'Block required!' unless block_given?
55
+ yield
56
+ ok
57
+ rescue Halt => e
58
+ e.keka
59
+ end
60
+
61
+
62
+ # private (maybe)
63
+ def self.ok(msg = nil)
64
+ Base.new(true, msg)
65
+ end
66
+
67
+ # private (maybe)
68
+ def self.err(msg = nil)
69
+ Base.new(false, msg)
70
+ end
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ module Keka
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - zino
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-13 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
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'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: No more ad hoc passage of boolean results and error messages.
70
+ email:
71
+ - zinocodes@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".DS_Store"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".rspec_status"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - keka.gemspec
88
+ - lib/keka.rb
89
+ - lib/keka/version.rb
90
+ homepage: https://github.com/zinosama/keka
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.14
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Better handle short-circuit logic, result state, and return message
115
+ test_files: []