monadic-exceptions 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.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +13 -0
- data/lib/monadic_exceptions.rb +3 -0
- data/lib/result.rb +28 -0
- data/lib/result_sanitizer.rb +37 -0
- data/monadic-exceptions.gemspec +22 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c3699758583b43eed740a9f0c153b64fa3adc8ebf32f502111d87deb9d02fd90
|
4
|
+
data.tar.gz: 7d78da7f3a5a454c52df368d22695417b32ca89c8e8f8beadab0a6977be916f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2b1504bbbb5109349825a73d82c7ed9cef546ffa38cd90757bf1e41f80010ff21eab423a87eeacc272e7f957338cdb039ca24c96d16fa63212688d4806e63d4
|
7
|
+
data.tar.gz: f51a0591a4e9135095190b701efa9e48906aa5ee33e09954f3711de488999ff2244dc40567a3db0fd84955a26dace10bb65348b4bc65a17578f871eede0ba80f
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2023 dry-rb team
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Monadic Exceptions
|
2
|
+
|
3
|
+
This gem aims to provide an interface from plain ruby exceptions to monads(using the [dry-monad gem](https://dry-rb.org/gems/dry-monads/1.3/maybe/))
|
4
|
+
|
5
|
+
## Initial goals for this gem
|
6
|
+
|
7
|
+
- Initially I'm planning just to provide a interface between `Exception ->
|
8
|
+
Result[Failure(), Success()]` where I try to provide as much information I can
|
9
|
+
get from that exception so it's easier for you to manage it.
|
10
|
+
|
11
|
+
## Disclaimers
|
12
|
+
|
13
|
+
Today we only support `Proc` as a parameter to the `from_exception` method because I still didn't discover a nice way to manage method references.
|
data/lib/result.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/monads/all'
|
4
|
+
|
5
|
+
require_relative 'result_sanitizer'
|
6
|
+
|
7
|
+
module MonadicExceptions
|
8
|
+
# Result wrap a static method called `from_exception` that receive a Proc and
|
9
|
+
# rescue it's exception raises returning either a Failure or a Success
|
10
|
+
# variant of the Result monad.
|
11
|
+
class Result
|
12
|
+
# This static method act as a bridge between exception to Result, it
|
13
|
+
# supress any raises a method invokes and transform into a valid Failure
|
14
|
+
# return.
|
15
|
+
# @param callback [Proc]
|
16
|
+
# @return [Failure({error: Symbol, where: String, orig_exception: Exception, message: String}), Success(data)]
|
17
|
+
def self.from_exception(callback)
|
18
|
+
result = callback.call
|
19
|
+
|
20
|
+
Dry::Monads::Result::Success.new(result)
|
21
|
+
rescue => e
|
22
|
+
exception_name = MonadicExceptions::ResultSanitizer.new.treat_exception_name(e.class.to_s)
|
23
|
+
Dry::Monads::Result::Failure.new({ error: exception_name,
|
24
|
+
where: callback.source_location.first, orig_exception: e,
|
25
|
+
message: e.message })
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MonadicExceptions
|
4
|
+
# ResultSanitizer hold methods responsible to perform string transformations on the exception name
|
5
|
+
class ResultSanitizer
|
6
|
+
# @param exception [String] before calling this method call the .to_s method on the exception at hand.
|
7
|
+
# @return [Symbol]
|
8
|
+
def treat_exception_name(exception)
|
9
|
+
apply_transformations_to_string(exception, [
|
10
|
+
proc { |x| x.downcase },
|
11
|
+
proc { |x| remove_non_ascii_characters(x) },
|
12
|
+
proc { |x| x.to_sym }
|
13
|
+
])
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param input [String]
|
17
|
+
# @return [String] without any character outside the ascii range
|
18
|
+
def remove_non_ascii_characters(input)
|
19
|
+
input.gsub(/[^\x00-\x7F]/, '')
|
20
|
+
end
|
21
|
+
|
22
|
+
# This function apply any number of transformations (a proc that receive
|
23
|
+
# the value and return the transformed value) into a string input.
|
24
|
+
# @param input [String] A target to start the transformations
|
25
|
+
# @param transformations [Array<Proc>] An list of procs that perform pure transformation at a target
|
26
|
+
# @return [String] The modified input
|
27
|
+
def apply_transformations_to_string(input, transformations)
|
28
|
+
modified_string = input.dup
|
29
|
+
|
30
|
+
transformations.each do |transformation|
|
31
|
+
modified_string = transformation.call(modified_string)
|
32
|
+
end
|
33
|
+
|
34
|
+
modified_string
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'monadic-exceptions'
|
8
|
+
spec.version = '0.1.0'
|
9
|
+
spec.summary = 'A bridge between a exception and a Result monad provided by dry-monads gem'
|
10
|
+
spec.description = 'A bridge between a exception and a Result monad provided by dry-monads gem'
|
11
|
+
spec.authors = ['Cherry Ramatis']
|
12
|
+
spec.homepage = 'https://github.com/cherryramatisdev/monadic-exceptions.git'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.required_ruby_version = '>= 3.0.0'
|
16
|
+
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
spec.files = Dir['LICENSE', 'README.md', 'monadic-exceptions.gemspec', 'lib/**/*']
|
19
|
+
|
20
|
+
spec.add_dependency 'dry-monads', '~> 1.5'
|
21
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: monadic-exceptions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cherry Ramatis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-monads
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
description: A bridge between a exception and a Result monad provided by dry-monads
|
28
|
+
gem
|
29
|
+
email:
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- lib/monadic_exceptions.rb
|
37
|
+
- lib/result.rb
|
38
|
+
- lib/result_sanitizer.rb
|
39
|
+
- monadic-exceptions.gemspec
|
40
|
+
homepage: https://github.com/cherryramatisdev/monadic-exceptions.git
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
rubygems_mfa_required: 'true'
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.0.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.4.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: A bridge between a exception and a Result monad provided by dry-monads gem
|
64
|
+
test_files: []
|