safe_operation 1.0.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: 1e94fff35d4584131e4618984db17237d7e0eb20
4
+ data.tar.gz: 5990172463731f5ea965977ab30ebca21a7c5fe5
5
+ SHA512:
6
+ metadata.gz: b386772b2152fd9699f92fd7050b6da0086e503abc8082d309739302fd611eb0f9312169920cf4bae598a1c9f26385a1ff94f3439fe151ebc8aae5821ee2aaee
7
+ data.tar.gz: aa6ddc9cb1ed18e8621476d42c2806d87775621bd46fe91e3ca9564e92e10e7a2634fd82b8b1678e6779643459545db3c17bbf6ac072b8375fdb2e98a2fd9c1e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # CHANGELOG
2
+
3
+ ## Unreleased
4
+
5
+ ## 1.0.0
6
+
7
+ First implementation ([#1][pr1]) - 2017.07.09
8
+
9
+ [pr1]: https://github.com/juanitofatas/safe_operation/pull/1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at juanito-fatas@cookpad.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in safe_operation.gemspec
4
+ gemspec
5
+
6
+ gem "rake"
7
+ gem "bundler"
8
+ gem "pry"
9
+ gem "rspec", "~> 3.6.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Juanito Fatas
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,92 @@
1
+ # SafeOperation
2
+
3
+ Write safer code with SafeOperation.
4
+
5
+ ```ruby
6
+ any_operation_may_fail = ->{ User.find(1) }
7
+
8
+ operation = SafeOperation.either(any_operation_may_fail) do
9
+ # MUST handle the failure here
10
+ end
11
+
12
+ # know if operation succeeded
13
+ operation.success?
14
+
15
+ # get the result of the performed operation
16
+ operation.result
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem "safe_operation"
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install safe_operation
34
+
35
+ ## Usage
36
+
37
+ ```ruby
38
+ class User
39
+ ActiveRecordRecordNotFound = Class.new(StandardError)
40
+
41
+ def self.find(id)
42
+ (id == 1) ? new : raise(ActiveRecordRecordNotFound)
43
+ end
44
+
45
+ def self.find_by(*); end
46
+ end
47
+
48
+ class Guest
49
+ end
50
+
51
+ SafeOperation.either(->{ User.find(1) })
52
+ # => raises an SafeOperation::NoFailureHandler error
53
+
54
+ SafeOperation.either(->{ User.find(1) }) do
55
+ Guest.new
56
+ end
57
+ # => returns a SafeOperation::Success object with User
58
+ # => #<SafeOperation::Success:0x007fbc8d8c5ae0 @success=#<User:0x007fbc8d8c5b08>>
59
+
60
+ SafeOperation.either(->{ User.find(2) }) do
61
+ Guest.new
62
+ end
63
+ # => rescue ActiveRecordRecordNotFound, returns a SafeOperation::Failure object with Guest
64
+ # #<SafeOperation::Failure:0x007fbc8dbb71e0 @failure=#<Guest:0x007fbc8dbb7208>>
65
+
66
+ SafeOperation.either(->{ User.find_by(id: 42) }) do
67
+ Guest.new
68
+ end
69
+ # => returns a SafeOperation::Failure object with Guest
70
+ # #<SafeOperation::Failure:0x007fbc8dbb71e0 @failure=#<Guest:0x007fbc8dbb7208>>
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ This project follows the [Moya Contributors Guidelines][moya].
76
+ TLDR: means we give out push access easily and often.
77
+
78
+ [moya]: https://github.com/Moya/contributors
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JuanitoFatas/safe_operation.
81
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are
82
+ expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the
87
+ [MIT License](http://opensource.org/licenses/MIT).
88
+
89
+ ## Code of Conduct
90
+
91
+ Everyone interacting in the SafeOperation project’s codebases, issue trackers is expected to follow
92
+ the [code of conduct](https://github.com/JuanitoFatas/safe_operation/blob/master/CODE_OF_CONDUCT.md).
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/hack ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "safe_operation"
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
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "safe_operation/version"
4
+ require_relative "safe_operation/success"
5
+ require_relative "safe_operation/failure"
6
+
7
+ module SafeOperation
8
+ NoFailureHandler = Class.new NotImplementedError
9
+
10
+ def self.either(maybe_block)
11
+ raise NoFailureHandler if !block_given?
12
+
13
+ if maybe = maybe_block.call
14
+ Success.new maybe
15
+ else
16
+ Failure.new yield
17
+ end
18
+ rescue StandardError
19
+ Failure.new yield
20
+ end
21
+
22
+ NO_FAILURE_HANDLER_MESSAGE = "Please pass in a block to handle the failure 😅"
23
+ private_constant :NO_FAILURE_HANDLER_MESSAGE
24
+ end
@@ -0,0 +1,18 @@
1
+ module SafeOperation
2
+ class Failure
3
+ def initialize(failure)
4
+ @failure = failure
5
+ end
6
+
7
+ def result
8
+ failure
9
+ end
10
+
11
+ def success?
12
+ false
13
+ end
14
+
15
+ private
16
+ attr_reader :failure
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module SafeOperation
2
+ class Success
3
+ def initialize(success)
4
+ @success = success
5
+ end
6
+
7
+ def result
8
+ success
9
+ end
10
+
11
+ def success?
12
+ true
13
+ end
14
+
15
+ private
16
+ attr_reader :success
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module SafeOperation
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "safe_operation/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "safe_operation"
8
+ spec.version = SafeOperation::VERSION
9
+ spec.authors = ["Juanito Fatas"]
10
+ spec.email = ["katehuang0320@gmail.com"]
11
+
12
+ spec.summary = %(Write safer code with SafeOperation.)
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/juanitofatas/safe_operation"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_operation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juanito Fatas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Write safer code with SafeOperation.
14
+ email:
15
+ - katehuang0320@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/hack
30
+ - bin/setup
31
+ - lib/safe_operation.rb
32
+ - lib/safe_operation/failure.rb
33
+ - lib/safe_operation/success.rb
34
+ - lib/safe_operation/version.rb
35
+ - safe_operation.gemspec
36
+ homepage: https://github.com/juanitofatas/safe_operation
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.6.12
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Write safer code with SafeOperation.
60
+ test_files: []